Stormancer::ContainerBuilder Class Reference

The ContainerBuilder is the primary element of the dependency injection mechanism. Use it to set up the dependencies that you want to make available to consumer components. More...

#include <DependencyInjection.h>

Classes

struct  All
 A "type tag" struct to be used as a type argument to registerDependency() when denoting a dependency on multiple instances of T. More...
 

Public Member Functions

template<typename T >
RegistrationHandle< T > registerDependency (std::function< std::shared_ptr< T >(const DependencyScope &)> factory)
 Register a dependency into the container. More...
 
template<typename T >
RegistrationHandle< T > registerDependency (std::shared_ptr< T > instance)
 Register an existing instance of type T as a dependency. More...
 
template<typename T , typename... TCtorArgs>
RegistrationHandle< T > registerDependency ()
 Register a dependency of type T with an automatically generated factory. More...
 
DependencyScope build ()
 Build a DependencyScope from this container. More...
 

Friends

class DependencyScopeImpl
 

Detailed Description

The ContainerBuilder is the primary element of the dependency injection mechanism. Use it to set up the dependencies that you want to make available to consumer components.

Objects of this class are not thread-safe.

ContainerBuilder builder;
builder.registerDependency<Foo>(fooFactory);
builder.registerDependency<Bar>(barFactory);
DependencyScope rootScope = builder.build();

Member Function Documentation

◆ build()

DependencyScope Stormancer::ContainerBuilder::build ( )

Build a DependencyScope from this container.

Call this method when you are done registering and configuring dependencies.

Returns
A DependencyScope containing the registrations that were added to this ContainerBuilder.

◆ registerDependency() [1/3]

template<typename T , typename... TCtorArgs>
RegistrationHandle< T > Stormancer::ContainerBuilder::registerDependency
inline

Register a dependency of type T with an automatically generated factory.

T must have a public constructor that takes arguments of types TCtorArgs as std::shared_ptrs, in the same order as they are supplied to this method. When T is resolved in a given scope, dependencies of types TCtorArgs will also be resolved from this scope and passed to T's constructor. If T's constructor takes a vector of dependencies of a certain type U (std::vector<std::shared_ptr<U>>), you should pass a ContainerBuilder::All<U> type argument.

Template Parameters
TConcrete type of the dependency to be registered.
TCtorArgsTypes of the arguments for T's constructor.
Returns
A RegistrationHandle that can be used to configure the dependency. See the API documentation for RegistrationHandle for details.

◆ registerDependency() [2/3]

template<typename T >
RegistrationHandle<T> Stormancer::ContainerBuilder::registerDependency ( std::function< std::shared_ptr< T >(const DependencyScope &)>  factory)
inline

Register a dependency into the container.

Template Parameters
TType of the dependency to be registered. This is the concrete, actual type of the dependency. If you want this dependency to be registered as one or more types different from T, use RegistrationHandle::as().
Parameters
factoryFactory function for the dependency. This function will be called when an instance of the dependency needs to be created. It may be called from mutliple threads at the same time. Dependencies are always instantiated lazily; that is, only when code that needs them directly or indirectly through a DependencyScope::resolve() or DependencyScope::resolveAll() call. For details about the lifetime of dependency instances, see RegistrationHandle.
Returns
A RegistrationHandle that can be used to configure the dependency. See the API documentation for RegistrationHandle for details.

◆ registerDependency() [3/3]

template<typename T >
RegistrationHandle<T> Stormancer::ContainerBuilder::registerDependency ( std::shared_ptr< T >  instance)
inline

Register an existing instance of type T as a dependency.

This object will be registered with the "single instance" lifetime. Choosing a different lifetime will have no observable effect.

Template Parameters
TType of the object pointed to by instance.
Parameters
instanceInstance of type T to register.
Returns
A RegistrationHandle that can be used to configure the dependency. See the API documentation for RegistrationHandle for details.

The documentation for this class was generated from the following file: