Stormancer::DependencyScope Class Reference

An object from which dependencies can be retrieved. More...

#include <DependencyInjection.h>

Public Member Functions

 DependencyScope ()=default
 Create an empty DependencyScope. More...
 
 DependencyScope (const DependencyScope &)=delete
 
DependencyScopeoperator= (const DependencyScope &)=delete
 
 DependencyScope (DependencyScope &&)
 
DependencyScopeoperator= (DependencyScope &&)
 
template<typename T >
std::shared_ptr< T > resolve () const
 Retrieve the dependency that was registered for the type T. More...
 
template<typename T >
std::vector< std::shared_ptr< T > > resolveAll () const
 Retrieve all the dependencies that were registered for the type T. More...
 
template<typename T >
std::shared_ptr< T > resolveNamed (const std::string &name) const
 Retrieve the dependency named name that was registered for the type T. More...
 
DependencyScope beginLifetimeScope () const
 Create a child dependency scope. More...
 
DependencyScope beginLifetimeScope (std::function< void(ContainerBuilder &)> builder) const
 Create a child dependency scope. More...
 
DependencyScope beginLifetimeScope (std::string tag) const
 Create a child dependency scope. More...
 
DependencyScope beginLifetimeScope (std::string tag, std::function< void(ContainerBuilder &)> builder) const
 Create a child dependency scope. More...
 
bool isValid () const
 Check if this scope has been fully constructed. More...
 

Friends

class DependencyScopeImpl
 
class ContainerBuilder
 

Detailed Description

An object from which dependencies can be retrieved.

The DependencyScope controls the lifetime of the dependencies that are accessible through it. For more details on dependency lifetime, see RegistrationHandle. Objects of this class are thread-safe. They are not copyable so as to avoid the emergence of circualr dependencies between a DependencyScope and the dependencies which instances live in its scope. If you need to store a copy of a DependencyScope inside an object, what you most likely want is to store a child scope of this scope instead.

ContainerBuilder buider;
builder.registerDependency<Foo>();
DependencyScope scope = builder.build();
std::shared_ptr<Foo> foo = scope.resolve<Foo>();

Constructor & Destructor Documentation

◆ DependencyScope()

Stormancer::DependencyScope::DependencyScope ( )
default

Create an empty DependencyScope.

This scope will be an empty placeholder; a call to isValid() will return false.

Member Function Documentation

◆ beginLifetimeScope() [1/4]

DependencyScope Stormancer::DependencyScope::beginLifetimeScope ( ) const
inline

Create a child dependency scope.

Returns
A new DependencyScope that is a child of this.

◆ beginLifetimeScope() [2/4]

DependencyScope Stormancer::DependencyScope::beginLifetimeScope ( std::function< void(ContainerBuilder &)>  builder) const
inline

Create a child dependency scope.

Parameters
builderFunction in which you can register dependencies for the child scope using the supplied ContainerBuilder argument.
Returns
A new DependencyScope that is a child of this.

◆ beginLifetimeScope() [3/4]

DependencyScope Stormancer::DependencyScope::beginLifetimeScope ( std::string  tag) const
inline

Create a child dependency scope.

Parameters
tagTag of the child scope. This is useful if you use the RegistrationHandle::instancePerMatchingScope() feature.
Returns
A new DependencyScope that is a child of this.

◆ beginLifetimeScope() [4/4]

DependencyScope Stormancer::DependencyScope::beginLifetimeScope ( std::string  tag,
std::function< void(ContainerBuilder &)>  builder 
) const

Create a child dependency scope.

Parameters
tagTag of the child scope. This is useful if you use the RegistrationHandle::instancePerMatchingScope() feature.
builderFunction in which you can register dependencies for the child scope using the supplied ContainerBuilder argument.
Returns
A new DependencyScope that is a child of this.

◆ isValid()

bool Stormancer::DependencyScope::isValid ( ) const

Check if this scope has been fully constructed.

Returns
true if this scope was constructed from a call to ContainerBuilder::build() or beginLifetimeScope(); false if it was default-constructed.

◆ resolve()

template<typename T >
std::shared_ptr<T> Stormancer::DependencyScope::resolve ( ) const
inline

Retrieve the dependency that was registered for the type T.

If needed, the dependency will be instantiated according to its lifetime options. See RegistrationHandle for more details on those.

Exceptions
Stormancer::DependencyResolutionExceptionIf no dependency registration could be found for type T, or if the registration could not be instantiated. The exception's message will contain details about the error.
Returns
A shared_ptr containing the instance of the dependency that was resolved.

◆ resolveAll()

template<typename T >
std::vector<std::shared_ptr<T> > Stormancer::DependencyScope::resolveAll ( ) const
inline

Retrieve all the dependencies that were registered for the type T.

If needed, the dependencies will be instantiated according to their lifetime options. See RegistrationHandle for more details on those.

Returns
A vector containing the instances of the dependencies that were resolved. The vector may be empty if no suitable dependency registration could be found, or if their lifetime requirements could not be satisfied.

◆ resolveNamed()

template<typename T >
std::shared_ptr<T> Stormancer::DependencyScope::resolveNamed ( const std::string &  name) const
inline

Retrieve the dependency named name that was registered for the type T.

If needed, the dependency will be instantiated according to its lifetime options. See RegistrationHandle for more details on those.

See also
RegistrationHandle::named()
Exceptions
Stormancer::DependencyResolutionExceptionIf no dependency registration could be found for type T, or if the registration could not be instantiated. The exception's message will contain details about the error.
Returns
A shared_ptr containing the instance of the dependency that was resolved.

The documentation for this class was generated from the following file:
DependencyScope()=default
Create an empty DependencyScope.