ServiceProvider

class ServiceProvider

Public Types

using Ptr = std::unique_ptr<ServiceProvider>

Public Functions

inline explicit ServiceProvider(IServiceInstanceProvider::Ptr instanceProvider)

Constructs service provider with specified instance provider.

If service instance provider is nullptr, constructor throws exception

Throws:

sb::di::NullPointerException

ServiceProvider(const ServiceProvider&) = delete
ServiceProvider(ServiceProvider&&) = delete
ServiceProvider &operator=(const ServiceProvider&) = delete
ServiceProvider &operator=(ServiceProvider&&) = delete
inline const IServiceInstanceProvider &getInstanceProvider() const

Returns inner service instance provider.

inline IServiceInstanceProvider &getInstanceProvider()

Returns inner service instance provider.

inline ServiceProvider createScope() const

Create a scoped service provider.

Scoped service provider creates/holds its own scoped services

Example:

auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider();
auto scoped = provider.createScope();

&scoped.getService<TestClass>() != &provide.getService<TestClass>(); // True

inline Ptr createScopeAsPtr() const

Create a scoped service provider as unique_ptr.

Scoped service provider creates/holds its own scoped services

Example:

auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProviderAsPtr();
auto scoped = provider->createScopeAsPtr();

&scoped->getService<TestClass>() != &provider->getService<TestClass>(); // True

template<class TService>
inline TService *tryGetService()

Return service pointer might be null.

If the service was not registered or was registered as transient, method returns null

Example:

auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider();

TestClass* service = provider.tryGetService<TestClass>();

template<class TService>
inline TService *tryGetKeyedService(const std::string_view serviceKey)

Return service pointer might be null.

If the service was not registered or was registered as transient, method returns null

Example:

auto provider = ServiceCollection{}.addKeyedScoped<TestClass>("key").buildServiceProvider();

TestClass* service = provider.tryGetKeyedService<TestClass>("key");

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline TService &getService()

Return service reference might throw exception.

If the service was not registered or was registered as transient, method throws exception

Throws:

sb::di::ServiceNotFoundException – Example:

auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider();

TestClass& service = provider.getService<TestClass>();

template<class TService>
inline TService &getKeyedService(const std::string_view serviceKey)

Return service reference might throw exception.

If the service was not registered or was registered as transient, method throws exception

Example:

auto provider = ServiceCollection{}.addKeyedScoped<TestClass>("key").buildServiceProvider();

TestClass& service = provider.getKeyedService<TestClass>("key");

Throws:

sb::di::ServiceNotFoundException

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline std::vector<TService*> getServices()

Returns services.

If the service was not registered or was registered as transient, method returns empty vector

Example:

auto provider = ServiceCollection{}
             .addScoped<ITestClass, TestClass1>()
             .addScoped<ITestClass, TestClass2>()
             .buildServiceProvider();

std::vector<ITestClass *> services = provider.getServices<ITestClass>();

template<class TService>
inline std::vector<TService*> getKeyedServices(const std::string_view serviceKey)

Returns services.

If the service was not registered or was registered as transient, method returns empty vector

Example:

auto provider = ServiceCollection{}
             .addKeyedScoped<ITestClass, TestClass1>("key")
             .addKeyedScoped<ITestClass, TestClass2>("key")
             .buildServiceProvider();

std::vector<ITestClass *> services = provider.getKeyedServices<ITestClass>("key");

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline std::unique_ptr<TService> tryCreateService()

Creates service unique pointer, might be null.

If the service was not registered or was registered as scoped/singleton, method returns null

Example:

auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider();

std::unique_ptr<TestClass> service = provider.tryCreateService<TestClass>();

template<class TService>
inline std::unique_ptr<TService> tryCreateKeyedService(const std::string_view serviceKey)

Creates service unique pointer, might be null.

If the service was not registered or was registered as scoped/singleton, method returns null

Example:

auto provider = ServiceCollection{}.addKeyedTransient<TestClass>("key").buildServiceProvider();

std::unique_ptr<TestClass> service = provider.tryCreateKeyedService<TestClass>("key");

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline std::unique_ptr<TService> createService()

Creates service unique pointer, might throw exception.

If the service was not registered or was registered as scoped/singleton, method throws exception

Throws:

sb::di::ServiceNotFoundException – Example:

auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider();

std::unique_ptr<TestClass> service = provider.createService<TestClass>();

template<class TService>
inline std::unique_ptr<TService> createKeyedService(const std::string_view serviceKey)

Creates service unique pointer, might throw exception.

If the service was not registered or was registered as scoped/singleton, method throws exception

Example:

auto provider = ServiceCollection{}.addKeyedTransient<TestClass>("key").buildServiceProvider();

std::unique_ptr<TestClass> service = provider.createKeyedService<TestClass>("key");

Throws:

sb::di::ServiceNotFoundException

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline TService createServiceInPlace()

Creates service in place, might throw exception.

If the service was not registered or was registered as scoped/singleton, method throws exception

Throws:

sb::di::ServiceNotFoundException – Example:

auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider();

TestClass service = provider.createServiceInPlace<TestClass>();

template<class TService>
inline TService createKeyedServiceInPlace(const std::string_view serviceKey)

Creates service in place, might throw exception.

If the service was not registered or was registered as scoped/singleton, method throws exception

Example:

auto provider = ServiceCollection{}.addKeyedTransient<TestClass>("key").buildServiceProvider();

TestClass service = provider.createKeyedServiceInPlace<TestClass>("key");

Throws:

sb::di::ServiceNotFoundException

Parameters:

serviceKey – service key can be empty to get default service

template<class TService>
inline std::vector<std::unique_ptr<TService>> createServices()

Creates services.

If the service was not registered or was registered as scoped/singleton, method returns empty vector

Example:

auto provider = ServiceCollection{}
             .addTransient<ITestClass, TestClass1>()
             .addTransient<ITestClass, TestClass2>()
             .buildServiceProvider();

std::vector<std::unique_ptr<ITestClass>> services = provider.createServices<ITestClass>();

template<class TService>
inline std::vector<std::unique_ptr<TService>> createKeyedServices(const std::string_view serviceKey)

Creates services.

If the service was not registered or was registered as scoped/singleton, method returns empty vector

Example:

auto provider = ServiceCollection{}
             .addKeyedTransient<ITestClass, TestClass1>("key")
             .addKeyedTransient<ITestClass, TestClass2>("key")
             .buildServiceProvider();

std::vector<std::unique_ptr<ITestClass>> services = provider.createKeyedServices<ITestClass>("key");

Parameters:

serviceKey – service key can be empty to get default service