简体   繁体   English

我该如何在温莎城堡注册?

[英]How do would I register this in Castle Windsor?

public interface IDo
{
    ... details 
}
public class DoOneThing : IDo
{
    ...
}
public class DoAnotherThing : IDo
{
    ....
}

public interface IFooService
{
    ... details
}

public class FooService
{
    private IDo do;

    public FooService(IDo do)
    {
        // instance is of type specifically resolved per call
        this.do = do;
    }


    ...
}

Container.Register(ComponentFor<IDo>().ImplementedBy<DoOneThing>().Named("DoOneThing");
Container.Register(ComponentFor<IFooService>().ImplementedBy<FooService>().DependsOn(Dependency.OnComponent(typeof(IDo), "DoOneThing")).Named("DoItWithOneThing");
Container.Register(ComponentFor<IFooService>().ImplementedBy<FooService>().DependsOn(Dependency.OnComponent(typeof(IDo), "DoAnotherThing")).Named("DoItWithAnotherThing");



Container.Resolve<IFooService>("DoItWithOneThing");

How Do I register FooService to have a dependency of type IDo and then resolve with a specific implementation type? 如何注册FooService使其具有IDo类型的依赖关系,然后使用特定的实现类型进行解析? I've tried using something like the code above but I get an exception that no component for service can be found. 我尝试使用类似上面的代码的方法,但是出现一个例外,即找不到服务组件。 If I try to resolve to the named instance then it tells me that it's waiting for dependencies of DoOneThing. 如果我尝试解析为命名实例,则它告诉我它正在等待DoOneThing的依赖项。

You can use the typed Dependency.OnComponent as mentioned in Castle Windsor - multiple implementation of an interface . 您可以使用Castle Windsor中提到的类型化的Dependency.OnComponent 接口的多重实现

See also: Castle Project -- Inline dependencies 另请参阅: Castle Project-内联依赖项

var container = new WindsorContainer();

container.Register(
    Component
        .For<IDo>()
        .ImplementedBy<DoAnotherThing>());

container.Register(
    Component
        .For<IDo>()
        .ImplementedBy<DoOneThing>());

container.Register(
    Component
        .For<IFooService>()
        .ImplementedBy<FooService>()
        .Named("DoItWithOneThing")
        .DependsOn(
            Dependency.OnComponent<IDo, DoOneThing>()));

container.Register(
    Component
        .For<IFooService>()
        .ImplementedBy<FooService>()
        .Named("DoItWithAnotherThing")
        .DependsOn(
            Dependency.OnComponent<IDo, DoAnotherThing>()));

Test 测试

var doItWithOneThing = container.Resolve<IFooService>("DoItWithOneThing");
var doItWithAnotherThing = container.Resolve<IFooService>("DoItWithAnotherThing");

Console
    .WriteLine(
        "doItWithOneThing.Do is DoOneThing // {0}",
        doItWithOneThing.Do is DoOneThing);
Console
    .WriteLine(
        "doItWithAnotherThing.Do is DoAnotherThing // {0}",
        doItWithAnotherThing.Do is DoAnotherThing);

Output 产量

doItWithOneThing.Do is DoOneThing // True
doItWithAnotherThing.Do is DoAnotherThing // True

Declarations 声明

public interface IDo {}
public class DoOneThing : IDo {}
public class DoAnotherThing : IDo {}
public interface IFooService
{
    IDo Do { get; }
}

public class FooService : IFooService
{
    public FooService(IDo @do)
    {
        Do = @do;
    }

    public IDo Do { get; private set; }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM