Skip to content
Yohan edited this page May 11, 2017 · 4 revisions

Say you have a component on which you want to call initialize() once it is created. Hypodermic allows you to write this code during the registration:

ContainerBuilder builder;

builder.registerType< HostInitializer >()
       .as< IHostInitializer >()
       .onActivated([](ComponentContext&,
                       const std::shared_ptr< HostInitializer >& instance)
        {
            instance->initialize();
        });

auto container = builder.build();

auto initializer = container->resolve< IHostInitializer >();

When the variable initializer will be assigned a fresh instance of IHostInitializer, initialize() will have already been invoked.

You notivced the arguments of the lambda? You will be invoked with a ComponentContext which is basically a Container, and an instance of the type of the registration.

Note: on singleInstance registrations, onActivated is called only once, i.e. the first time it is resolved. On the contrary, the hooks you provide on transient instances (the default registration lifetime) will be called everytime the registrations are resolved.

Read more about lifetimes.

Clone this wiki locally