views:

34

answers:

2

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 advance.

A: 

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.

Stuart Lange
+1  A: 

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.

Mauricio Scheffer
So I don't need to register something with AsFactory to have the factory facility use it?
JeffN825
Also, the documentation says I shouldn't use NoTrackingReleasePolicy with the facility and that I must use LifecycledComponentsReleasePolicy...but this really isn't possible, since I need my other transient components to release as normal (and not be held on to in the container forever).
JeffN825
I see now (verified with a WeakReference) that the latest version of Windsor no longer has the problem that it holds onto references of transient components...?
JeffN825