moved SmallInjector to new Project.

This commit is contained in:
2019-07-20 22:58:53 +02:00
parent e4ce2a472a
commit 8954cd82fc
9 changed files with 84 additions and 50 deletions

View 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; }
}
}
}

View 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>();
}
}

View 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>