infusion 0.1.0 copy "infusion: ^0.1.0" to clipboard
infusion: ^0.1.0 copied to clipboard

SDKDart
outdated

A light weight dependency injector that can be used with any dart project.

example/infusion_example.dart

import 'package:infusion/infusion.dart';

class ServiceInterfaceA {
  void printMethod(String text) {}
}

class ServiceA implements ServiceInterfaceA {
  @override
  void printMethod(String text) {
    print("ServiceA: $text");
  }
}

class ServiceInterfaceB {
  // Make this interface un-initializable
  ServiceInterfaceB._private();

  String anotherMethod() => "This should NOT be returned";
}

class ServiceB implements ServiceInterfaceB {
  @override
  String anotherMethod() => "This WILL be returned instead";
}

abstract class ServiceInterfaceC {
  void abstractMethod();
}

class ServiceC implements ServiceInterfaceC {
  final ServiceA serviceA;
  final ServiceB serviceB;

  // serviceA & serviceB will be injected
  ServiceC(this.serviceA, this.serviceB);

  @override
  void abstractMethod() {
    serviceA.printMethod(serviceB.anotherMethod());
  }
}

void main() {
  var container = Container();
  container.addSingleton<ServiceA, ServiceA>();
  container.addService<ServiceB, ServiceB>();
  container.addService<ServiceC, ServiceC>();

  final serviceC = container.getService<ServiceC>();
  serviceC.abstractMethod();
}
0
likes
160
points
22
downloads

Publisher

unverified uploader

Weekly Downloads

A light weight dependency injector that can be used with any dart project.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on infusion