Windsor Castle: - Injection dictionary of interfaces through configuration

Hi, I am trying to enter an interface dictionary, but I am getting an error from the lock as follows: -

Castle.MicroKernel.SubSystems.Conversion.ConverterException: the converter is not registered to process type IFoo

To get around the exception, I had to create a wrapper containing the list of the Ifoo interface, and return it using the property. Then the shell was used in the dictionary == ==> instead of the dictionary

Is there a way in the lock that I can just have an interface dictionary instead of this workaround?

public interface IFoo {}
public class Foo {}
public class IfooWrapper {
    IList<IFoo> container{get;set;}
}
+3
source share
2 answers

- . , . , , -, . .

, " ", , " " Castle Windsor. , ...

, , , , .

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"]);
        }
    }
}
+6

Source: https://habr.com/ru/post/1708777/


All Articles