Lifetime - Between Transient and Singleton? PerWebRequest maybe?

Jul 13, 2008 at 8:54 PM
Hi there,

I have an IDataContext object that is injected into my GenericService class.

Optionally, I have SpecializedService classes, that may contain (not inherit from) the GenericService class.  This SpecializedService class also has an IDataContext object.

Now, I don't want a Singleton for my DataContext, but Transient causes Unit of Work problems.

For scenarios like so:

SpecializedMethod()
{
   GenericService.DoSomething();
   SpecializedService.DoSomething();
}

The DataContext is not shared.

I'm wondering what my options are here, and before I go write something, I wanted to make sure there wasn't something already available that I missed.

Something like Windor's PerWebRequest might work, but I'm thinking of a more comprehensive solution.  Basically, what I want to say, is that within the context of SpecializedService1, share the Same DataContext with GenericService.  If SpecializedService2 is instantiated, then create a NEW DataContext, but SHARE it in the event that GenericService methods get called during the lifetime of SpecializedService2.

Clear as mud?

Thanks.
Jul 20, 2008 at 5:46 PM


skbach wrote:

Clear as mud?

Hi skbach,

I used your requirement to test Chris Tavares's new lifetime manager documentation (recent post by him).    If I understood your requirement correctly, which I think I did, you'll find "a" solution HERE.   Please let us know if you find a better / different solution - I'd be interested in seeing it.
Aug 21, 2008 at 1:01 AM
Hi BillKrat,

I've taken a look at that post, but in a web environment I think a PerRequest or PerSession is more useful than PerThread, as multiple requests will be executed in the same Thread, unless I'm mistaken on that.

Thanks.
Aug 21, 2008 at 5:48 AM
Hi skbach,  I see your point... bad assumption on my part that request were on separate threads; in hindsight (after short research) it seems obvious that you would want to pool threads for performance reasons.

Did you find a solution?
Aug 21, 2008 at 5:24 PM
Not yet.

I've reduced the immediate need for this though as some internal refactorings have meant that DataContext's are higher up the chain and so will probably only be resolved once per request anyway.  I'm hoping that the Unity team will be addressing this issue in coming builds, as it seems to be a popular request.

Thanks.