多个接口注入与城堡windsor [英] Multiple Interface injection with castle windsor

查看:108
本文介绍了多个接口注入与城堡windsor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,让我说,我有这样一个例子:一个简单的界面,称为IExamCalc,它进行计算,以了解某人在该考试中的做法。



不,我们有几个实现,例如,如下所示:

  public interface IExamCalc 
{
int CalculateMark(ExamAnswers examAnswers)
}

public class WritenExam:IExamCalc
{
public int CalculateMark(ExamAnswers examAnswers)
{
return 4;
}
}

public class OralExam:IExamCalc
{
public int CalculateMark(ExamAnswers examAnswers)
{
return 8 ;
}
}

public class ExamMarkService
{
private IExamCalc _examCalc;
public ExamMarkService(IExamCalc examCalc)
{
_examCalc = examCalc;
}

public int [] CalculateExamMarks(ExamAnswers [] examAnswers)
{
IList< int> marks = new List< int> ;;
foreach(examAnswers中的ExamAnswers考试)
{
marks.Add(_examCalc.CalculateMark);
}
}
}

说ExamMarkService正在被重新通过Windor,我如何确保在构造函数中注入正确的实现,这是一个多租户问题的例子?



希望所有的使p>

Colin G

解决方案

正如David所说,你不能,但是IHandlerSelector将让您控制。检查测试以了解如何使用它们: https://svn.castleproject.org/svn/castle/trunk/InversionOfControl/Castle.Windsor.Tests/HandlerSelectorsTestCase.cs



基本上你可以这样做:

  public class WritenExamHandler:IHandlerSelector 
{
public bool HasOpinionAbout键,类型服务)
{
//决定逻辑在这里
返回somethingThatWouldBeTrueToSelectWritenExam&& service == typeof(IExamCalc);
}

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

然后您注册:

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

这将允许您轻松处理多任务问题:)


How can you get castle Windsor to choose the right implantation of a interface at run time when you have multiple implementations in the container.

For example lets say i have a simple interface called IExamCalc that does calculations to work out how someone did in that exam.

No we have several implementation of this like bellow for example,

public interface IExamCalc
{
    int CalculateMark(ExamAnswers examAnswers)
}

public class WritenExam : IExamCalc
{
    public int CalculateMark(ExamAnswers examAnswers)
    {
         return 4;
    }
}

public class OralExam : IExamCalc
{
    public int CalculateMark(ExamAnswers examAnswers)
    {
         return 8;
    }
}

public class ExamMarkService
{
    private IExamCalc _examCalc;
    public ExamMarkService(IExamCalc examCalc)
    {
        _examCalc = examCalc;
    }

    public int[] CalculateExamMarks(ExamAnswers[] examAnswers)
    {
        IList<int> marks = new List<int>;
        foreach(ExamAnswers examanswer in examaAnswers)
        {
            marks.Add(_examCalc.CalculateMark);
        }
    }
}

Say the ExamMarkService is being resloved through Windor how can i make sure that the correct implementation is injected in the constructor and is this an example of a multi-tenancy problem?

Hope that all makes sence

Colin G

解决方案

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 :)

这篇关于多个接口注入与城堡windsor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆