IServiceProvider#
-
struct IServiceProvider#
Subclassed by sb::di::details::DefaultServiceProvider
Public Types
-
using Ptr = std::unique_ptr<IServiceProvider>#
Public Functions
-
virtual IServiceProvider::Ptr createScope() = 0#
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>() != &provider->getService<TestClass>(); // True
-
virtual const IServiceInstance *tryGetInstance(TypeId serviceTypeId) = 0#
Returns service instance pointner, might be null.
If service was not registered or was registered as transient, method returns null
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); IServiceInstance * instance = provider->tryGetInstance(typeid(TestClass));
- Attention
It is advised to use tryGetService<T> method istead
-
template<class TService>
inline TService *tryGetService()# Returns service pointner, 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>();
-
virtual const IServiceInstance &getInstance(TypeId serviceTypeId) = 0#
Returns service instance reference, might throw exception.
If service was not registered or was registered as transient, method throws exception
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); const IServiceInstance & instance = provider->getInstance(typeid(TestClass));
- Attention
It is advised to use getService<T> method istead
- Throws:
ServiceNotFoundException – service was not found
-
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
Example:
auto provider = ServiceCollection{}.addScoped<TestClass>().buildServiceProvider(); TestClass & service = provider->getService<TestClass>();
- Throws:
ServiceNotFoundException – service was not found
-
virtual std::vector<const IServiceInstance*> getInstances(TypeId serviceTypeId) = 0#
Returns service instances.
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<const IServiceInstance *> instances = provider->getInstances(typeid(ITestClass));
- Attention
It is advised to use getServices<T> method istead
-
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<TService *> services = provider->getServices<ITestClass>();
-
virtual std::unique_ptr<IServiceInstance> tryCreateInstance(TypeId serviceTypeId) = 0#
Creates service instance unique pointner, might be null.
If service was not registered or was registered as scoped/transient, method returns null
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); std::unique_ptr<IServiceInstance> instance = provider->tryCreateInstance(typeid(TestClass));
- Attention
It is advised to use tryCreateService<T> method istead
-
template<class TService>
inline std::unique_ptr<TService> tryCreateService()# Creates service unique pointner, might be null.
If service was not registered or was registered as scoped/transient, method returns null
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); std::unique_ptr<TestClass> service = provider->tryCreateService<TestClass>();
-
virtual std::unique_ptr<IServiceInstance> createInstance(TypeId serviceTypeId) = 0#
Creates service instance unique pointner, might throw exception.
If service was not registered or was registered as scoped/transient, method throws exception
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); std::unique_ptr<IServiceInstance> instance = provider->createInstance(typeid(TestClass));
- Attention
It is advised to use createService<T> method istead
- Throws:
ServiceNotFoundException – service was not found
-
template<class TService>
inline std::unique_ptr<TService> createService()# Creates service unique pointner, might throw exception.
If service was not registered or was registered as scoped/transient, method throws exception
Example:
auto provider = ServiceCollection{}.addTransient<TestClass>().buildServiceProvider(); std::unique_ptr<TestClass> service = provider->createService<TestClass>();
- Throws:
ServiceNotFoundException – service was not found
-
virtual std::vector<std::unique_ptr<IServiceInstance>> createInstances(TypeId serviceTypeId) = 0#
Creates service instances.
If service was not registered or was registered as scoped/transient, method returns empty vector
Example:
auto provider = ServiceCollection{} .addTransient<ITestClass, TestClass1>() .addTransient<ITestClass, TestClass2>() .buildServiceProvider(); std::vector<std::unique_ptr<IServiceInstance>> instances = provider->createInstances(typeid(ITestClass));
- Attention
It is advised to use createServices<T> method istead
-
template<class TService>
inline std::vector<std::unique_ptr<TService>> createServices()# Creates services.
If service was not registered or was registered as scoped/transient, method returns empty vector
Example:
auto provider = ServiceCollection{} .addTransient<ITestClass, TestClass1>() .addTransient<ITestClass, TestClass2>() .buildServiceProvider(); d::vector<std::unique_ptr<ITestClass>> services = provider->createServices<ITestClass>();
-
virtual ~IServiceProvider() = default#
-
using Ptr = std::unique_ptr<IServiceProvider>#