factory_castle 2.0.2
factory_castle: ^2.0.2 copied to clipboard

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.

Pub Version Pub Version test codecov

IoC and DI #

Setup #

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 #

import 'package:factory_castle/factory_castle.dart';

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<MyService>((c) => MyService(c.res<Logger>(), c.res<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 registered component type if you don't want to abstract it with any interface.

Next example does exactly the same as previous one:

container.register((c) => MyService(c.res(), c.res()));

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

Components are resolved lazily which makes the order of registration not important. See the full example:

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

Registering components under multiple interfaces is not supported yet. The following workaround is suggested:

final logger = FileLogger();
container.register<ILogger>((c) => logger);
container.register<Logger>((c) => logger);

Obtain components #

Call FactoryContainer.resolve<>() with component type to obtain it from container.

final repo = container.resolve<UserRepository>();

Lifestyles #

Component lifecycle can be specified via optional parameter in FactoryContainer.register():

container.register((c) => CacheEntry(c.res(), c.res()), lifestyle: Lifestyle.Transient);

Currently two lifecycle options are available.

Singleton #

Component's factory delegate is called once on first resolve<>(). Same instance is returned on every subsequent call. This is default lifestyle.

Transient #

Component's factory delegate is called on every resolve<>() call. There is no way to specify dynamic parameters yet.

Names #

You can specify component name so that this exact component is resolved when needed.

container.register<ILogger>((c) => DbLogger(), name: 'DbLogger');
container.register<ILogger>((c) => FileLogger(), name: 'FileLogger');

// ...

final fileLog = container.resolve<ILogger>(name: 'FileLogger');

Component overrides #

By default every unnamed registered component overrides previous. That is not a final design and can be changed in future. For now you can register multiple unnamed components and expect to get last one on resolve<>().

container.register<ICacheFactory>((c) => DefaultCacheFactory());
container.register<ICacheFactory>((c) => MyCacheFactory());

final cacheFactory = container.resolve<ICacheFactory>(); // MyCacheFactory

This behaviour is not consistent when both named and unnamed components are registered.

Container disposal #

Container and component disposal is not yet implemented.

Container hierarchy #

Flutter state management and MVVM #

Root widget #

View #

ViewModel #

Service Locator #

Obtaining TickerProvider #

Example #

Cookbook #

Roadmap #

Planned features.

Parameterized transient components creation #

container.registerTransient((c, p) => CacheEntry(p['key'], c.res()));

// ...

final entry = container.resolve<CacheEntry>(params: { 'key': key });

Register components under multiple interfaces #

container.register<IStorage, FileStorage>((c) => FileStorage(/*...*/));

Mark component as default upon registration #

container.register((c) => FileStorage(/*...*/, asDefault: true));
2
likes
90
pub points
12%
popularity

Publisher

nuc134r.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

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on factory_castle