0

I'm using StructureMap and I've configured ISession with HybridHttpOrLocalThreadStorage life cycle. New session is created and injected into controllers on per request basis.

Now, the question I have is about disposal. I've read number of articles presenting number of different approaches. Some people were doing it in controllers, some in repositories, some in http modules and others did it in Application_EndRequest() handler. Critique ranged from SRP violations to 'the one creating an object should be responsible for its disposal' to name a few.

So the bottom line is that:

  • common approach was to manually dispose these sessions - why? I have already configured my container to manage particular object's life cycle. Shouldn't it (i.e. IoC) manage it for me?
  • out of options available for disposal is handling it in Application_EndRequest() "the best" way of going about it?

For example, this article explains in length one available approach but the article itself is over 2.5 years old. Perhaps new version of StructureMap makes most of that implementation obsolete?

user981375
  • 639
  • 1
  • 5
  • 15
  • 1
    StructureMap should dispose of the session when the HTTP request ends, this is the purpose of that life-cycle. This is equivalent to disposing of the session in Application_EndRequest. – eulerfx Aug 29 '12 at 21:54
  • 1
    Related: http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why – Steven Sep 03 '12 at 12:43
  • @eulerfx : Are you sure that that's the case? So, all I'd have to do it pass Session to my controller, do whatever I have to do to/with data, call `SaveChanges` and exit, right? StructureMap would do the rest? I wasn't so sure after reading other people disposing their sessions manually in controllers, repositories, modules etc. Is there a way to verify that? – user981375 Sep 04 '12 at 19:07
  • @Steven - very good article. Thanks. I up-voted your answer there! – user981375 Sep 04 '12 at 19:23
  • I haven't used StructureMap so I can't be certain, but that is the job of an IoC container - disposing of components, so I'm guessing it should. – eulerfx Sep 04 '12 at 19:32

1 Answers1

2

If you are using RavenDB .net client you will be using DocumentStore and DocumentSession. Both of these object do a fair amount of work in the background; local caching to mention one thing. Just to keep things clean and efficient, each session should call session.dispose() when the work is done. documentStore.Dispose() should be called when the application ends.

Eric Rohlfs
  • 1,811
  • 2
  • 19
  • 29
  • This is perfectly clear. I have no problems configuring `DocumentStore` (singleton) and `DocumentSession` (per request). My question was about how to properly dispose `DocumentSession` after its work has been completed. – user981375 Sep 04 '12 at 18:59