14.6 StyletIoC module
Any application of reasonable complexity is going to contain a lot of IoC container configuration: there's a lot of explicit binding of interfaces to implementations, specifying singletons, and other (entirely necessary) configuration.
The default method is to put all this configuration in your bootloader, which makes sense: it's all in one place, and it's easy to see what's going on.
However, as your design grows, you may find yourself looking for a way to split the IoC container configuration slightly. That's what modules do.
I think an example is the best way to show how they work. I won't include hundreds of lines of IoC configuration just for the sake of example - I'll just include a few, the rest is up to your imagination.
forward:
class Bootstrapper : Bootstrapper<IShellViewModel>
{
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
builder.Bind<IServiceAComponent1>().To<ServiceAComponent1>().InSingletonScope();
builder.Bind<IServiceAComponent2>().To<ServiceAComponent2>().InSingletonScope();
builder.Bind<ISomeFactory>().ToAbstractFactory();
}
}
back:
class ServiceAModule : StyletIoCModule
{
protected override void Load()
{
Bind<IServiceAComponent1>().To<ServiceAComponent1>().InSingletonScope();
Bind<IServiceAComponent2>().To<ServiceAComponent2>().InSingletonScope();
}
}
class FactoriesModule : StyletIoCModule
{
protected override void Load()
{
Bind<ISomeFactory>().ToAbstractFactory();
}
}
class Bootstrapper : Bootstrapper<IShellViewModel>
{
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
builder.AddModule(new ServiceAModule());
builder.AddModule(new FactoriesModulie());
}
}
As you can see, we have been able to break down the container configuration into modules. Each module is responsible for doing some container configuration, and then bootstrap adds all these modules to the container. While it's more code, you have a tool to separate and modularize things a bit, which can be very useful at times.
Modules are just StyletIoCModule
classes that inherit from abstract classes. You have to override an abstract method void Load()
: this method is called when a module is added to the container and is responsible for adding all bindings. If you try to add bindings anywhere else (such as in the constructor), they will have no effect.
StyeltIoCModule
There are two available protected methods available to you: Bind<TService>()
and Bind(Type serviceType)
. They are IStyletIoCBuilder
exactly the same as above and you can use them to create bindings.
Finally, IStyletIoCBuilder
there is a AddModule
method called on that takes a module instance as an argument. This just runs the module's Load
methods and adds any resulting bindings to itself. If you add the same module twice, you'll get duplicate bindings - plain and simple.
Original address of project: https://github.com/canton7/Stylet
Original address of current document: https://github.com/canton7/Stylet/wiki/StyletIoC-Modules
Previous section: WPF MVVM Framework Stylet Development Documentation 14.5 StyletIoC Factory
Next section: WPF MVVM Framework Stylet Development Documentation 14.7-14.9 StyletIoC Miscellaneous
Previous: WPF MVVM Framework Stylet Development Documentation 13. Validating Model Base Class ValidatingModelBaseNext
: WPF MVVM Framework Stylet Development Documentation 15. The ViewManager