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