开发者

Add Factory Support for a Windsor Registration Dynamically

I have an interface

public interface IResolver<T>
{
    T Resolve();
}

I know this isn't the ideal (service locator is an anti-pattern and all), but in some cases it's still necessary (for example WCF service activation - I don't want to instantiate the channel until Resolve() is explicitly called...and in the case of BasicHTTP, the channel may need to be Resolve()'d multiple times, since the channel is closed after each Operation call). And having a factory injected beats having the container itself injected into a constructor, right?

I want any class that inherits IResolver to be registered as a Factory automatically when they are registered in the container. That is, I want to automatically call AsFactory on the ComponentRegistration before registration gets processed.

How might I accomplish this?

Thanks in adv开发者_高级运维ance.


Take a look at the Typed factory facility. With this you can just have a dependency on Func<ISomeService> and Windsor will fill in the proper function to resolve it automatically.


You could definitely do this with a custom facility, although there may be a better way. Sorry I don't have time at the moment to investigate further.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜