factory_castle 2.0.1 copy "factory_castle: ^2.0.1" to clipboard
factory_castle: ^2.0.1 copied to clipboard

outdated

IoC container inspired by Castle Windsor + Flutter MVVM framework. Your whole app architecture in a single package you've been dreaming about.

IoC container for Dart inspired by Castle Windsor + Flutter MVVM framework.

IoC and DI #

Set up #

IoC/DI package is platform independent so can be used in both command line apps and Flutter. Just add it to your pubspec.yaml:

dependencies:
  factory_castle:

Creating container #

final container = FactoryContainer(); 

Component registration #

As name suggests components are registered into container as factory deleagates. You have to manually inject dependencies into constructor (luckily, Dart will help you).

Reflection could be used to avoid manual injection but sadly dart:mirrors library is not available for Flutter. Reflection support for non-Flutter apps may be intorduced later via separate package.

Let's register MyService which takes Logger and Config objects as parameters:

container.register(Component.For<MyService>((c) => MyService(c.resolve<Logger>(), c.resolve<Config>())));

Dart is good at type inference so you don't need to explicitly specify dependency types if they are the same as parameter types. You can also omit type in Component.For<>:

container.register(Component.For((c) => MyService(c.resolve(), c.resolve())));

Each factory delegate recieves current FactoryContainer instance as a parameter so that dependencies can be injected into constructor via resolve<>() method.

Components are resolved lazily so the order of registration is not important. See the full example:

container.register(Component.For((c) => MyService(c.resolve(), c.resolve())));
container.register(Component.For((c) => Logger()));
container.register(Component.For((c) => Config(String.fromEnvironment('Flavor'))));

Lifestyles #

Names #

Component overrides #

Container hierarchy #

Flutter state management and MVVM #

Root widget #

View #

ViewModel #

Tips and tricks #

Example #

2
likes
0
pub points
0%
popularity

Publisher

verified publishernuc134r.io

IoC container inspired by Castle Windsor + Flutter MVVM framework. Your whole app architecture in a single package you've been dreaming about.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on factory_castle