Reputation: 4171
I want to add per session lifestyle for one of my controllers in an ASP.NET MVC app but it currently has no such option. I've searched the stackoverflow and found the next solution
public class PerSessionLifestyleManager : AbstractLifestyleManager
{
private readonly string PerSessionObjectID = "PerSessionLifestyleManager_" + Guid.NewGuid().ToString();
public override object Resolve(CreationContext context)
{
if (HttpContext.Current.Session[PerSessionObjectID] == null)
{
// Create the actual object
HttpContext.Current.Session[PerSessionObjectID] = base.Resolve(context);
}
return HttpContext.Current.Session[PerSessionObjectID];
}
public override void Dispose()
{
}
}
But I want to be able to write somethink like
cr => cr.LifeStyle.PerSession.Named(cr.Implementation.Name)
I use Castle Windsor 3.0 and found that LifestyleType enum is contained inside Castle.Core namespace, it is being used by the DefaultKernel. My suggestion is to override the DefaultKernel but I do not really know how to do it bug free and seemlessly as if PerSession lifestyle if shipped with the dll.
Upvotes: 2
Views: 2331
Reputation: 27374
So there are two things that you're asking about.
First is how to implement the lifestyle. Good starting point would be to look at how per-web-request lifestyle is implemented (use Scoped lifestyle with custom scope and scope accessor)
Second, how to surface that in the API. what I recommend is to have an extension method that encapsulates your lower level call to LifestyleScoped<YourCustomScopeAccessor>()
with LifestylePerSession()
similar to how WCF Facility does it.
Upvotes: 2