Dependency injection 如何在Castle Windsor中隐式(内联,解析时)应用服务覆盖?

Dependency injection 如何在Castle Windsor中隐式(内联,解析时)应用服务覆盖?,dependency-injection,castle-windsor,overriding,Dependency Injection,Castle Windsor,Overriding,考虑使用附加参数来解析“有效”组件的形式。请注意,如果在这里解析根,则没有问题 var c1a = container.Resolve<IC1>(new { v = "a" }); var c1b = container.Resolve<IC1>(new { v = "b" }); // Takes two different objects, of the same interface var root = container.Resolve<C2>(new

考虑使用附加参数来解析“有效”组件的形式。请注意,如果在这里解析根,则没有问题

var c1a = container.Resolve<IC1>(new { v = "a" });
var c1b = container.Resolve<IC1>(new { v = "b" });
// Takes two different objects, of the same interface
var root = container.Resolve<C2>(new { c1a = c1a, c2a = c2a });
我想做的是使用隐式服务覆盖的类似以下内容(当然无效)——请注意,只有一个直接解析,对象应该由CW在内部创建

var root = container.Resolve<C2>(new {
   c1a = InlineOverride.For<IC1>.With(new { v = "a" }),
   c2a = InlineOverride.For<IC1>.With(new { v = "a" })
});
container.Release(root); // Just one root
var root=container.Resolve(新){
c1a=InlineOverride.For.With(new{v=“a”}),
c2a=InlineOverride.For.With(new{v=“a”})
});
容器。释放(根);//只有一根

我有一种感觉,我只是错过了一个概念/应用程序,它似乎只有在注册依赖(C2)组件时才得到明确支持。

因此,这只是我处理类似问题的一种方法,不确定它是否会有所帮助

public class Tinker
{
    public interface ISomethingWithChar
    {
        char V { get; set; }
    }

    public class SomethingWithChar:ISomethingWithChar
    {
        public char V { get; set; }
    }

    public interface INeedTwoSomethingWithChars
    {
        ISomethingWithChar C1A { get; set; }
        ISomethingWithChar C2A { get; set; }
    }

    public class NeedTwoSomethingWithChars:INeedTwoSomethingWithChars
    {
        public ISomethingWithChar C1A { get; set; }
        public ISomethingWithChar C2A { get; set; }
    }

    public class Installers:IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
            Component
                .For<ISomethingWithChar>()
                .ImplementedBy<SomethingWithChar>()
                .Named("CharOne")
                .DynamicParameters((k,d) =>
                {
                    d["V"] = container.Resolve<IArgs>().CharOne;
                }),
            Component
                .For<ISomethingWithChar>()
                .ImplementedBy<SomethingWithChar>()
                .Named("CharTwo")
                .DynamicParameters((k, d) =>
                {
                    d["V"] = container.Resolve<IArgs>().CharTwo;
                }),
            Component
                .For<INeedTwoSomethingWithChars>()
                .ImplementedBy<NeedTwoSomethingWithChars>()
                .DependsOn(Parameter.ForKey("C1A").Eq("${CharOne}"), Parameter.ForKey("C2A").Eq("${CharTwo}")));
        }
    }


    public static void DoSomething(IArgs args)
    {
        using (var container = BootstrapContainer(args))
        {
            var needTwo = container.Resolve<INeedTwoSomethingWithChars>();
            Console.WriteLine("One: {0} and Two: {1}", needTwo.C1A.V, needTwo.C2A.V);
        }
    }

    private static IWindsorContainer BootstrapContainer(IArgs args)
    {
        var container = new WindsorContainer();
        container.Register(Component
            .For<IArgs>()
            .Instance(args));

        container.Install(FromAssembly.This());

        return container;
    }
}
公共类修补程序
{
公共接口ISomethingWithChar
{
char V{get;set;}
}
公共类SomethingWithChar:ISomethingWithChar
{
公共字符V{get;set;}
}
公共接口为两个字符
{
ISomethingWithChar C1A{get;set;}
ISomethingWithChar C2A{get;set;}
}
公共类需要两个带字符的东西:我需要两个带字符的东西
{
public ISomethingWithChar C1A{get;set;}
public ISomethingWithChar C2A{get;set;}
}
公共类安装程序:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
集装箱。登记(
组成部分
.对于()
.由()实施
.命名为(“CharOne”)
.动态参数((k,d)=>
{
d[“V”]=container.Resolve().CharOne;
}),
组成部分
.对于()
.由()实施
.命名为(“CharTwo”)
.动态参数((k,d)=>
{
d[“V”]=container.Resolve().CharTwo;
}),
组成部分
.对于()
.由()实施
.DependsOn(Parameter.ForKey(“C1A”).Eq(${CharOne})、Parameter.ForKey(“C2A”).Eq(${CharTwo}));
}
}
公共静态无效剂量测定(IArgs args)
{
使用(var容器=BootstrapContainer(args))
{
var needTwo=container.Resolve();
WriteLine(“一:{0}和二:{1}”,needTwo.C1A.V,needTwo.C2A.V);
}
}
专用静态IWindsorContainer引导容器(IArgs args)
{
var container=新的WindsorContainer();
容器寄存器(组件
.对于()
.实例(args));
container.Install(fromsassembly.This());
返回容器;
}
}
public class Tinker
{
    public interface ISomethingWithChar
    {
        char V { get; set; }
    }

    public class SomethingWithChar:ISomethingWithChar
    {
        public char V { get; set; }
    }

    public interface INeedTwoSomethingWithChars
    {
        ISomethingWithChar C1A { get; set; }
        ISomethingWithChar C2A { get; set; }
    }

    public class NeedTwoSomethingWithChars:INeedTwoSomethingWithChars
    {
        public ISomethingWithChar C1A { get; set; }
        public ISomethingWithChar C2A { get; set; }
    }

    public class Installers:IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
            Component
                .For<ISomethingWithChar>()
                .ImplementedBy<SomethingWithChar>()
                .Named("CharOne")
                .DynamicParameters((k,d) =>
                {
                    d["V"] = container.Resolve<IArgs>().CharOne;
                }),
            Component
                .For<ISomethingWithChar>()
                .ImplementedBy<SomethingWithChar>()
                .Named("CharTwo")
                .DynamicParameters((k, d) =>
                {
                    d["V"] = container.Resolve<IArgs>().CharTwo;
                }),
            Component
                .For<INeedTwoSomethingWithChars>()
                .ImplementedBy<NeedTwoSomethingWithChars>()
                .DependsOn(Parameter.ForKey("C1A").Eq("${CharOne}"), Parameter.ForKey("C2A").Eq("${CharTwo}")));
        }
    }


    public static void DoSomething(IArgs args)
    {
        using (var container = BootstrapContainer(args))
        {
            var needTwo = container.Resolve<INeedTwoSomethingWithChars>();
            Console.WriteLine("One: {0} and Two: {1}", needTwo.C1A.V, needTwo.C2A.V);
        }
    }

    private static IWindsorContainer BootstrapContainer(IArgs args)
    {
        var container = new WindsorContainer();
        container.Register(Component
            .For<IArgs>()
            .Instance(args));

        container.Install(FromAssembly.This());

        return container;
    }
}