Module aermicioi.aedi.test.example

Example

Example using object storage to store different data, with reference behavior or value behavior.

ObjectStorage!() storage = new ObjectStorage!();

storage.register(Color(255, 255, 255), "color.white"); //Save a struct in storage of values, identified by color.white.
storage.register("Aedi tutorial.", "book.title"); //Same as with Color struct.
storage.register("A value saved by it's type FQN"); //Save a value in storage, identified by it's type FQN.

assert(storage.locate!Color("color.white") == Color(255, 255, 255));
assert(storage.locate!string("book.title") == "Aedi tutorial.");
assert(storage.locate!string == "A value saved by it's type FQN");

Example

A small example on how is possible to use aedi.

SingletonContainer container = new SingletonContainer; // Container that will hold our objects.

container.register!Bookshelf; // Register Bookshelf as a component in container.

container.register!Color // Register Color struct as a component in container.
    .set!"r"(cast(ubyte) 200)
    .set!"g"(cast(ubyte) 201)	// Set field g to ubyte 201.
    .set!"b"(cast(ubyte) 202);
container.link(name!Color, "color.white");
    
container.register!Size // Register Size struct as a component in container.
    .set!"width"(295)
    .set!"height"(210); // Set field height to ubyte 210.
    
container.register!Paper("page.paper") // Register paper by page.paper identity.
    .construct( // instantiate Paper using it's constructor.
        Paper.Quality.high,
        "color.white".lref
    );
    
container.register!Page() // store component in Prototype container
    .autowire!"size"
    .set!"paper"("page.paper".lref) // set paper to an object located in container.
    .autowire!"textColor"
    .autowire!"foreground";
    
container.instantiate(); // prepare container for serving objects.

assert(container.locate!Bookshelf !is null); // Check if instantiated
assert(container.locate!Color == Color(200, 201, 202)); // Checking if Color was properly set.
assert(container.locate!Size == Size(295, 210));
assert(container.locate!Paper("page.paper").quality == Paper.Quality.high); // Checking if quality of page.paper is same as for Paper.Quality in container 
assert(container.locate!Page.size == container.locate!Size); // Check if page's size was set correctly.

Example

Full version of example.

Contains

  • Composite container
  • Code api - register family of functions
  • construct - construct using constructor
  • set - set field/method
  • autowire - autowire constructor/field/method
  • fact - construct using a delegate/function
  • callback - manipulate object using a delegate/function
  • factoryMethod - construct using another object

ApplicationContainer container = new ApplicationContainer; // A composite container that consists of Singleton, Prototype, containers and a value storage. 

container.register(Color(255, 255, 255), "color.white"); //Save a struct in storage of values, identified by color.white.
container.register("Aedi tutorial.", "book.title"); //Same as with Color struct.
container.registerInto("A value saved by it's type FQN"); //Save a value in storage, identified by it's type FQN.
container.registerInto(Paper.Quality.medium);

container.registerInto!Bookshelf; // Register Bookshelf as a component in container.

container.registerInto!Color // Register Color struct as a component in container.
    .set!"r"(cast(ubyte) 200)
    .set!"g"(cast(ubyte) 201)	// Set field g to ubyte 201.
    .set!"b"(cast(ubyte) 202);
    
container.registerInto!Size // Register Size struct as a component in container.
    .set!"width"(295)
    .set!"height"(210); // Set field g to ubyte 210.
    
container.register!Paper("page.paper")
    .construct( // instantiate Paper using it's constructor.
        lref!(Paper.Quality), // lref is used to indicate that argument, is located in container. Check if it is of right type will be done at runtime.
        "color.white".lref
    );
    
container.register!(Paper, HardenedPaper) // Register HardenedPaper in container with identity of Paper's FQN.
    .callback( // use the delegate to construct HardenedPaper. Useful to use when some special instantiation logic is required. 
        delegate(Locator!() loc) {
            return new HardenedPaper(Paper.Quality.high, loc.locate!Color);
        }
    );
    
container.registerInto!Cover
    .autowire!"material" // autowire material. Searches a value for material field in container by it's type FQN.
    .set!"title"("Random title");

container.registerInto!Page("prototype") // store component in Prototype container
    .autowire!"size"
    .set!"paper"("page.paper".lref) // set paper to an object located in container.
    .autowire!"textColor"
    .autowire!"foreground"
    .autowire!"text";
    
container.registerInto!Book
    .factoryMethod!(Bookshelf, "getABook")(lref!Bookshelf) // Use Bookshelf's method getABook to create the Book.
    .callback(
        (Locator!() loc, Book book) { // Use it, when object is required to be configured in way that is not possible to do by means of library annotations.
            Page[] pages;
            foreach (i; 0 .. 99) {
                import std.conv;
                
                pages ~= loc.locate!Page;
                pages[$ - 1].text("Some text on page " ~ i.to!string);
            }
            
            book.pages(pages);
        }
    );

container.instantiate(); // prepare container to serve objects in it (instantiates them, and performs other things).

assert(container.locate!Bookshelf !is null); // Check if instantiated
assert(container.locate!Color == Color(200, 201, 202)); // Checking if Color was properly set.
assert(container.locate!Size == Size(295, 210));
assert(container.locate!Paper("page.paper").quality == container.locate!(Paper.Quality)); // Checking if quality of page.paper is same as for Paper.Quality in container 
assert(container.locate!Cover.material is container.locate!Paper); // Checking if cover material is from a hardened paper.

Example

Example on annotation based api.

Contains

  • Scan of one component (struct/object).
  • Scan of one component and registration by one of it's interface.
  • Scan of multiple components in one sweep

ApplicationContainer container = new ApplicationContainer;

container.register(Color(255, 255, 255), "color.white"); //Save a struct in storage of values, identified by color.white.
container.register("Aedi tutorial.", "book.title"); //Same as with Color struct.
container.registerInto("A value saved by it's type FQN"); //Save a value in storage, identified by it's type FQN.
container.registerInto(Paper.Quality.medium);
container.register("Book title", "book.title");

container.componentScan!Color; // scan struct component and save it in container.
container.componentScan!Size;
container.componentScan!Paper; // scan object component and save it in container. Use parameters container to fetch it's dependencies
container.componentScan!(Paper, HardenedPaper); // scan object and save it by Paper's type FQN. Use parameters container to fetch it's dependencies
container.componentScan!(Page, Cover, Book, Bookshelf); // scan several aggregates and save them in container. An object prefixed with one of it's interfaces will be registered by it in container.

container.instantiate;

assert(container.locate!Bookshelf !is null); // Check if instantiated
assert(container.locate!Color == Color(200, 200, 200)); // Checking if Color was properly set.
assert(container.locate!Size == Size(295, 210));
assert(container.locate!Paper("page.paper").quality == container.locate!(Paper.Quality)); // Checking if quality of page.paper is same as for Paper.Quality in container 
assert(container.locate!Cover.material is container.locate!Paper); // Checking if cover material is from a hardened paper.

Example

Example on how to register entire module.

ApplicationContainer container = new ApplicationContainer;

container.register(Color(255, 255, 255), "color.white"); //Save a struct in storage of values, identified by color.white.
container.register("Aedi tutorial.", "book.title"); //Same as with Color struct.
container.registerInto("A value saved by it's type FQN"); //Save a value in storage, identified by it's type FQN.
container.registerInto(Paper.Quality.medium);
container.register("Book title", "book.title");

container.componentScan!(aermicioi.aedi.test.example); // Scan entire module for components.

container.instantiate;

assert(container.locate!Bookshelf !is null); // Check if instantiated
assert(container.locate!Color == Color(200, 200, 200)); // Checking if Color was properly set.
assert(container.locate!Size == Size(295, 210));
assert(container.locate!Paper("page.paper").quality == container.locate!(Paper.Quality)); // Checking if quality of page.paper is same as for Paper.Quality in container 
assert(container.locate!Cover.material is container.locate!Paper); // Checking if cover material is from a hardened paper.