service_registry 2.0.1 service_registry: ^2.0.1 copied to clipboard
A Service registry for dart. It is a simple attempt to provide some sort of IoC in a dart environment, it makes much easier to test components that rely on external services, because we can easily moc [...]
import 'package:service_registry/service_registry.dart';
class CounterService {
int count = 0;
increment() {
count++;
}
decrement() {
count--;
}
}
void main() {
ServiceRegistry.registerService<CounterService>(new CounterService());
var counterService = ServiceRegistry.getService<CounterService>();
counterService.increment();
counterService.increment();
var currentCount = ServiceRegistry.getService<CounterService>().count;
print("CounterService current count [$currentCount]"); // 2
counterService.decrement();
// Actually you don't need to add the <CounterService> part, but it's handy for tools to know the type and provide code completion
currentCount = ServiceRegistry.getService<CounterService>().count;
print("CounterService current count [$currentCount]"); // 1
}