简体   繁体   English

将其他通用类型注册为已扫描

[英]Registering a different generic type as scanned

I try to register a generic type Repository<T> with the interface IRepository<T> where T is IEntity . 我尝试通过接口IRepository<T>注册通用类型Repository<T> ,其中TIEntity

builder.RegisterAssemblyTypes(assemblies)
       .Where(t => typeof(IEntity).IsAssignableFrom(t))
       .WithMetadata("Type", (t) => t)
       .AsImplementedInterfaces()
       .InstancePerLifetimeScope();

The question is now how to register for each IEntity found my class 现在的问题是如何为找到我的课程的每个IEntity注册

Repository<T>: IRepository<T> where T: IEntity

Actually, you don't need to register the entities, you only have to register the repository. 实际上,您不需要注册实体,只需要注册存储库。 Autofac have excellent support for generics, both open and closed types. Autofac对开放式和封闭式泛型都有出色的支持。 From the OpenGenerics documentation, register the open generic type of your repository: OpenGenerics文档中,注册存储库的开放通用类型:

builder.RegisterGeneric(typeof(Repository<>))
    .As(typeof(IRepository<>));

You can now resolve closed repository types like this: 现在,您可以解析封闭的存储库类型,如下所示:

var userRepo = container.Resolve<IRepository<User>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Castle Windsor注册通用类型 - Registering generic type in Castle Windsor 向多个构造函数注册通用类型 - Registering generic type with multiple constructors 在 Autofac 中注册一个开放泛型类型 - Registering an open-generic type in Autofac 使用Generic复杂类型和AutoFac注册通用类型 - Registering Generic Types using Generic complex type with AutoFac 在 .NET Core (MS.DI) 中注册具有类型约束的泛型 - Registering generic type with type constraint in .NET Core (MS.DI) Simple Injector:使用构造函数参数注册开放通用类型 - Simple Injector: Registering open generic type with constructor parameter 使用StructureMap注册具有通用类型的Contrete类 - Registering a Contrete Class which has a Generic Type with StructureMap 注册一个开放的通用服务错误地要求我提供类型参数 - Registering an open Generic Service erroneously asks me for type arguments 注册使用开放泛型类型将多个接口实现为单例的服务 - Registering service that implements several interfaces as singleton using open generic type 将带有已定义类型的通用接口注册到没有类型的通用接口有什么区别? - What is the difference in registering generic interface with defined type into generic interface without type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM