My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SettingUpConcreteTypes  
Updated Jul 6, 2012 by nfauve...@gmail.com

Getting your container ready

Components are set up in a ContainerBuilder.

ContainerBuilder builder;

Consider these classes:

Basics

Registering an instance

auto driver = std::make_shared< Driver >();
builder.registerInstance(driver);

This instance will be shared across any other components that will need to resolve a Driver.

N.B.: once you registered an instance, you are no longer responsible for its destruction since it will be shared in your application --that is, managed by your IoC container--. Deleting a registered instance will likely cause flaws in your application (an immediate segfault if you're lucky).

Registering a type

builder.registerType< Driver >();

This is narrowly equivalent to the expression builder.registerType< Driver >(CREATE(new Driver));.

Registering a lambda expression type builder

builder.registerType< Driver >(
    [](IComponentContext&) -> Driver*
    {
        return new Driver;
    }));

this can be folded by using the Hypodermic macro CREATE defined in Helpers.h:

builder.registerType< Driver >(CREATE(new Driver));

Note: to avoid any conflicts, Hypodermic's macros can be disabled by defining HYPODERMIC_NO_HELPERS.

Intermediate

Setting up a concrete type as interfaces

The as() method can be used and chain to allow polymorphic resolution.

builder.registerType< Driver >()->as< IDriver >()->as< IRobot >();

Expressing dependencies through setup method

Using a lambda expression to chain dependencies resolution:

builder.registerType< Car >(
    [](IComponentContext& c) -> Car*
    {
        return new Car(c.resolve< IDriver >());
    }));

or a helper to make it fit in a single line:

builder.registerType< Car >(CREATE(new Car(INJECT(IDriver))));

Type autowiring

Hypodermic offers API developers the ability to describe their types' constructors.

class Car : public ICar
{
public:
    typedef AutowiredConstructor< Car(IDriver*) > AutowiredSignature;

    Car(std::shared_ptr< IDriver > driver);
}

The type Car::AutowiredSignature will be used when you register Car like so:

builder.autowireType< Car >()->as< ICar >();
builder.registerType< Driver >()-as< IDriver >();

...

auto car = container->resolve< ICar >();

It actually is a little bit intrusive but this spares you the cost of writing every single dependency of the type you are registering.

Tweaking sharing mode

By default, Hypodermic will not share registered types --i.e. lifetime is transient--. To prevent Hypodermic from creating a new instance from a peculiar type each time it gets resolved you have to invoke singleInstance():

builder.registerType< Driver >()->as< IDriver >()->singleInstance();

or

builder.registerType< Car >(CREATE(new Car(INJECT(IDriver))))->as< ICar >()->singleInstance();

That means every time Hypodermic will need IDriver or ICar, it will create them only once and use a cached value for the next calls to resolve.

Comment by Davidgui...@gmail.com, Apr 11, 2014

beauty !!!


Sign in to add a comment
Powered by Google Project Hosting