DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ServiceDescriptor.h
Go to the documentation of this file.
1#pragma once
2
3#include "System/Object.h"
7#include <typeindex>
8#include <functional>
9#include <utility>
10
11namespace DotNetDupe {
12 namespace Extensions {
13 namespace DependencyInjection {
20 public:
22 using FactoryType = std::function<DotNetDupe::System::SmartPointer<DotNetDupe::System::Object>(const DotNetDupe::System::SmartPointer<DotNetDupe::System::IServiceProvider>&)>;
23
26 : m_serviceType(typeid(void)),
27 m_implementationType(typeid(void)),
28 m_eLifetime(ServiceLifetime::Transient),
29 m_fnFactory(nullptr),
30 m_pInstance(nullptr) {}
31
37 const std::type_index& serviceType,
38 const std::type_index& implementationType,
39 ServiceLifetime eLifetime
40 ) : m_serviceType(serviceType),
41 m_implementationType(implementationType),
42 m_eLifetime(eLifetime),
43 m_fnFactory(nullptr),
44 m_pInstance(nullptr) {}
45
51 const std::type_index& serviceType,
52 FactoryType fnFactory,
53 ServiceLifetime eLifetime
54 ) : m_serviceType(serviceType),
55 m_implementationType(serviceType),
56 m_eLifetime(eLifetime),
57 m_fnFactory(std::move(fnFactory)),
58 m_pInstance(nullptr) {}
59
64 const std::type_index& serviceType,
66 ) : m_serviceType(serviceType),
67 m_implementationType(serviceType),
68 m_eLifetime(ServiceLifetime::Singleton),
69 m_fnFactory(nullptr),
70 m_pInstance(std::move(pInstance)) {}
71
74 std::type_index GetServiceType() const { return m_serviceType; }
75
78 std::type_index GetImplementationType() const { return m_implementationType; }
79
82 ServiceLifetime GetLifetime() const { return m_eLifetime; }
83
86 const FactoryType& GetFactory() const { return m_fnFactory; }
87
91
98 const std::type_index& serviceType,
99 const std::type_index& implementationType,
100 ServiceLifetime eLifetime
101 ) {
102 return ServiceDescriptor(serviceType, implementationType, eLifetime);
103 }
104
111 const std::type_index& serviceType,
112 FactoryType fnFactory,
113 ServiceLifetime eLifetime
114 ) {
115 return ServiceDescriptor(serviceType, std::move(fnFactory), eLifetime);
116 }
117
124 template <typename TService, typename TImplementation, typename... TArgs>
134
135 private:
136 std::type_index m_serviceType;
137 std::type_index m_implementationType;
138 ServiceLifetime m_eLifetime;
139 FactoryType m_fnFactory;
141 };
142 }
143 }
144}
145
Defines a mechanism for retrieving a service object; that is, an object that provides custom support ...
Base object class for DotNetDupe mirroring .NET System.Object.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
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.
ServiceDescriptor(const std::type_index &serviceType, FactoryType fnFactory, ServiceLifetime eLifetime)
Initializes a descriptor with a factory delegate.
static ServiceDescriptor Describe(const std::type_index &serviceType, FactoryType fnFactory, ServiceLifetime eLifetime)
Creates a ServiceDescriptor using a factory function delegate.
ServiceDescriptor(const std::type_index &serviceType, const std::type_index &implementationType, ServiceLifetime eLifetime)
Initializes a descriptor with explicit service type, implementation type, and lifetime.
std::function< DotNetDupe::System::SmartPointer< DotNetDupe::System::Object >(const DotNetDupe::System::SmartPointer< DotNetDupe::System::IServiceProvider > &)> FactoryType
Function signature for dynamic factory service instantiation delegates.
ServiceDescriptor(const std::type_index &serviceType, DotNetDupe::System::SmartPointer< DotNetDupe::System::Object > pInstance)
Initializes a descriptor with a pre-existing singleton instance.
ServiceDescriptor()
Default constructor initializing an empty descriptor.
static ServiceDescriptor Describe(ServiceLifetime eLifetime)
Creates a ServiceDescriptor resolving constructor dependencies via IServiceProvider.
static ServiceDescriptor Describe(const std::type_index &serviceType, const std::type_index &implementationType, ServiceLifetime eLifetime)
Creates a ServiceDescriptor with the specified service type, implementation type, and lifetime.
std::type_index GetImplementationType() const
Gets the concrete implementation type for this service.
ServiceLifetime GetLifetime() const
Gets the lifetime scope configured for this service.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
ServiceLifetime
Specifies the lifetime of a service in an IServiceCollection.
@ Singleton
Specifies that a single instance of the service will be created.
@ Transient
Specifies that a new instance of the service will be created every time it is requested.