Windsor Setter Injection in code

江枫思渺然 提交于 2019-12-10 20:31:59

问题


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!).

Here is how I'm registering the DAOs:

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

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.

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.

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

Am I missing something w/ Windsor here?

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

More information:

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.

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

Update:

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")));

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.


回答1:


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{}


来源:https://stackoverflow.com/questions/4190292/windsor-setter-injection-in-code

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