Stormancer::RegistrationHandle< T > Class Template Reference

The RegistrationHandle allows configuring a dependency added via ContainerBuilder::registerDependency(). More...

#include <DependencyInjection.h>

Public Member Functions

template<typename TAs >
RegistrationHandleas ()
 Register this dependency as a given type. More...
 
RegistrationHandleasSelf ()
 Register the dependency as its own concrete type. More...
 
template<typename TAs >
RegistrationHandlenamed (std::string name)
 Register this dependency as a given type, with a given name. More...
 
RegistrationHandleinstancePerRequest ()
 Set this dependency to have transient instances. More...
 
RegistrationHandleinstancePerScope ()
 Set this dependency to have an instance per dependency scope. More...
 
RegistrationHandlesingleInstance ()
 Set this dependency to have only one instance. More...
 
RegistrationHandleinstancePerMatchingScope (std::string scopeTag)
 Set this dependency to use an instance per matching dependency scope. More...
 

Friends

class ContainerBuilder
 

Detailed Description

template<typename T>
class Stormancer::RegistrationHandle< T >

The RegistrationHandle allows configuring a dependency added via ContainerBuilder::registerDependency().

This object is not intended to be stored; instead, it should be seen as a handle to tweak a registration's settings.

ContainerBuilder builder;
builder.registerDependency(fooFactory).as<Foo>().as<Bar>().singleInstance();

Member Function Documentation

◆ as()

template<typename T >
template<typename TAs >
RegistrationHandle& Stormancer::RegistrationHandle< T >::as ( )
inline

Register this dependency as a given type.

You can call this method with as many types as you like, as long as the actual type of the dependency can be converted to TAs* (this is checked statically, and generates a compile error if it cannot). Dependencies are registered as the type given to ContainerBuilder::registerDependency() by default. This method allows to override this behavior, and add more types to register the dependency as.

Template Parameters
TAsType to register this dependency as.
Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

◆ asSelf()

template<typename T >
RegistrationHandle& Stormancer::RegistrationHandle< T >::asSelf ( )
inline

Register the dependency as its own concrete type.

This is a shortcut for as<T>().

Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

◆ instancePerMatchingScope()

template<typename T >
RegistrationHandle& Stormancer::RegistrationHandle< T >::instancePerMatchingScope ( std::string  scopeTag)
inline

Set this dependency to use an instance per matching dependency scope.

When you resolve this dependency under a given DependencyScope, the resolver will look for the closest dependency scope with a tag that is equal to scopeTag, starting from the scope it was requested from and up the parent chain. When such a scope is found, the dependency will be instantiated for this scope if it wasn't already.

Exceptions
std::invalid_argumentIf scopeTag is empty
Parameters
scopeTagThe dependency scope tag that this dependency will be bound to.
Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

◆ instancePerRequest()

template<typename T >
RegistrationHandle& Stormancer::RegistrationHandle< T >::instancePerRequest ( )
inline

Set this dependency to have transient instances.

An instance of the dependency will be created each time the dependency is resolved, and it will not be stored.

Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

◆ instancePerScope()

template<typename T >
RegistrationHandle& Stormancer::RegistrationHandle< T >::instancePerScope ( )
inline

Set this dependency to have an instance per dependency scope.

An instance of the dependency will be created once for each DependencyScope that the dependency is resolved from. The lifetime of these instances will be bound to their owning scope's.

Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

◆ named()

template<typename T >
template<typename TAs >
RegistrationHandle& Stormancer::RegistrationHandle< T >::named ( std::string  name)
inline

Register this dependency as a given type, with a given name.

You can call this method with as many types as you like, as long as the actual type of the dependency can be converted to TAs* (this is checked statically, and generates a compile error if it cannot). Dependencies are registered as the type given to ContainerBuilder::registerDependency() by default. This method allows to override this behavior, and add more types to register the dependency as, in addition to specifying a name to identify this dependency for TAs.

Template Parameters
TAsType to register this dependency as.
Parameters
nameName of the dependency for the type TAs.
Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

Registering a dependency of type MyDep as type IMyDep with name "mycustomname", and resolving this dependency.

ContainerBuilder builder;
builder.registerDependency<MyDep>().named<IMyDep>("mycustomname");
auto scope = builder.build();
auto dep = scope.resolveNamed<IMyDep>("mycustomname");

◆ singleInstance()

template<typename T >
RegistrationHandle& Stormancer::RegistrationHandle< T >::singleInstance ( )
inline

Set this dependency to have only one instance.

An instance of the dependency will be created when the dependency is first resolved, in the scope where it was registered. All subsequent requests for this dependency in any child dependency scope will return this same instance. The instance's lifetime is tied to the scope where the corresponding dependency was registered.

Returns
A reference to the same handle, to enable chaining calls in a "fluent API" fashion.

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