views:

182

answers:

1

Issue

I am using Castle Windsor as an IoC container in a Castle Monorail project. I would like to inject the current instance of IRailsEngineContext into an object being resolved from the container in a controller.

Context

The object I would like inject the rails context into would be used to wrap the session object for the purpose of retaining the ids of previously viewed records. It would then be referenced to ensure that they aren't viewed again.

Alternate Solutions

I could pass the context to the methods with each call or inject it manually, but it would be nice to inject it directly from the container.

Question

I can't think of a way to inject the context within the container. Is there a way to do this? Does this even make sense?

+1  A: 
Container.Register(
    Component.For<IRailsEngineContext>()
             .UsingFactoryMethod(()=>MonoRailHttpHandler.CurrentContext)
             .LifeStyle.Transient
);

IRailsEngineContext - that's from an old version of MonoRail I guess. I'd advise you move to a newer one, the sooner the better.

Ken Egozi
btw - take notice not to inject this into Singleton services - these will resolve only once, and will use the first Context for concecutive requests.If you have to use the context on a Singlton, use MRHttpHandler.CurrentContext directly, or inject it with a custom IContextProvider that asks the handler for the current context each time.
Ken Egozi
@Ken: you can register this with a transient or PerWebRequest lifestyle and the lambda factory will get called as expected.
Mauricio Scheffer
@mausch - not if it is a dependency of a singleton.Say class Something { ... public Something (IEngineContext context); }and Something is registered as a Singleton.the first time Something is being resolved, the factory method will generate the correct context.on a consecutive request, the same ol' instance of Something will be served, holding a reference to the **old** context, (which is the *wrong* context)That's true for any non-singleton service that is a dependency of a singleton service
Ken Egozi
@ken: but that's about other components' lifestyles, not this one... If I have a transient component Foo that depends on IRailsEngineContext and I register IRailsEngineContext as singleton (like the code on your answer), then Foo will get an invalid IRailsEngineContext on a consecutive request. By registering IRailsEngineContext as transient this case is solved. Now if Foo is a singleton and it depends on a transient, that's Foo's problem.
Mauricio Scheffer
This is just like the case I blogged about a while ago: http://bugsquash.blogspot.com/2008/12/castle-windsor-factory-method-support.html which in turn gave origin to UsingFactoryMethod() only I used System.Web.Abstractions in this post instead of Monorail stuff.
Mauricio Scheffer
@mausch - oopise, my bad. I thought you mean the other way around, and I forgot to include the LifeStyle setting on registration. fixed, thx.
Ken Egozi
Is there a way to do this via XML configuration?
Jonathon Watney
@Jonathon: of course. The FactorySupport facility started in the old XML days.http://www.castleproject.org/container/facilities/trunk/factory/index.htmlhttp://blog.codiceplastico.com/?p=143
Ken Egozi