为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
哔哔one 2023-05-14 16:45:50
我尝试注册基于泛型类的具体类,而这个泛型类是基于泛型接口的。问题是如何在 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 ConcreteServicevar test2 = IocContainer.Resolve<IQueryService<AnotherSomeType>(); // Here should be AnotherConcreteService通过逐行方法,它可以工作,但是如何注册所有基于IQueryService<>?
查看完整描述

2 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

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()

{

    var thisAssembly = ...


    IocManager.RegisterAssemblyByConvention(thisAssembly);


    IocManager.IocContainer.Register(

        Classes.FromAssembly(thisAssembly)

            .BasedOn(typeof(IQueryService<>))

            .WithService.Base()

            .Configure(configurer => configurer.Named(Guid.NewGuid().ToString())) // Add this

    );


    // ...

}

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 : ...

{

    /* Define a DbSet for each entity of the application */


    // DbSet<SomeType> SomeEntities { get; set; }     // Remove this

    public DbSet<SomeType> SomeEntities { get; set; } // Add this


    // ...

}


查看完整回答
反对 回复 2023-05-14
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

如果您的目标是在一个表达式中注册泛型的所有各种实现,那么IQueryService<T>您很可能会这样做


container.Register(

   Classes.FromThisAssembly()

      .BasedOn(typeof(IQueryService<>))

      .WithServiceBase()

);


查看完整回答
反对 回复 2023-05-14
  • 2 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信