Multiple Interface injection with castle windsor

别等时光非礼了梦想. 提交于 2019-11-30 04:01:21

As David said, you can't, but IHandlerSelector will let you take control. Check out the tests to get an idea of how to use them: https://svn.castleproject.org/svn/castle/trunk/InversionOfControl/Castle.Windsor.Tests/HandlerSelectorsTestCase.cs

Basically, you would do something like:

public class WritenExamHandler : IHandlerSelector
    {
        public bool HasOpinionAbout(string key, Type service)
        {
            // Decision logic here
            return somethingThatWouldBeTrueToSelectWritenExam && service == typeof(IExamCalc);
        }

        public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
        {
            return handlers.Where(handler => handler.ComponentModel.Implementation == typeof (WritenExam)).First();
        }
    }

and then you register it with:

container.Kernel.AddHandlerSelector(new WritenExamHandler());

This will allow you to easily deal with multi-tenency issues :)

Multi-tenancy is defined as being able to run your software on one instance, serving multiple tenants/customers/clients. I guess you could run into problems like yours more often in a multi-tenancy setup.

All your components have keys which are unique strings, so you may always so a container.Resolve("someKey") to get a specific implementation.

If you want to have a specific implementation automatically injected, you may configure your component like this (off my memory, may not be 100% precise):

<component id="someService.customer1" service="ISomeService" type="Customer1SomeService" />

<component id="anotherId" service="IAnotherService" type="AnotherService">
    <parameters>
        <parameterName> <!-- as written in the ctor's signature -->
           ${someService.customer1}
        </parameterName>
    </parameters> 
</component>
David Kemp

The short answer is, you can't. This kind of choice is dependent on application code, so if you just did container.Resolve<IExamCalc>, then Windsor couldn't know which one you wanted.

The question to ask is how do you know which type to use?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!