moved SmallInjector to new Project.
This commit is contained in:
58
SmallInjector/Container.cs
Normal file
58
SmallInjector/Container.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SmallInjector
|
||||
{
|
||||
/// <summary>
|
||||
/// A small dependency injector to demonstrate the pattern.
|
||||
/// </summary>
|
||||
public class Container : IContainer
|
||||
{
|
||||
private readonly Dictionary<Type, RegisteredType> _container = new Dictionary<Type, RegisteredType>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
||||
{
|
||||
_container.Add(typeof(TInterface), new RegisteredType(typeof(TService), isSingleton, null));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterType<TService>(bool isSingleton, TService instance = default) => RegisterType<TService, TService>(isSingleton, instance);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TService Resolve<TService>() => (TService)Resolve(typeof(TService));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object Resolve(Type serviceInterface)
|
||||
{
|
||||
if (!_container.TryGetValue(serviceInterface, out var registeredType))
|
||||
throw new Exception("Can't resolve dependency " + serviceInterface);
|
||||
|
||||
if (registeredType.IsSingleton && registeredType.Instance != null)
|
||||
return registeredType.Instance;
|
||||
var constructor = registeredType.ServiceType.GetConstructors()[0];
|
||||
var parameters = constructor.GetParameters();
|
||||
var constructorParameters = new object[parameters.Length];
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||
}
|
||||
var instance = constructor.Invoke(constructorParameters);
|
||||
return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||
}
|
||||
|
||||
private class RegisteredType
|
||||
{
|
||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||
{
|
||||
ServiceType = serviceType;
|
||||
IsSingleton = isSingleton;
|
||||
Instance = instance;
|
||||
}
|
||||
|
||||
public readonly bool IsSingleton;
|
||||
public readonly Type ServiceType;
|
||||
public object Instance { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
34
SmallInjector/IContainer.cs
Normal file
34
SmallInjector/IContainer.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace SmallInjector
|
||||
{
|
||||
/// <summary>
|
||||
/// DI Container
|
||||
/// </summary>
|
||||
public interface IContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Register types in the resolve container.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">Type of the service.</typeparam>
|
||||
/// <typeparam name="TInterface">Type of the interface of the service.</typeparam>
|
||||
/// <param name="isSingleton">True if the service should be singleton. False otherwise.</param>
|
||||
/// <param name="instance">instance of the service</param>
|
||||
void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface;
|
||||
|
||||
/// <summary>
|
||||
/// Register types in the resolve container.
|
||||
/// </summary>
|
||||
void RegisterType<TService>(bool isSingleton, TService instance = default);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
object Resolve(Type serviceInterface);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
TService Resolve<TService>();
|
||||
}
|
||||
}
|
15
SmallInjector/SmallInjector.csproj
Normal file
15
SmallInjector/SmallInjector.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DocumentationFile>C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DocumentationFile>C:\Users\Holger\Desktop\SmallInjectorDemo\SmallInjector\SmallInjector.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user