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.
-
ServiceProvider(const ServiceProvider &parent) = delete#
-
ServiceProvider(ServiceProvider&&) = delete#
-
ServiceProvider &operator=(const ServiceProvider &parent) = delete#
-
ServiceProvider &operator=(ServiceProvider &&parent) = delete#
-
inline const IServiceInstanceProvider &getInstanceProvider() const#
Returns inner service instance provider.
If service instance provider is nullptr, method throws exception
- Throws:
sb::di::NullPointerException –
-
inline IServiceInstanceProvider &getInstanceProvider()#
Returns inner service instance provider.
If service instance provider is nullptr, method throws exception
- Throws:
sb::di::NullPointerException –
-
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()# Returns service pointer, might be null.
If 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)# Returns service pointer, might be null.
If service was not registered or was registered as transient, method returns null
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); TestClass* service = provider.tryGetService<TestClass>();
- Parameters:
serviceKey – service key can be empty to get default service
-
template<class TService>
inline TService &getService()# Returns service reference, might throw exception.
If 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)# Returns service reference, might throw exception.
If service was not registered or was registered as transient, method throws exception
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); TestClass& service = provider.getService<TestClass>();
- 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 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 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>();
- 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 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 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>();
- 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 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 service was not registered or was registered as scoped/singleton, method throws exception
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); std::unique_ptr<TestClass> service = provider.createService<TestClass>();
- 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 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 service was not registered or was registered as scoped/singleton, method throws exception
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); TestClass service = provider.createServiceInPlace<TestClass>();
- 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 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 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>();
- Parameters:
serviceKey – service key can be empty to get default service
-
using Ptr = std::unique_ptr<ServiceProvider>#