猿问

如何将具体类注册到通用接口?

我尝试注册基于泛型类的具体类,而这个泛型类是基于泛型接口的。问题是如何在 Castle Windsor 和 ASP.NET Boilerplate 中执行此操作,这样我就不需要很多代码来一一注册它。


public interface IService<T> where T: class ...


public class Service<T, TE, TPrimary> : IService<T> where T: class ...


public class ConcreteService : Service<SomeType, SomeType2, string> ...

public class AnotherConcreteService : Service<AnotherSomeType, AnotherSomeType2, string> ...

有了这样的结构,我想注册到实现这个服务的IService<SomeType>类ConcreteService。我怎么能用温莎城堡做到这一点?一个一个的实现看起来像这样:


IocManager.IocContainer.Register(Component.For(typeof(IQueryService<SomeType>))

    .ImplementedBy(typeof(ConcreteService)).Named("ConcreteTest"));


IocManager.IocContainer.Register(Component.For(typeof(IQueryService<AnotherSomeType>))

    .ImplementedBy(typeof(AnotherConcreteService)).Named("AnotherConcreteTest"));

我想要的用法:


var test1 = IocContainer.Resolve<IQueryService<SomeType>(); // Here should be ConcreteService

var test2 = IocContainer.Resolve<IQueryService<AnotherSomeType>(); // Here should be AnotherConcreteService

通过逐行方法,它可以工作,但是如何注册所有基于IQueryService<>?


哔哔one
浏览 143回答 2
2回答

扬帆大鱼

1. IQueryService<SomeTypeDto>Castle.MicroKernel.ComponentNotFoundException: 没有支持服务 AbpCompanyName.AbpProjectName.IGenerics.IQueryService`1[[AbpCompanyName.AbpProjectName.SomeEntitiesDto.SomeTypeDto, AbpCompanyName.AbpProjectName.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 的组件]] 被找到要将具体类注册到通用接口,请使用.WithService.Base().为了与兼容1RegisterAssemblyByConvention,配置一个唯一的名称。public override void Initialize(){&nbsp; &nbsp; var thisAssembly = ...&nbsp; &nbsp; IocManager.RegisterAssemblyByConvention(thisAssembly);&nbsp; &nbsp; IocManager.IocContainer.Register(&nbsp; &nbsp; &nbsp; &nbsp; Classes.FromAssembly(thisAssembly)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .BasedOn(typeof(IQueryService<>))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WithService.Base()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Configure(configurer => configurer.Named(Guid.NewGuid().ToString())) // Add this&nbsp; &nbsp; );&nbsp; &nbsp; // ...}1如果您的具体类实现了 ASP.NET 样板ITransientDependency,则默认名称用于在.WithService.Self()内部注册该类RegisterAssemblyByConvention。2. IRepository<SomeType>Castle.MicroKernel.Handlers.HandlerException:无法创建组件“AbpCompanyName.AbpProjectName.SomeEntityService.SomeTypeService”,因为它具有需要满足的依赖项。'AbpCompanyName.AbpProjectName.SomeEntityService.SomeTypeService' 正在等待以下依赖项:- 服务 'Abp.Domain.Repositories.IRepository`1[[AbpCompanyName.AbpProjectName.SomeEntities.SomeType, AbpCompanyName.AbpProjectName.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 未注册。要使用 ASP.NET Boilerplate 存储库,请定义一个公共DbSet。public class AbpProjectNameDbContext : ...{&nbsp; &nbsp; /* Define a DbSet for each entity of the application */&nbsp; &nbsp; // DbSet<SomeType> SomeEntities { get; set; }&nbsp; &nbsp; &nbsp;// Remove this&nbsp; &nbsp; public DbSet<SomeType> SomeEntities { get; set; } // Add this&nbsp; &nbsp; // ...}

饮歌长啸

如果您的目标是在一个表达式中注册泛型的所有各种实现,那么IQueryService<T>您很可能会这样做container.Register(   Classes.FromThisAssembly()      .BasedOn(typeof(IQueryService<>))      .WithServiceBase());
随时随地看视频慕课网APP
我要回答