-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a1e584
commit 82c357c
Showing
17 changed files
with
1,159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31911.196 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApacheTech.Common.DependencyInjection", "ApacheTech.Common.DependencyInjection\ApacheTech.Common.DependencyInjection.csproj", "{545558F3-2E21-42BA-8BD8-DCB850F29D88}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Package|Any CPU = Package|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Package|Any CPU.ActiveCfg = Debug|Any CPU | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Package|Any CPU.Build.0 = Debug|Any CPU | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{545558F3-2E21-42BA-8BD8-DCB850F29D88}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {B65743EB-7EEA-4B68-BCBF-E6C8B0D24DBE} | ||
EndGlobalSection | ||
EndGlobal |
81 changes: 81 additions & 0 deletions
81
ApacheTech.Common.DependencyInjection/Abstractions/IServiceCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
|
||
// ReSharper disable UnusedMemberInSuper.Global | ||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace ApacheTech.Common.Extensions.Abstractions | ||
{ | ||
/// <summary> | ||
/// An IOC Container, which holds references to registered types of services, and their instances. | ||
/// </summary> | ||
public interface IServiceCollection | ||
{ | ||
/// <summary> | ||
/// Registers a raw service descriptor, pre-populated with meta-data for the service. | ||
/// </summary> | ||
/// <param name="descriptor">The pre-populated descriptor for the service to register.</param> | ||
/// <seealso cref="ServiceDescriptor"/> | ||
/// | ||
void Register(ServiceDescriptor descriptor); | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. Only one instance of the service will be created within the container. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <seealso cref="ServiceLifetime.Singleton"/> | ||
/// | ||
void RegisterSingleton<TService>() where TService : class; | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. Only one instance of the service will be created within the container. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <typeparam name="TImplementation">The type of implementation to use.</typeparam> | ||
/// <seealso cref="ServiceLifetime.Singleton"/> | ||
void RegisterSingleton<TService, TImplementation>() where TImplementation : TService; | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. Only one instance of the service will be created within the container. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <param name="implementationFactory">The factory that creates the service.</param> | ||
/// <seealso cref="ServiceLifetime.Singleton"/> | ||
void RegisterSingleton<TService>(Func<IServiceResolver, TService> implementationFactory) where TService : class; | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. Only one instance of the service will be created within the container. | ||
/// </summary> | ||
/// <typeparam name="TService">The instance to register.</typeparam> | ||
/// <seealso cref="ServiceLifetime.Singleton"/> | ||
void RegisterSingleton<TService>(TService implementation); | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. A new instance of the service will be created each time it is resolved. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <seealso cref="ServiceLifetime.Transient"/> | ||
void RegisterTransient<TService>() where TService : class; | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. A new instance of the service will be created each time it is resolved. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <typeparam name="TImplementation">The type of implementation to use.</typeparam> | ||
/// <seealso cref="ServiceLifetime.Transient"/> | ||
void RegisterTransient<TService, TImplementation>() where TImplementation : TService; | ||
|
||
/// <summary> | ||
/// Registers a service as a singleton. A new instance of the service will be created each time it is resolved. | ||
/// </summary> | ||
/// <typeparam name="TService">The type of service to register.</typeparam> | ||
/// <param name="implementationFactory">The factory that creates the service.</param> | ||
/// <seealso cref="ServiceLifetime.Transient"/> | ||
void RegisterTransient<TService>(Func<IServiceResolver, TService> implementationFactory) where TService : class; | ||
|
||
/// <summary> | ||
/// Build a service resolver, to access services within this collection. | ||
/// </summary> | ||
/// <returns>An IOC Service Resolver.</returns> | ||
IServiceResolver Build(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
ApacheTech.Common.DependencyInjection/Abstractions/IServiceResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
|
||
// ReSharper disable UnusedMemberInSuper.Global | ||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace ApacheTech.Common.Extensions.Abstractions | ||
{ | ||
/// <summary> | ||
/// Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects. | ||
/// </summary> | ||
public interface IServiceResolver : IDisposable | ||
{ | ||
/// <summary> | ||
/// Gets the service object of the specified type. | ||
/// </summary> | ||
/// <param name="serviceType">An object that specifies the type of service object to get.</param> | ||
/// <returns>A service object of type <paramref name="serviceType" />. | ||
/// | ||
/// -or- | ||
/// | ||
/// <see langword="null" /> if there is no service object of type <paramref name="serviceType" />.</returns> | ||
object GetService(Type serviceType); | ||
|
||
/// <summary> | ||
/// Creates an object of a specified type, using the IOC Container to resolve dependencies. | ||
/// </summary> | ||
/// <param name="serviceType">An object that specifies the type of service object to get.</param> | ||
/// <param name="args">An optional list of arguments, sent the the constructor of the instantiated class.</param> | ||
/// <returns>A service object of type <paramref name="serviceType" />. | ||
/// | ||
/// -or- | ||
/// | ||
/// <see langword="null" /> if no object of type <paramref name="serviceType" /> can be instantiated from the service collection.</returns> | ||
object CreateInstance(Type serviceType, params object[] args); | ||
|
||
/// <summary> | ||
/// Creates an object of a specified type, using the IOC Container to resolve dependencies. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object to create.</typeparam> | ||
/// <param name="args">An optional list of arguments, sent the the constructor of the instantiated class.</param> | ||
/// <returns>An object of type <typeparamref name="T" />. | ||
/// | ||
/// -or- | ||
/// | ||
/// <see langword="null" /> if there is no object of type <typeparamref name="T" /> can be instantiated from the service collection.</returns> | ||
T CreateInstance<T>(params object[] args) where T : class; | ||
|
||
/// <summary> | ||
/// Gets the service object of the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type of service object to get.</typeparam> | ||
/// <returns>A service object of type <typeparamref name="T" />. | ||
/// | ||
/// -or- | ||
/// | ||
/// <see langword="null" /> if there is no service object of type <typeparamref name="T" />.</returns> | ||
T Resolve<T>(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
ApacheTech.Common.DependencyInjection/Abstractions/ObjectFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace ApacheTech.Common.Extensions.Abstractions | ||
{ | ||
/// <summary> | ||
/// The result of <see cref="ActivatorUtilities.CreateFactory(Type, Type[])"/>. | ||
/// </summary> | ||
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> to get service arguments from.</param> | ||
/// <param name="arguments">Additional constructor arguments.</param> | ||
/// <returns>The instantiated type.</returns> | ||
public delegate object ObjectFactory(IServiceProvider serviceProvider, object[] arguments); | ||
} |
87 changes: 87 additions & 0 deletions
87
ApacheTech.Common.DependencyInjection/Abstractions/ServiceDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
|
||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace ApacheTech.Common.Extensions.Abstractions | ||
{ | ||
/// <summary> | ||
/// Describes a service with its service type, implementation, and lifetime. | ||
/// </summary> | ||
public class ServiceDescriptor | ||
{ | ||
/// <summary> | ||
/// Gets the type of the service. | ||
/// </summary> | ||
/// <value>The <see cref="Type"/> of the service.</value> | ||
public Type ServiceType { get; } | ||
|
||
/// <summary> | ||
/// Gets the type of the implementation. | ||
/// </summary> | ||
/// <value>The <see cref="Type"/> of the implementation of the service.</value> | ||
public Type ImplementationType { get; } | ||
|
||
/// <summary> | ||
/// Gets the concrete implementation, which gets returned to the user. | ||
/// </summary> | ||
/// <value>The implementation of the service.</value> | ||
public object Implementation { get; internal set; } | ||
|
||
/// <summary> | ||
/// Specifies the lifetime of a service in an <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <value>The <see cref="ServiceLifetime"/> of the service.</value> | ||
public ServiceLifetime Lifetime { get; } | ||
|
||
/// <summary> | ||
/// Initialises a new instance of the <see cref="ServiceDescriptor"/> class. | ||
/// </summary> | ||
/// <param name="implementation">The concrete implementation, which gets returned to the user.</param> | ||
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param> | ||
public ServiceDescriptor(object implementation, ServiceLifetime lifetime) | ||
{ | ||
ServiceType = implementation.GetType(); | ||
Implementation = implementation; | ||
ImplementationType = implementation.GetType(); | ||
Lifetime = lifetime; | ||
} | ||
|
||
/// <summary> | ||
/// Initialises a new instance of the <see cref="ServiceDescriptor"/> class. | ||
/// </summary> | ||
/// <param name="serviceType">The <see cref="Type"/> of the service.</param> | ||
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param> | ||
public ServiceDescriptor(Type serviceType, ServiceLifetime lifetime) | ||
{ | ||
ServiceType = serviceType; | ||
Lifetime = lifetime; | ||
} | ||
|
||
/// <summary> | ||
/// Initialises a new instance of <see cref="ServiceDescriptor"/> with the specified <paramref name="implementationType"/>. | ||
/// </summary> | ||
/// <param name="serviceType">The <see cref="Type"/> of the service.</param> | ||
/// <param name="implementationType">The <see cref="Type"/> implementing the service.</param> | ||
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param> | ||
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime) | ||
{ | ||
ServiceType = serviceType; | ||
Lifetime = lifetime; | ||
ImplementationType = implementationType; | ||
} | ||
|
||
/// <summary> | ||
/// Initialises a new instance of the <see cref="ServiceDescriptor"/> class. | ||
/// </summary> | ||
/// <param name="serviceType">The <see cref="Type"/> of the service.</param> | ||
/// <param name="implementation">The concrete implementation, which gets returned to the user.</param> | ||
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param> | ||
public ServiceDescriptor(Type serviceType, object implementation, ServiceLifetime lifetime) | ||
{ | ||
ServiceType = serviceType; | ||
Lifetime = lifetime; | ||
ImplementationType = implementation.GetType(); | ||
Implementation = implementation; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
ApacheTech.Common.DependencyInjection/Abstractions/ServiceLifetime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace ApacheTech.Common.Extensions.Abstractions | ||
{ | ||
/// <summary> | ||
/// Specifies the lifetime of a service in an <see cref="IServiceCollection" />. | ||
/// </summary> | ||
public enum ServiceLifetime | ||
{ | ||
/// <summary> | ||
/// Specifies that a single instance of the service will be created. | ||
/// </summary> | ||
Singleton, | ||
|
||
/// <summary> | ||
/// Specifies that a new instance of the service will be created every time it is requested. | ||
/// </summary> | ||
Transient, | ||
} | ||
} |
Oops, something went wrong.