service_registry 2.0.1 copy "service_registry: ^2.0.1" to clipboard
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 [...]

service_registry #

Version Build Status

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>(new CounterService());

  var counterService = ServiceRegistry.getService<CounterService>();
  counterService.increment();
  counterService.increment();
  print(ServiceRegistry.getService<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>().count); // 1
}


TODO #

  • [] add coverage reporting
  • [] check service type with the given register type
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

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 mock then and add to the registry

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

More

Packages that depend on service_registry