Code reorganization related to MediatR infrastructure

This commit is contained in:
Anton Kasyanov
2018-02-18 17:17:52 +02:00
parent 7b5858287a
commit 59f4e193d6
29 changed files with 182 additions and 180 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using LightInject;
namespace EveOPreview
@@ -19,31 +21,38 @@ namespace EveOPreview
return this._container.CanGetInstance(typeof(TService), "");
}
public void Register<TService>()
public void Register(Type serviceType, Assembly container)
{
if (!typeof(TService).IsInterface)
if (!serviceType.IsInterface)
{
this._container.Register<TService>(new PerContainerLifetime());
this._container.Register(serviceType, new PerContainerLifetime());
return;
}
foreach (Type implementationType in typeof(TService).Assembly.DefinedTypes)
if (serviceType.IsInterface && serviceType.IsGenericType)
{
if (!implementationType.IsClass || implementationType.IsAbstract)
{
continue;
}
if (!typeof(TService).IsAssignableFrom(implementationType))
{
continue;
}
this._container.Register(typeof(TService), implementationType, new PerContainerLifetime());
break;
this._container.RegisterAssembly(container, (st, it) => st.IsConstructedGenericType && st.GetGenericTypeDefinition() == serviceType);
}
else
{
foreach (TypeInfo implementationType in container.DefinedTypes)
{
if (!implementationType.IsClass || implementationType.IsAbstract)
{
continue;
}
if (serviceType.IsAssignableFrom(implementationType))
{
this._container.Register(serviceType, implementationType, new PerContainerLifetime());
}
}
}
}
public void Register<TService>()
{
this.Register(typeof(TService), typeof(TService).Assembly);
}
public void Register<TService, TImplementation>()
@@ -52,12 +61,17 @@ namespace EveOPreview
this._container.Register<TService, TImplementation>(new PerContainerLifetime());
}
public void Register<TService>(Expression<Func<TService>> factory)
{
this._container.Register(f => factory);
}
public void Register<TService, TArgument>(Expression<Func<TArgument, TService>> factory)
{
this._container.Register(f => factory);
}
public void RegisterInstance<T>(T instance)
public void RegisterInstance<TService>(TService instance)
{
this._container.RegisterInstance(instance);
}
@@ -66,5 +80,20 @@ namespace EveOPreview
{
return this._container.GetInstance<TService>();
}
public IEnumerable<TService> ResolveAll<TService>()
{
return this._container.GetAllInstances<TService>();
}
public object Resolve(Type serviceType)
{
return this._container.GetInstance(serviceType);
}
public IEnumerable<object> ResolveAll(Type serviceType)
{
return this._container.GetAllInstances(serviceType);
}
}
}