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

outdated

An IoC container for Dart inspired by Castle Windsor

An IoC container for Dart inspired by Castle Windsor.

And a complete MVVM state management solution for Flutter.

Table of contents:

IoC and DI #

Basics #

As name suggests components are registered to container as factory deleagates. Reflection usage in Dart is not particularly legal (at least with Flutter).

Each factory delegate recieves FactoryContainer as a parameter so that dependencies can be injected into constructor via resolve<>(). Dart is good at type inference so you don't need to explicitly specify dependency types if they are the same as parameter types. Components are resolved lazily so the order of registration is not important.

See the example:

// Registration of a component for ListViewModel. On component creation two dependencies of types that correspond with constructor params will be also resolved from the container.
container.register(Component.For((c) => ListViewModel(c.resolve(), c.resolve())));

Sample code involving component registration, dependency injection and service locator style resolution:

// creating container is straighforward
final container = FactoryContainer();

// register a couple components that depend on each other
container.register(Component.For<ILogger>((c) => Logger('Demo')));
container.register(Component.For((c) => DataRepository(c.resolve())));
container.register(Component.For((c) => ListViewModel(c.resolve(), c.resolve())));

// get a component from the container
final viewModel = container.resolve<ListViewModel>();

// use it
viewModel.update();

Lifestyles #

Names #

Component overrides #

Container hierarchy #

Flutter state management and MVVM #

Root widget #

View #

ViewModel #

Tips and tricks #

Example #

2
likes
20
pub points
0%
popularity

Publisher

verified publishernuc134r.io

An IoC container for Dart inspired by Castle Windsor

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

More

Packages that depend on factory_castle