[英]Resolve IEnumerable of All Types that Implement Generic Interface in Autofac
I want to be able to resolve a collection of services from Autofac that represents all registered types which implement an open generic interface.我希望能够从 Autofac 中解析一组服务,这些服务代表所有实现开放通用接口的注册类型。
public interface IEntityService<in T> where T : Entity
{
void DoEntityWork(T entity);
}
I have many classes that inherit Entity
and many corresponding service classes which implement IEntityService
for that Entity.我有许多继承
Entity
的类和许多为该实体实现IEntityService
的相应服务类。
public class EntityA : Entity { }
public class EntityB : Entity { }
public class EntityC : Entity { }
public class EntityAService : IEntityService<EntityA>
{
public void DoEntityWork(EntityA entity)
}
public class EntityBService : IEntityService<EntityB>
{
public void DoEntityWork(EntityB entity)
}
public class EntityCService : IEntityService<EntityC>
{
public void DoEntityWork(EntityB entity)
}
Here is how I am registering them with Autofac:这是我使用 Autofac 注册它们的方式:
builder.RegisterType<EntityAService>().As<IEntityService<EntityA>();
builder.RegisterType<EntityBService>().As<IEntityService<EntityB>();
builder.RegisterType<EntityCService>().As<IEntityService<EntityC>();
What I would like to be able to do is to resolve each one of those IEntityService
registrations in a collection.我想做的是解决集合中的每一个
IEntityService
注册。 However, attempting to inject them with the following code returns an empty collection:但是,尝试使用以下代码注入它们会返回一个空集合:
public class MyProcessingClass(IEnumerable<IEntityService<Entity>> entityServices)
{
_entityServices = entityServices;
}
I have tried instead registering them all As<IEntityService<Entity>>()
, but this throws an ArgumentException
with the following message:我尝试将它们全部注册为
As<IEntityService<Entity>>()
,但这会引发ArgumentException
并显示以下消息:
The type 'MyProject.Services.EntityAService' is not assignable to service 'MyProject.Interfaces.IEntityService'1[[MyProject.Models.Entity]]
How can I resolve all of the types which implement IEntityService
with a type argument that implements Entity
?如何使用实现
Entity
的类型参数来解析实现IEntityService
的所有类型?
This is how contravariance works in C# works, EntityAService
is not IEntityService<Entity>
, this relation works another way around:这就是逆变在 C# 中的工作方式,
EntityAService
不是IEntityService<Entity>
,这种关系以另一种方式工作:
public class EntityService : IEntityService<Entity>
{
public void DoEntityWork(Entity entity) { }
}
IEntityService<EntityA> x = new EntityService();
One way around would be introducing a non-generic version of IEntityService
, registering all your entity services as that one and resolving IEnumerable<IEntityService>
.一种解决方法是引入
IEntityService
的非通用版本,将所有实体服务注册为该版本并解析IEnumerable<IEntityService>
。
Autofac doesn't support this sort of thing out of the box. Autofac 不支持这种开箱即用的东西。 There is a
ContravariantRegistrationSource
but it does sort of the inverse of this - if you register IEntityService<Entity>
and then resolve IEntityService<EntityA>
it'll resolve the IEntityService<Entity>
instance for you.有一个
ContravariantRegistrationSource
但它确实与此相反 - 如果您注册IEntityService<Entity>
然后解析IEntityService<EntityA>
它将为您解析IEntityService<Entity>
实例。 It doesn't allow you to resolve IEnumerable<IEntityService<Entity>>
and locate all the derived types.它不允许您解析
IEnumerable<IEntityService<Entity>>
并定位所有派生类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.