Setter injection on an existing service instance in Castle Windsor
Does Castle Windsor support applying setter injection to existing service instances? I have a case in which I have no control over the creation of certain class instances while I do need to resolve dependencies for them. This rules out constructor in开发者_Python百科jection but leaves the door open for setter injection since I can intercept the new instances right after they're created.
I know Castle Windsor supports setter injection out of the box, I just don't know whether its possible (and if so: how) to leverage it explicitly without using the Resolve
method.
I know as well that such thing is possible in StructureMap, using the ObjectFactory.BuildUp
method so that raises my hope of finding an equivalent in Castle Windsor.
In case you are wondering, I'm applying dependency injection to pages in ASP.NET WebForms, providing a custom PageHandlerFactory
. I don't want to go down the path of trying to create the page handler myself, I should leave that to the base implementation in the PageHandlerFactory
class.
public class CustomPageHandlerFactory : PageHandlerFactory
{
public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
{
IHttpHandler handler = base.GetHandler(context, requestType, virtualPath, path);
Page page = handler as Page;
if (page == null)
{
return handler;
}
// Resolve dependencies using setter injection...
return page;
}
}
See the bottom of this article:
http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/
Jeremy provides an extension method for the Windsor Container that implements property injection.
Does Castle Windsor support applying setter injection to existing service instances?
No. Check out the FAQ for rationale.
I'm applying dependency injection to pages in ASP.NET WebForms,
See this question.
Check out Neal Shawn's ASpNetWindsorModule.
精彩评论