minimal

Aedi, a dependency injection framework.

Aedi is a dependency injection framework. It does provide a set of containers that do IoC, and an interface to configure application components (structs, objects, etc.) managed by framework.

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.

Usage: The process of configuring components using Aedi consists of following steps:

  • Create a container
  • Register an application component.
  • Bind dependencies to it.
  • Repeat process for other components.
  • Boot container, and use components from it
  • Shutdown container

Following code example shows how can a IoC container be configured and used:

1 module app;
2 
3 import aermicioi.aedi;
4 import std.stdio;
5 
6 /**
7 A struct that should be managed by container.
8 **/
9 @component
10 struct Color {
11     @setter(cast(ubyte) 255)
12     ubyte r;
13 
14     @setter(cast(ubyte) 10)
15     ubyte g;
16 
17     @setter(cast(ubyte) 10)
18     ubyte b;
19 }
20 
21 /**
22 Size of a car.
23 **/
24 @component // Register component using annotations
25 struct Size {
26 
27     @setter(200UL) // Set property to specific value
28     ulong width;
29 
30     @setter(150UL)
31     ulong height;
32 
33     @setter(500UL)
34     ulong length;
35 }
36 
37 /**
38 A class representing a car.
39 **/
40 @component
41 class Car {
42 
43     private {
44         Color color_; // Car color
45         Size size_; // Car size
46     }
47 
48     public {
49 
50         @constructor(lref!Size) // Construct component using Size component from IoC container.
51         this(Size size) {
52             this.size_ = size;
53         }
54 
55         @property {
56 
57             @autowired // Autowire property with a component from IoC container
58             Car color(Color color) @safe nothrow {
59             	this.color_ = color;
60 
61             	return this;
62             }
63 
64             inout(Color) color() @safe nothrow pure inout {
65                 return this.color_;
66             }
67 
68             inout(Size) size() @safe nothrow pure inout {
69                 return this.size_;
70             }
71         }
72     }
73 }
74 
75 class Mercedes : Car {
76     this(Size s) {
77         super(s);
78     }
79 }
80 
81 class Volkswagen : Car {
82     this(Size s) {
83         super(s);
84     }
85 }
86 
87 @component // Configuration component that creates components managed by container.
88 class Manufacturer {
89 
90     public {
91         @component // Add component to container that is constructed by Manufacturer.
92         Mercedes makeMercedes() {
93             return new Mercedes(Size(201, 150, 501));
94         }
95     }
96 }
97 
98 void print(Car car) {
99     writeln("You bought a new ", car.classinfo.name, " with following specs:");
100     writeln("Size:\t", car.size());
101     writeln("Color:\t", car.color());
102 }
103 
104 void main() {
105     auto container = singleton(); // 1. Create container that will manage a color
106     scope(exit) container.terminate(); // 6. Shutdown the container
107 
108     with (container.configure) {
109 
110         register!Volkswagen // 2. Register color into container.
111             .construct(Size(100, 200, 500))
112             .set!"color"("blue".lref); // 3. Bind blue color from container
113 
114         register!Color("blue")
115             .set!"r"(cast(ubyte) 0)
116             .set!"g"(cast(ubyte) 0)
117             .set!"b"(cast(ubyte) 255);
118     }
119 
120     container.scan!app; // 4. Scan app module, and register all annotated components.
121 
122     container.instantiate(); // 5. Start the IoC container.
123 
124     container.locate!Car.print; // 5. Use component from IoC container.
125     container.locate!Mercedes.print;
126     container.locate!Volkswagen.print;
127 }

Members

Classes

Car
class Car

A class representing a car.

Structs

Color
struct Color

A struct that should be managed by container.

Size
struct Size

Size of a car.

Meta

Authors

aermicioi

License

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:

The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.