Code cleanup and enforce csharpier
This commit is contained in:
@ -1,98 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
namespace SmallInjector;
|
||||
|
||||
namespace SmallInjector
|
||||
/// <summary>
|
||||
/// A small dependency injector to demonstrate the pattern.
|
||||
/// </summary>
|
||||
public class Container : IContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// A small dependency injector to demonstrate the pattern.
|
||||
/// </summary>
|
||||
public class Container : IContainer
|
||||
private readonly Dictionary<Type, List<RegisteredType>> _container = new();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default) where TService : TInterface
|
||||
{
|
||||
private readonly Dictionary<Type, List<RegisteredType>> _container = new Dictionary<Type, List<RegisteredType>>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterType<TService, TInterface>(bool isSingleton, TService instance = default)
|
||||
where TService : TInterface
|
||||
if (!IsRegistered<TInterface>())
|
||||
{
|
||||
if (!IsRegistered<TInterface>())
|
||||
{
|
||||
_container[typeof(TInterface)] = new List<RegisteredType>
|
||||
{new RegisteredType(typeof(TService), isSingleton, null)};
|
||||
}
|
||||
else
|
||||
{
|
||||
_container[typeof(TInterface)].Add(new RegisteredType(typeof(TService), isSingleton, null));
|
||||
}
|
||||
_container[typeof(TInterface)] = new List<RegisteredType> { 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 bool IsRegistered<TService>() => IsRegistered(typeof(TService));
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsRegistered(Type serviceType) => _container.ContainsKey(serviceType);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<object> ResolveAny(Type serviceType)
|
||||
else
|
||||
{
|
||||
if (!_container.TryGetValue(serviceType, out var registeredTypes))
|
||||
throw new Exception($"Dependency {serviceType.FullName} is not registered");
|
||||
foreach (var registeredType in registeredTypes)
|
||||
{
|
||||
if (registeredType.IsSingleton && registeredType.Instance != null)
|
||||
{
|
||||
yield return registeredType.Instance;
|
||||
continue;
|
||||
}
|
||||
|
||||
var constructor = registeredType.ServiceType.GetConstructors()[0];
|
||||
var parameters = constructor.GetParameters();
|
||||
var constructorParameters = new object[parameters.Length];
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var parameterType = parameters[i].ParameterType;
|
||||
if (typeof(Array).IsAssignableFrom(parameterType))
|
||||
{
|
||||
constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||
}
|
||||
}
|
||||
|
||||
var instance = constructor.Invoke(constructorParameters);
|
||||
yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<object> ResolveAny<TService>() => ResolveAny(typeof(TService));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object Resolve(Type serviceType) => ResolveAny(serviceType).First();
|
||||
|
||||
private class RegisteredType
|
||||
{
|
||||
public bool IsSingleton { get; }
|
||||
public Type ServiceType { get; }
|
||||
public object Instance { get; set; }
|
||||
|
||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||
{
|
||||
ServiceType = serviceType;
|
||||
IsSingleton = isSingleton;
|
||||
Instance = instance;
|
||||
}
|
||||
|
||||
public override string ToString() => ServiceType.ToString();
|
||||
_container[typeof(TInterface)].Add(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 bool IsRegistered<TService>() => IsRegistered(typeof(TService));
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsRegistered(Type serviceType) => _container.ContainsKey(serviceType);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<object> ResolveAny(Type serviceType)
|
||||
{
|
||||
if (!_container.TryGetValue(serviceType, out var registeredTypes))
|
||||
throw new Exception($"Dependency {serviceType.FullName} is not registered");
|
||||
foreach (var registeredType in registeredTypes)
|
||||
{
|
||||
if (registeredType.IsSingleton && registeredType.Instance != null)
|
||||
{
|
||||
yield return registeredType.Instance;
|
||||
continue;
|
||||
}
|
||||
|
||||
var constructor = registeredType.ServiceType.GetConstructors()[0];
|
||||
var parameters = constructor.GetParameters();
|
||||
var constructorParameters = new object[parameters.Length];
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var parameterType = parameters[i].ParameterType;
|
||||
if (typeof(Array).IsAssignableFrom(parameterType))
|
||||
{
|
||||
constructorParameters[i] = ResolveAny(parameterType.GetElementType()).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
constructorParameters[i] = Resolve(parameters[i].ParameterType);
|
||||
}
|
||||
}
|
||||
|
||||
var instance = constructor.Invoke(constructorParameters);
|
||||
yield return registeredType.IsSingleton ? (registeredType.Instance = instance) : instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<object> ResolveAny<TService>() => ResolveAny(typeof(TService));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object Resolve(Type serviceType) => ResolveAny(serviceType).First();
|
||||
|
||||
private class RegisteredType
|
||||
{
|
||||
public bool IsSingleton { get; }
|
||||
public Type ServiceType { get; }
|
||||
public object Instance { get; set; }
|
||||
|
||||
public RegisteredType(Type serviceType, bool isSingleton, object instance)
|
||||
{
|
||||
ServiceType = serviceType;
|
||||
IsSingleton = isSingleton;
|
||||
Instance = instance;
|
||||
}
|
||||
|
||||
public override string ToString() => ServiceType.ToString();
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace SmallInjector;
|
||||
|
||||
namespace SmallInjector
|
||||
/// <summary>
|
||||
/// DI Container
|
||||
/// </summary>
|
||||
public interface IContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// DI Container
|
||||
/// Register types in the resolve 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);
|
||||
/// <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>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
object Resolve(Type serviceType);
|
||||
/// <summary>
|
||||
/// Register types in the resolve container.
|
||||
/// </summary>
|
||||
void RegisterType<TService>(bool isSingleton, TService instance = default);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
TService Resolve<TService>();
|
||||
/// <summary>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
object Resolve(Type serviceType);
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if the service is registered. False otherwise.
|
||||
/// </summary>
|
||||
bool IsRegistered<TService>();
|
||||
/// <summary>
|
||||
/// Resolve service of specified type.
|
||||
/// </summary>
|
||||
TService Resolve<TService>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns true, if the service is registered. False otherwise.
|
||||
/// </summary>
|
||||
bool IsRegistered(Type serviceType);
|
||||
/// <summary>
|
||||
/// Returns true, if the service is registered. False otherwise.
|
||||
/// </summary>
|
||||
bool IsRegistered<TService>();
|
||||
|
||||
/// <summary>
|
||||
/// Resolve any implementation
|
||||
/// </summary>
|
||||
IEnumerable<object> ResolveAny(Type serviceType);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve any implementation
|
||||
/// </summary>
|
||||
IEnumerable<object> ResolveAny<TService>();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns true, if the service is registered. False otherwise.
|
||||
/// </summary>
|
||||
bool IsRegistered(Type serviceType);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve any implementation
|
||||
/// </summary>
|
||||
IEnumerable<object> ResolveAny(Type serviceType);
|
||||
|
||||
/// <summary>
|
||||
/// Resolve any implementation
|
||||
/// </summary>
|
||||
IEnumerable<object> ResolveAny<TService>();
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
Reference in New Issue
Block a user