I am using unity 2.0 with MVC3 and need some help understanding the LifeTimeManagers. I have read a lot of people using a custom LifeTimeManager that places items into the HTTPContext. This makes perfect sense because you only want the UoW around for the lifetime of the request in MVC. However, do I need the same lifetime manager for my repositories and services? I was looking at this post and noticed the same lifetime manager for the UoW, repositories, and services.
My Repositories depend on a UoW, and my Services depend on the Repositories. I am not sure what Unity does, by default, regarding a lifetime manager, but I did create a custom HttpContext manager and have had issues with it just being on the UoW. I have not put it on any of my services or repositories yet. I am wondering if I need to or if that is the best practice.
Simply use the Nuget package for unity.mvc3 and it contains a hierarchicallifetimemanager. When you register the types that need to be disposed in your mappings, it will dispose them.
DOn’t worry about doing anything directly with httpcontext, this is far easier.
See the section on
IDisposable dependencies
http://www.devtrends.co.uk/blog/integrating-the-unity.mvc3-1.1-nuget-package-from-scratch
For anything you want disposed explicitly (that implements IDisposable) use this lifetime manager: