service_registry 1.0.0 service_registry: ^1.0.0 copied to clipboard
A Service registry for dart.
service_registry #
A service registry for doing IoC on Dart.
Motivation #
Basically because I did not know how to properly handle singletons in a flutter application, so I did this for a project and felt that I could share.
Usage #
import 'package:service_registry/service_registry.dart';
class CounterService {
int count = 0;
increment() {
count++;
}
decrement() {
count--;
}
}
void main() {
ServiceRegistry.registerService<CounterService>(CounterService, new CounterService());
var counterService = ServiceRegistry.getService<CounterService>(CounterService);
counterService.increment();
counterService.increment();
print(ServiceRegistry.getService<CounterService>(CounterService).count); // 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
print(ServiceRegistry.getService<CounterService>(CounterService).count); // 1
}