Module aermicioi.aedi
Aedi, a dependency injection library.
Aedi is a dependency injection library. It does provide a set of containers that do IoC, and an interface to configure application components (structs, objects, etc.)
Aim:
The aim of library is to provide a dependency injection solution that is feature rich, easy to use, easy to learn, and easy to extend up to your needs.
Why should it be used:
- Decouples components in your application.
- Decreases hassle with application components setup (wiring and creation)
- Increases code reusability (since no dependencies are created in dependent objects.)
- Allows easier to test code that is dependent on other components
- Eases the implementation single responsibility principle in components
When should it be used:
- When an application has to be highly configurable.
- When an application has a high number of interdependent components.
- When doing unit testing of highly dependent components.
How should it be used:
It's simple:
- Create a container
- Register an application component. Any data (struct, object, union, etc) is treated as application component.
- Write a wiring configuration
- Repeat process for other components.
- Boot container
First of all a container should be created:
SingletonContainer container = new SingletonContainer;
Container is responsible for storing, and managing application's components.
Next, register component into container:
container .register!Color
Component is registered by calling .register method on container with type of component. Note, that in example we do not end the statement. That's because component should be configured next:
.set!"r"(cast(ubyte) 250)
.set!"g"(cast(ubyte) 210)
.set!"b"(cast(ubyte) 255);
.set method configures component properties to specific values (setter injection in other words).
Note the example ends in ;
which means that it's end of statement and Color registration/configuration.
Once components are registered and configured, container needs to be booted (instantiated):
container .instantiate();
Container during boot operation, will do various stuff, including creation and wiring of components
between them. It's important to call container
after all application's components
have been registered into container, otherwise it is not guaranteed that application will work correctly.
Once container is booted, components in it are available for use. To fetch it use locate method like in following example:
container .locate!Color .writeln;
Try running the minimal example from examples folder, the output of example will be the Color that was registered in container:
Color is: Color(250, 210, 255)