温莎Setter注入代码 [英] Windsor Setter Injection in code

查看:195
本文介绍了温莎Setter注入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用温莎做的IoC在我们的.NET项目,但我有代码做setter注入的困难。

I'm using Windsor to do IoC in our .Net project, but I'm having difficulties doing setter injection in code.

我相信它来自于一个事实,我咧注册我的成分,因为最终我希望遗留项目将完全符合国际奥委会,大多进行单元测试(甜蜜的梦!)。

I believe it comes from the fact that I blanket register my components, since eventually I'm hoping that legacy project will be fully IoC compliant and mostly Unit Tested (sweet dreams!).

下面是如何我注册的DAO:

Here is how I'm registering the DAOs:

container
    .Register(AllTypes.FromAssemblyNamed("MyApp.Business")
    .Where(Component.IsInNamespace("MyApp.Business.Dao"))
    .WithService.DefaultInterface());



这里是我如何使用DAO的注册组件:

And here is how I'm registering the components using the DAOs:

container
    .Register(AllTypes.FromAssemblyNamed("MyApp.Business")
    .Where(Component.IsInNamespace("MyApp.MyComponentObject")));



我希望的制定者会得到的组件会自动回升,但我发现的资源似乎展现制定者需要定义。

I was hoping that the setters would get picked up automatically in the components, but the resources I found seem to show the setters need to be defined.

不幸的是,我只发现了怎么办,在XML配置,而不是在代码示例。

Unfortunately I've only found examples of how to do that in the configuration XML, not in the code.

我发现了如何将参数添加到代码中的一个组成部分,但似乎有限制。看起来它必须是一个字符串,即使不是这样,好像它会迫使我一个,而不是仅仅注册毯一宣布这些组件。如果我不得不做,该代码将成为巨大的,不知何故温莎将失去它的一些吸引力的我。

I found how to add a parameter to a component in the code but there seem to be limitations. Looks like it has to be a string and even if that's not the case, seems like it would force me to declare those components one by one instead of just blanket registering. That code would become huge if I had to do that and somehow Windsor would lose some of its appeal to me.

我记得使用Spring是,如果一个bean被宣布自动装配会注入它没有任何问题。

I remember using Spring were if a bean was declared autowiring would inject it without any issue.

我缺少的东西瓦特/温莎在这里?

Am I missing something w/ Windsor here?

===== ==========

===============

更多信息:

在将使用该组件类这里的道组件是如何声明二传手:

In the component class that will use the Dao component here is how I declare the setter:

private IMyDao dao = null;

public IMyDao Dao
{
  set { this.dao = value;  }
}



主要的原因我想设定器注入是因为我不能改变构造函数,而不必在传统的应用程序相当的影响。

The main reason I want to setter inject is because I can't change the constructors without having a pretty impact on the legacy app.

===============

===============

更新:

注释掉了我大部分的注册码,左眼前这个,看看是否能在简单的情况下工作,它没有

Have commented out most of my registration code, left just this to see if that works in a simple case scenario and it did not:

//DAOs
container.Register(

Component.For<IMyDao>()
  .ImplementedBy<MyDao>()
    .Named("myDao"),

//Components
Component.For<MyComponentObject>()
  .Parameters(Parameter.ForKey("Dao").Eq("myDao")));



我把这些代码被轰炸和一个破发点也对属性和DAO依赖的仍然是在我的组件和的设置行该属性实际上是永远不会被调用。

I put a break point where the code is bombing and also on the set of the property and DAO dependency is still null in my component and the set line of the property is actually never called.

推荐答案

温莎城堡的setter注入根本就正常工作。

Castle Windsor's setter injection simply does work properly.

[TestClass]
public class SetterInjectionTest
{
    [TestMethod]
    public void TestMethod1()
    {
        var container = new WindsorContainer();
        container.Register(Component.For<IServiceB>().ImplementedBy<ServiceB>());
        container.Register(Component.For<IServiceA>().ImplementedBy<ServiceA>());
        var serviceA = container.Resolve<IServiceA>();

        Assert.IsTrue(serviceA.IsPropertyInjected());
    }
}

public class ServiceA : IServiceA
{
    public IServiceB B { get; set; }

    public bool IsPropertyInjected()
    {
        return B != null;
    }
}

public interface IServiceA
{
    bool IsPropertyInjected();
}

public class ServiceB : IServiceB{}
public interface IServiceB{}

这篇关于温莎Setter注入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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