4#include <unordered_map>
14 class ServiceProviderProxy :
public DotNetDupe::System::IServiceProvider {
16 ServiceProviderProxy(ServiceProvider* pProvider) : m_pProvider(pProvider) {}
17 ~ServiceProviderProxy()
override =
default;
19 DotNetDupe::System::SmartPointer<DotNetDupe::System::Object> GetService(
const std::type_index& serviceType)
override {
21 return m_pProvider->GetService(serviceType);
25 ServiceProvider* m_pProvider;
48 struct ServiceProvider::Impl {
49 std::unordered_map<std::type_index, ServiceDescriptor> mapDescriptors;
50 std::unordered_map<std::type_index, DotNetDupe::System::SmartPointer<DotNetDupe::System::Object>> pMapSingletons;
51 std::unordered_map<std::type_index, DotNetDupe::System::SmartPointer<DotNetDupe::System::Object>> pMapScoped;
52 std::vector<DotNetDupe::System::SmartPointer<DotNetDupe::System::IO::IDisposable>> pvDisposables;
57 if (pInst.
IsNull())
return;
61 if (!pDisp.IsNull()) {
62 pvDisposables.push_back(pDisp);
71 auto it = pMapSingletons.find(desc.GetServiceType());
72 if (it != pMapSingletons.end())
return it->second;
77 pMapSingletons[desc.GetServiceType()] = pInst;
83 DotNetDupe::System::SmartPointer<DotNetDupe::System::Object> ResolveScoped(
ServiceProvider* pSelf,
const ServiceDescriptor& desc) {
88 auto it = pMapScoped.find(desc.GetServiceType());
89 if (it != pMapScoped.end())
return it->second;
94 pMapScoped[desc.GetServiceType()] = pInst;
100 DotNetDupe::System::SmartPointer<DotNetDupe::System::Object> ResolveTransient(
ServiceProvider* pSelf,
const ServiceDescriptor& desc) {
114 : m_bIsRoot(true), m_pRootProvider(nullptr), m_pImpl(
DotNetDupe::
System::SmartPointer<Impl>::NewShared()) {
116 for (
int iIdx = 0; iIdx < collection.
GetCount(); ++iIdx) {
118 m_pImpl->mapDescriptors.insert({ descriptor.
GetServiceType(), descriptor });
123 : m_bIsRoot(false), m_pRootProvider(pRootProvider), m_pImpl(
DotNetDupe::
System::SmartPointer<Impl>::NewShared()) {
150 auto it = pRoot->m_pImpl->mapDescriptors.find(serviceType);
151 if (it == pRoot->m_pImpl->mapDescriptors.end()) {
156 return ResolveService(it->second);
163 return pRoot->m_pImpl->ResolveSingleton(
this, descriptor);
171 return m_pImpl->ResolveScoped(
this, descriptor);
175 return m_pImpl->ResolveTransient(
this, descriptor);
180 if (m_pImpl.IsNull())
return;
184 for (
auto it = m_pImpl->pvDisposables.rbegin(); it != m_pImpl->pvDisposables.rend(); ++it) {
189 m_pImpl->pvDisposables.clear();
192 m_pImpl->pMapSingletons.clear();
193 m_pImpl->pMapScoped.clear();
199 : m_pProvider(std::move(pProvider)) {
215 if (!m_pProvider.IsNull()) {
216 m_pProvider->Dispose();
217 m_pProvider =
nullptr;
224 : m_pProvider(pProvider) {
Provides an RAII-style scoped lock wrapper around synchronization primitives.
Specifies the contract for a collection of service descriptors.
Defines a factory interface used to create instances of IServiceScope.
Describes a service with its service type, implementation, and lifetime.
std::type_index GetServiceType() const
Gets the service type represented by this descriptor.
const FactoryType & GetFactory() const
Gets the optional factory function delegate.
const DotNetDupe::System::SmartPointer< DotNetDupe::System::Object > & GetInstance() const
Gets the optional pre-existing singleton instance.
ServiceLifetime GetLifetime() const
Gets the lifetime scope configured for this service.
Default IoC service provider for resolving dependencies.
void Dispose() override
Disposes all resolved singleton instances and clears cached references.
ServiceProvider(const IServiceCollection &collection)
Initializes a root ServiceProvider from an IServiceCollection.
~ServiceProvider() override
Destructor releasing resolved services.
ServiceScopeFactory(ServiceProvider *pProvider)
Initializes a ServiceScopeFactory bound to a root provider.
DotNetDupe::System::SmartPointer< IServiceScope > CreateScope() override
Creates a new child IServiceScope.
DotNetDupe::System::SmartPointer< DotNetDupe::System::IServiceProvider > GetServiceProvider() const override
Gets the scoped IServiceProvider.
void Dispose() override
Disposes the scope and all scoped instances created within it.
~ServiceScope() override
Destructor disposing the child scope provider.
ServiceScope(DotNetDupe::System::SmartPointer< ServiceProvider > pProvider)
Initializes a ServiceScope wrapping a scoped ServiceProvider.
int GetCount() const
Gets the number of elements contained in the List.
Provides a mechanism for releasing unmanaged resources.
Defines a mechanism for retrieving a service object; that is, an object that provides custom support ...
SmartPointer< T > GetService()
Gets the service object of the specified generic type T.
The exception that is thrown when a method call is invalid for the object's current state.
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
SmartPointer< U > DynamicCast() const
Dynamically casts the managed pointer to another type U and returns a new SmartPointer sharing owners...
Provides a re-entrant mutual exclusion primitive for thread synchronization.
@ Scoped
Specifies that a new instance of the service will be created for each scope.
@ Singleton
Specifies that a single instance of the service will be created.
static DotNetDupe::System::SmartPointer< DotNetDupe::System::Object > InstantiateService(ServiceProvider *pSelf, const ServiceDescriptor &descriptor)
Lock< CriticalSection > CriticalSectionLock
Convenience alias for Lock<CriticalSection>.