-
Notifications
You must be signed in to change notification settings - Fork 82
Registering components
ContainerBuilder builder;
builder.registerType< MessageDispatcher >();ContainerBuilder builder;
auto configuration = std::make_shared< DispatcherConfiguration >();
builder.registerInstance(configuration);ContainerBuilder builder;
builder.registerInstanceFactory([](ComponentContext&)
{
return std::make_shared< ThreadPool >(std::thread::hardware_concurrency());
});When registering types, instances, or instance factories, you may want the container to know them by their abstraction(s).
ContainerBuilder builder;
builder.registerType< MessageDispatcher >().as< IMessageDispatcher >();The container will give an instance of MessageDispatcher when it is asked to resolve a IMessageDispatcher.
Note: it will no longer be able to resolve MessageDispatcher when you use as.
ContainerBuilder builder;
builder.registerType< MessageDispatcher >()
.named< IMessageDispatcher >("LeDispatcher");
auto leDispatcher = container->resolveNamed< IMessageDispatcher >("LeDispatcher");The container looks up for an implementation of IMessageDispatcher named "LeDispatcher".
Note: it won't be able to resolve IMessageDispatcher if you don't provide the name you gave at the same time.
ContainerBuilder builder;
builder.registerType< MessageDispatcher >().named("LeDispatcher");
auto leDispatcher = container->resolveNamed< MessageDispatcher >("LeDispatcher");Using named without passing a type does two things: letting the container know about the MessageDispatcher implementation named "LeDispatcher" and registering the type as itself. It is basically like calling asSelf.
ContainerBuilder builder;
builder.registerType< MessageDispatcher >().as< IMessageDispatcher >().asSelf();This way, you explicitly tell the container that both MessageDispatcher and IMessageDispatcher can be resolvable.
You can provide a hook when instances are activated by the container. Read the doc here.
You can configure Hypodermic to expand or narrow the lifetime of your registrations. Read this section of the wiki to know how to do it.
Read this section to learn how you can free yourself from registering default components in a particular order.