Windsor Setter Injection in code

I use Windsor to do IoC in our .Net project, but I'm having difficulty installing injections in the code.

I believe that this is due to the fact that I put on my components, because in the end I hope that the old project will be fully compatible with IoC and basically Unit Test (sweet dreams!).

This is how I register DAO:

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

And this is how I register components using DAO:

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

I was hoping that setters would load automatically in components, but the resources I found indicate that setters should be defined.

Unfortunately, I found examples of how to do this in the XML configuration, and not in the code.

, , , , . , , , , . , , - .

, Spring , bean, autowiring - .

- ?

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

:

, Dao, :

private IMyDao dao = null;

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

, , - , , .

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

Update:

, , , , :

//DAOs
container.Register(

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

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

, , set , DAO - null , set .

+3
1

Castle Windsor .

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

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


All Articles