1 /**
2 Aedi, a dependency injection library.
3 
4 Aedi is a dependency injection library. It does provide a set of containers that do
5 IoC, and an interface to configure application components (structs, objects, etc.) 
6 
7 Aim:
8 The aim of library is to provide a dependency injection solution that is
9 feature rich, easy to use, easy to learn, and easy to extend up to your needs.
10 
11 Usage:
12 The process of configuring components using Aedi consists of following steps:
13 
14 $(UL
15     $(LI Create a container )
16     $(LI Register an application component. Any data (struct, object, union, etc) is treated as application component. )
17     $(LI Write a wiring configuration )
18     $(LI Repeat process for other components. )
19     $(LI Boot container )
20 )
21 
22 First of all a container should be created:
23 ---------------
24     SingletonContainer container = new SingletonContainer;
25 ---------------
26 
27 Container is responsible for storing, and managing application's components.
28 
29 Next, register component into container:
30 ---------------
31     container.register!Color
32 ---------------
33 
34 Component is registered by calling .register method on container with type of component.
35 Note, that in example we do not end the statement. That's because component should be 
36 configured next:
37 ---------------
38         .set!"r"(cast(ubyte) 250)
39         .set!"g"(cast(ubyte) 210)
40         .set!"b"(cast(ubyte) 255);
41 ---------------
42 
43 .set method configures component properties to specific values (setter injection in other words).
44 Note the example ends in `;` which means that it's end of statement and Color registration/configuration.
45 Once components are registered and configured, container needs to be booted (instantiated):
46 ---------------
47     container.instantiate();
48 ---------------
49 
50 Container during boot operation, will do various stuff, including creation and wiring of components
51 between them. It's important to call `container.instantiate()` after all application's components 
52 have been registered into container, otherwise it is not guaranteed that application will work correctly.
53 
54 Once container is booted, components in it are available for use. 
55 To fetch it use locate method like in following example:
56 --------------- 
57 container.locate!Color.print;
58 ---------------
59 
60 Try running the example, the output of example will be the Color that was registered in container.
61 ---------------
62 Color is:	Color(250, 210, 255)
63 ---------------
64 
65 License:
66 	Boost Software License - Version 1.0 - August 17th, 2003
67 
68 	Permission is hereby granted, free of charge, to any person or organization
69 	obtaining a copy of the software and accompanying documentation covered by
70 	this license (the "Software") to use, reproduce, display, distribute,
71 	execute, and transmit the Software, and to prepare derivative works of the
72 	Software, and to permit third-parties to whom the Software is furnished to
73 	do so, all subject to the following:
74 	
75 	The copyright notices in the Software and this entire statement, including
76 	the above license grant, this restriction and the following disclaimer,
77 	must be included in all copies of the Software, in whole or in part, and
78 	all derivative works of the Software, unless such copies or derivative
79 	works are solely in the form of machine-executable object code generated by
80 	a source language processor.
81 	
82 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
85 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
86 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
87 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
88 	DEALINGS IN THE SOFTWARE.
89 
90 Authors:
91 	aermicioi
92 **/
93 module app;
94 
95 import aermicioi.aedi;
96 import std.stdio;
97 
98 /**
99 A struct that should be managed by container.
100 **/
101 struct Color {
102     ubyte r;
103     ubyte g;
104     ubyte b;
105 }
106 
107 void print(Color color) {
108     writeln("Color is:\t", color);
109 }
110 
111 void main() {
112     SingletonContainer container = new SingletonContainer; // Creating container that will manage a color
113     
114     container.register!Color // Register color into container.
115         .set!"r"(cast(ubyte) 250) // Set red color to 250
116         .set!"g"(cast(ubyte) 210) // Set green color to 210
117         .set!"b"(cast(ubyte) 255); // Set blue color to 255
118         
119     container.instantiate(); // Boot container (or prepare managed code/data).
120     
121     container.locate!Color.print; // Get color from container and print it.
122 }