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 This tutorial will show how to register several components, and reference them 12 as dependencies. 13 14 Just registering a component with predefined data, is not enough even in smallest applications, 15 since an application is an amalgamation of interconnected components. Therefore the framework does 16 allow client code to register components that have dependencies, and reference them in configuration 17 of those components. 18 19 For example, in application that simulates cars, a car clearly is not a single god object. It is a 20 set of interconnected components, represented by objects in application. Though as seen in listing, 21 the car has it’s color and size hard-coded to specific values, and modifications to them aren’t 22 possible to do. Having a car simulator such code is not desired, since a simulator should be able 23 to simulate multiple types of cars with different sizes and colors. 24 25 ------------------ 26 class Car { 27 private Color color_; 28 private Size size; 29 30 public this() { 31 color_ = Color(255, 255, 100); 32 sizes = Size(200, 150, 500); 33 } 34 } 35 ------------------ 36 37 In improvement to the defined car in application (above), could be done by exposing color and size of 38 car to configuration from exterior environment 39 40 ------------------ 41 class Car { 42 private Color color_; 43 private Size size; 44 45 public this(Size size) { 46 this.size = size; 47 } 48 49 public @property void color(Color color) { 50 this.color_ = color; 51 } 52 } 53 54 void main() { 55 Car myCar = new Car(Size(200, 150, 500)); 56 myCar.color = Color(0, 255, 0); 57 } 58 ------------------ 59 60 Having the ability to change color and size of a car it is possible to have now multiple instances 61 of a car, with different sizes and colors, though with increase of different types cars, the amount 62 of code required to instantiate and configure them with right sizes increases tremendously, and in 63 the end can become complex, and cumbersome to understand. Not to mention, the importance of 64 micromanaging the order of how cars component instantiate. A car cannot be instantiated before, her 65 components. Clearly the solution is more flexible, yet at a cost of increased amount of code. 66 67 A DI framework aims to lessen the burden of writing wiring code, and will eliminate the need 68 of micromanaging the order component creation. It will do instead of developers, just like in following 69 example 70 71 ------------------ 72 SingletonContainer container = singleton(); // Creating container that will manage a color 73 74 with (container.configure) { 75 76 register!Color // Register color into container. 77 .set!"r"(cast(ubyte) 0) // Set red color to 0 78 .set!"g"(cast(ubyte) 255) // Set green color to 255 79 .set!"b"(cast(ubyte) 0); // Set blue color to 0 80 81 register!Size // Register a size into container 82 .set!"width"(200UL) // As for color set width to 150. 83 .set!"height"(150UL) 84 .set!"length"(500UL); 85 86 register!Car // Register a car in container 87 .construct(lref!Size) // Construct car using size present in container 88 .set!"color"(lref!Color); // Set car color to color present in container. 89 } 90 91 container.instantiate(); // Boot container (or prepare managed code/data). 92 93 container.locate!Car.print; 94 ------------------ 95 96 Referencing a dependency in a Car can be done using $(D_INLINECODE lref!Type) notation, that allows 97 framework to detect that a dependency is actually required to pass, and not a simple value. Therefore 98 the framework will search for a value of a type $(D_INLINECODE Type) and pass it as a dependency to Car. 99 The result of a container can be seen by fetching a car from container and printing it to the 100 stdout 101 102 At end we should see the car: 103 ------------------ 104 You bought a new car with following specs: 105 Size: Size(200, 150, 500) 106 Color: Color(0, 255, 0) 107 ------------------ 108 109 Try example in this module. Modify it, play with it to understand how to configure dependencies 110 between components. 111 112 License: 113 Boost Software License - Version 1.0 - August 17th, 2003 114 115 Permission is hereby granted, free of charge, to any person or organization 116 obtaining a copy of the software and accompanying documentation covered by 117 this license (the "Software") to use, reproduce, display, distribute, 118 execute, and transmit the Software, and to prepare derivative works of the 119 Software, and to permit third-parties to whom the Software is furnished to 120 do so, all subject to the following: 121 122 The copyright notices in the Software and this entire statement, including 123 the above license grant, this restriction and the following disclaimer, 124 must be included in all copies of the Software, in whole or in part, and 125 all derivative works of the Software, unless such copies or derivative 126 works are solely in the form of machine-executable object code generated by 127 a source language processor. 128 129 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 130 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 131 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 132 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 133 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 134 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 135 DEALINGS IN THE SOFTWARE. 136 137 Authors: 138 aermicioi 139 **/ 140 141 module dependencies; 142 143 import aermicioi.aedi; 144 import std.stdio; 145 146 /** 147 A struct that should be managed by container. 148 **/ 149 struct Color { 150 ubyte r; 151 ubyte g; 152 ubyte b; 153 } 154 155 /** 156 Size of a car. 157 **/ 158 struct Size { 159 160 ulong width; 161 ulong height; 162 ulong length; 163 } 164 165 /** 166 A class representing a car. 167 **/ 168 class Car { 169 170 private { 171 Color color_; // Car color 172 Size size_; // Car size 173 } 174 175 public { 176 177 /** 178 Constructor of car. 179 180 Constructs a car with a set of sizes. A car cannot of course have 181 undefined sizes, so we should provide it during construction. 182 183 Params: 184 size = size of a car. 185 **/ 186 this(Size size) { 187 this.size_ = size; 188 } 189 190 @property { 191 192 /** 193 Set color of car 194 195 Set color of car. A car can live with undefined color (physics allow it). 196 197 Params: 198 color = color of a car. 199 200 Returns: 201 Car 202 **/ 203 Car color(Color color) @safe nothrow { 204 this.color_ = color; 205 206 return this; 207 } 208 209 Color color() @safe nothrow { 210 return this.color_; 211 } 212 213 Size size() @safe nothrow { 214 return this.size_; 215 } 216 } 217 } 218 } 219 220 void print(Car car) { 221 "You bought a new car with following specs:".writeln; 222 writeln("Size:\t", car.size()); 223 writeln("Color:\t", car.color()); 224 } 225 226 void main() { 227 SingletonContainer container = singleton(); // Creating container that will manage a color 228 scope(exit) container.terminate(); 229 230 with (container.configure) { 231 232 register!Color // Register color into container. 233 .set!"r"(cast(ubyte) 0) // Set red color to 0 234 .set!"g"(cast(ubyte) 255) // Set green color to 255 235 .set!"b"(cast(ubyte) 0); // Set blue color to 0 236 237 register!Size // Register a size into container 238 .set!"width"(200UL) // As for color set width to 150. 239 .set!"height"(150UL) 240 .set!"length"(500UL); 241 242 register!Car // Register a car in container 243 .construct(lref!Size) // Construct car using size present in container 244 .set!"color"(lref!Color); // Set car color to color present in container. 245 } 246 247 container.instantiate(); // Boot container (or prepare managed code/data). 248 249 container.locate!Car.print; // Get color from container and print it. 250 }