小编Mar*_*ski的帖子

Castle Windsor:使用约定注册以及特定实现

假设由FirstBar和SecondBar实现的Foo和IBar实现了IFoo.

使用此约定注册:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface())
Run Code Online (Sandbox Code Playgroud)

我们在容器中有三个条目:

IFoo = Foo
IBar = FirstBar
IBar = SecondBar
Run Code Online (Sandbox Code Playgroud)

现在,我们如何调整此注册以告知容器对于IBar我们只想注册SecondBar?有点:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface()
        .For<IBar>().Select<SecondBar>())
Run Code Online (Sandbox Code Playgroud)

使用案例:我们的应用程序中有很多服务都是按惯例注册的.但是,一些服务接口具有两个或更多实现(例如,实现,虚假实现和测试实现).公约注册将在同一界面下注册它们,在解析界面时,我们将获得第一个实现(以非确定性顺序).我们希望能够在注册时为这些服务选择一个特定的实现.我们怎么做?

castle-windsor

5
推荐指数
2
解决办法
3225
查看次数

标签 统计

castle-windsor ×1