ServiceProvider#
-
class ServiceProvider#
Public Types
-
using Ptr = std::unique_ptr<ServiceProvider>#
Public Functions
-
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#
-
const ServiceProviderOptions &getOptions() const#
Get service provider options.
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); auto& usedOptions = provider.getOptions();
-
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
-
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
-
const IServiceInstanceProvider &getInstanceProvider() const#
Returns inner service instance provider.
If service instance provider is nullptr, method throws exception
- Throws:
sb::di::NullPointerException –
-
IServiceInstanceProvider &getInstanceProvider()#
Returns inner service instance provider.
If service instance provider is nullptr, method throws exception
- Throws:
sb::di::NullPointerException –
-
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 &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 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::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> 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 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 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>();
-
using Ptr = std::unique_ptr<ServiceProvider>#