TypedFactoryFacility: passing a null argument

Consider the following test:

    [Test]
    public void Create_ServiceWithDynamicDependency_Created()
    {
        // arrange
        IWindsorContainer container = new WindsorContainer();

        container.AddFacility<TypedFactoryFacility>();

        container.Register(Component.For<IServiceFactory>().AsFactory());

        container.Register(Component.For<ServiceWithDynamicDependency>().LifeStyle.Transient);
        container.Register(Component.For<SomethingStatic>().LifeStyle.Transient);

        var factory = container.Resolve<IServiceFactory>();

        // act
        ServiceWithDynamicDependency serviceWithDynamicDependency = factory.Create(null);

        // assert
        Assert.That(serviceWithDynamicDependency, Is.Not.Null);
    }

This does not happen with the following exception: Failed to resolve the optional dependency for "Testing.Windsor.Factory.ServiceWithDynamicDependency" (Testing.Windsor.Factory.ServiceWithDynamicDependency). "SomethingDynamic" parameter of type "System.String"

If I replaced the assert part as follows:

        ServiceWithDynamicDependency serviceWithDynamicDependency = factory.Create("foo");

Component resolved as expected. Does anyone know of a workaround for this, or do I need hand factories that accept null arguments?

+3
source share
1 answer

Although you did not specify this, I assume that your component has one constructor that looks like this:

public ServiceWithDynamicDependency(string somethingDynamic) {}

, , - , .

, , null . Windsor null , .

null - .NET, " ". , null , Windsor .

, , , , , .

Windsor , , ( , ), , ( null), , .

+12

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


All Articles