开发者

TypedFactoryFacility: how do i initialize an object with an inline parameter?

How can i produce the same output as specified below using the TypedFactoryFacility?

public class Something    
{
    public void Initialize(Whatever instance) {}
}

public interface ISomethingFactory
{
    Something Create(Whatever instance);
}

internal class SomethingFactory : ISomethingFactory
{
    private readonly IWindsorContainer _container;

    public SomethingFactory(IWindsorContainer container)
    {
        _container = container;
    }

    publ开发者_开发问答ic Something Create(Whatever instance)
    {
        Something item = _container.Resolve<Something>();
        item.Initialize(instance);
        return item;
    }
}

So I want to replace the manual factory with a proxy-generated ITypedFactoryFacility, but I cant find a way to invoke something on the resolved component after creation. I looked at commission-concerns, but you don't have a reference to the CreationContext from a custom commision concern so that won't work. I could of course move the dependency to the ctor and provide an ctor override, but I think properties are good when you want to convey non-optional dependencies.


You don't need to invoke stuff on the instance upon creation - Windsor will automagically inject stuff when the name of the parameter in the factory method signature matches something that can be injected - be it constructor paramaters or public properties... short example (using a public property):

interface ISomeFactory
{
    Something CreateSomething(object dataSource);
}

class Something
{
    public object DataSource { get; set; }
}

Given that these are registered like this:

container.Register(Component.For<ISomeFactory>().AsFactory(),
                   Component.For<Something>().Lifestyle.Transient)

you can resolve instances of Something like this:

var aintThatSomething = someFactory.CreateSomething(new [] {"ZOMG!", "w00t!"});

Remember that if something inside the burden associated with the instance of Something requires decommissioning, you need to provide an appropriate Release method on the factory as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜