亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將具體類注冊到通用接口?

如何將具體類注冊到通用接口?

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 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號