[英]Resolving ALL generics as enumerable in Autofac
我正在嘗試使用基本類型的IEnumerable解析所有泛型,甚至不確定是否完全可行...我正在尋找一種解決方案,其中我不必依賴autofac容器,因為我沒有訪問它 。 看到下面的代碼,只需將粘貼復制到您的Program.cs文件中,您將需要引用Autofac來查看我需要的內容:)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Autofac;
namespace AutoFacExperiments
{
class Program
{
static void Main(string[] args)
{
var containerBuilder = new ContainerBuilder();
//containerBuilder.RegisterGeneric(typeof (GenericManager<>)).As(typeof(IGenericManager<>));
var assembly = Assembly.GetAssembly(typeof(BaseEntity));
var allDerivedTypes = assembly.GetTypes().Where(t => GetBaseTypes(t).Contains(typeof(BaseEntity)));
var baseInterfaceType = typeof(IGenericManager<>).MakeGenericType(typeof(BaseEntity));
foreach (var typeToRegister in allDerivedTypes)
{
var t = typeof(GenericManager<>).MakeGenericType(typeToRegister);
var interfaceType = typeof(IGenericManager<>).MakeGenericType(typeToRegister);
containerBuilder.RegisterType(t)
.As(interfaceType).As(baseInterfaceType);
// Autofac doesn't like .As(baseInterfaceType);
// if I change IGenericManager<TEntity> to IGenericManager<out TEntity> it all works but then I cannot use TEntity inside method parameters due to co/contra variance laws
}
containerBuilder.RegisterType<App>().SingleInstance();
var builtContainer = containerBuilder.Build();
var app = builtContainer.Resolve<App>();
app.Run();
}
private static IEnumerable<Type> GetBaseTypes(Type target)
{
do
{
yield return target.BaseType;
target = target.BaseType;
} while (target != typeof(object) && target != null);
}
}
internal class App
{
private readonly Func<IContext, IGenericManager<EntityOne>> _entityOneGenericManagerFactory;
private readonly Func<IContext, IGenericManager<EntityTwo>> _entityTwoGenericManagerFactory;
private readonly IEnumerable<Func<IContext, IGenericManager<BaseEntity>>> _allGenericManagerFactories;
public App(
Func<IContext, IGenericManager<EntityOne>> entityOneGenericManagerFactory,
Func<IContext, IGenericManager<EntityTwo>> entityTwoGenericManagerFactory,
IEnumerable<Func<IContext, IGenericManager<BaseEntity>>> allGenericManagerFactories)
{
_entityOneGenericManagerFactory = entityOneGenericManagerFactory;
_entityTwoGenericManagerFactory = entityTwoGenericManagerFactory;
_allGenericManagerFactories = allGenericManagerFactories;
}
public void Run()
{
// !!!!!!!!!!!!!!! the collection _allGenericManagerFactories only contains the one factory for BaseEntity or empty depending on registration type
var list = new List<IGenericManager<BaseEntity>>();
foreach (var genericManagerFactory in _allGenericManagerFactories)
{
var entity = genericManagerFactory(new Context());
list.Add(entity);
}
}
}
internal class GenericManager<TEntity> : IGenericManager<TEntity> where TEntity : BaseEntity
{
private readonly IContext _context;
public GenericManager(IContext context)
{
_context = context;
}
public void Add(TEntity entity)
{
// entity gets added to the underlying context
}
public IEnumerable<TEntity> FindAllWithIncludeExpressions(Expression<Func<TEntity, bool>> filter = null, Func<IEnumerable<TEntity>, IOrderedEnumerable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties)
{
return new TEntity[0];
}
}
public interface IContext
{
}
internal class Context : IContext
{
}
public interface IGenericManager<TEntity> where TEntity : BaseEntity
{
void Add(TEntity entity);
IEnumerable<TEntity> FindAllWithIncludeExpressions(Expression<Func<TEntity, bool>> filter = null, Func<IEnumerable<TEntity>, IOrderedEnumerable<TEntity>> orderBy = null, params Expression<Func<TEntity, object>>[] includeProperties);
}
public abstract class BaseEntity
{
}
public class EntityOne : BaseEntity
{
}
public class EntityTwo : BaseEntity
{
}
public class EntityOnePointOne : EntityOne
{
}
}
我使用T4模板並使用簡單的Autofac開放式通用注冊方法進行了相當不錯的工作,如果有人對答案感興趣,我很樂意在此處發布其簡化版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.