ltackmann / dice

Lightweight dependency injection framework for Dart
Other
34 stars 7 forks source link

Add ModuleContainer #8

Closed antonmoiseev closed 11 years ago

antonmoiseev commented 11 years ago

Add ModuleContainer and convenient named factory constructor for Injector to address the problem of injecting classes from one module into a class from another module. As you can see implementation is simple and straightforward, it also doesn't break existing API. It works for our team, hope you will find it useful.

Example:

// Library package.
class DefaultProductService implements ProductService {}

class LibraryModule extends Module {
  configure() {
    bind(ProductService).toType(DefaultProductService);
  }
}


// Main application package.
class CatalogPage {
  @inject ProductService productService; // Class from Library module.
}

class AppModule extends Module {
  configure() {
    bind(CatalogPage).toType(CatalogPage);
  }
}


// Usage in the main app
main () {
  Injector injector = new Injector.fromModules([new LibraryModule(), new AppModule()]);
  var catalogPage = injector.getInstance(CatalogPage);
}
ltackmann commented 11 years ago

Thanks I will review it as soon as possible