简体   繁体   English

温莎城堡: - 通过配置注入接口字典

[英]Windsor Castle :- Inject Dictionary of Interfaces via configuration

Hi i am trying to inject a dictionary of interfaces but am getting an error from castle like this :- 嗨,我正在尝试注入接口字典,但我从这样的城堡得到一个错误: -

Castle.MicroKernel.SubSystems.Conversion.ConverterException: No converter registered to handle the type IFoo Castle.MicroKernel.SubSystems.Conversion.ConverterException:没有注册转换器来处理类型IFoo

In order to go around the exception, I had to create a wrapper that contained a list of the Ifoo interface and returns it using a property. 为了绕过异常,我必须创建一个包含Ifoo接口列表的包装器并使用属性返回它。 The wrapper was then used in the configuration ==> dictionary instead of dictionary 然后在配置==>字典而不是字典中使用包装器

Is there a way in castle that I can just have a dictionary of Interface instead of doing this workaround ? 在城堡中有没有办法,我可以只有一个Interface的字典而不是这个解决方法?

public interface IFoo {}
public class Foo {}
public class IfooWrapper {
    IList<IFoo> container{get;set;}
}

This works just fine for me (Windsor 2.0): 这对我来说很好(Windsor 2.0):

namespace WindsorTests {
    public interface IService {}    
    public class Service1 : IService {}    
    public class Service2 : IService {}    
    public class Consumer {
        private readonly IDictionary<string, IService> services;    
        public IDictionary<string, IService> Services {
            get { return services; }
        }    
        public Consumer(IDictionary<string, IService> services) {
            this.services = services;
        }
    }    

    [TestFixture]
    public class WindsorTests {    
        [Test]
        public void DictTest() {
            var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(@"<castle>
<components>
    <component id=""service1"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service1, MyAssembly""/>
    <component id=""service2"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service2, MyAssembly""/>
    <component id=""consumer"" type=""WindsorTests.Consumer, MyAssembly"">
        <parameters>
            <services>
                <dictionary>
                    <entry key=""one"">${service1}</entry>
                    <entry key=""two"">${service2}</entry>
                </dictionary>
            </services>
        </parameters>
    </component>
</components>
</castle>")));
            var consumer = container.Resolve<Consumer>();
            Assert.AreEqual(2, consumer.Services.Count);
            Assert.IsInstanceOfType(typeof(Service1), consumer.Services["one"]);
            Assert.IsInstanceOfType(typeof(Service2), consumer.Services["two"]);
        }
    }
}

I've had to do something very similar. 我必须做一些非常相似的事情。 However I think it exposed a design flaw more than anything else. 但是我觉得它暴露了设计缺陷比什么都重要。 I refactored my app so that it did what your wrapper class does as its standard way of working. 我重构了我的应用程序,以便它完成了你的包装类所做的标准工作方式。 It drastically simplified the app as well. 它也大大简化了应用程序。

It was really just a matter of doing things the "Castle Windsor way" instead of trying to adapt "my way" to the Castle Windsor model. 这真的只是做“温莎城堡”的事情,而不是试图让“我的方式”适应温莎城堡模型。 It was pretty humbling to see how much easier and better the Castle Windsor way was... 看到温莎城堡的方式更加轻松和优越,真是令人羞愧......

Technically not a solution to the problem you posed but hopefully it helps you out. 从技术上讲,这不是你提出的问题的解决方案,但希望它可以帮助你。

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

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