ASP.NET MVC, Unity and IDisposable

I am using ASP.Net MVC 4, EF and Unity for DI. The UnitOfWork template is also used. Trying to find the best way to implement this. I have a code as shown below. The problem I ran into is Dispose () in the Business and Repository layer, which is never called, only the Destructor call, so the objects never seem to be deleted. Answer the following

  • I really need an IDisposable implementation at the Business and Repository level (if Unity already takes care of this)

  • What should I do to get the Dispose () call (should I add it to the Controller too, and also delete all other objects or use a specific LifeTime manager)

  • I must use each instance of Singleton or dispose of it in each request, as it is in the web environment.

Global.asax.cs:

private static IUnityContainer _unityContainer; protected void Application_Start() { _unityContainer = UnityBootstrapper.SetupUnity(); _unityContainer.RegisterType<IController, ProductController>("Product"); DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(_unityContainer)); } 

UnityBootstrapper.cs:

 public class UnityBootstrapper { public static IUnityContainer SetupUnity() { UnityContainer container = new UnityContainer(); container.RegisterType<IProductDbContext, ProductDbContext>() .RegisterType<IUnitOfWork, UnitofWork>(new InjectionConstructor(new ResolvedParameter(typeof(IProductDbContext)))) .RegisterType<IProductRepository, ProductRepository>() .RegisterType<IProductBusiness, ProductBusiness>(); } } 

ProductController.cs:

 public class ProductController : ControllerBase { private readonly IProductBusiness _productBusiness; public ProductController(IProductBusiness productBusiness) { _productBusiness = productBusiness; } //No Dispose for this } 

ProductBusiness.cs:

 public class ProductBusiness : IProductBusiness, IDisposable { private readonly IUnitOfWork _unitOfWork; private readonly IProductRepository _productRepository; public ProductBusiness(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; _productRepository = _unitOfWork.ProductRepository; } public override void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected override void Dispose(bool disposing) { if (!_isDisposed) { if (disposing) { if (_productRepository != null) _productRepository.Dispose(); if (_unitOfWork != null) _unitOfWork.Dispose(); } _isDisposed = true; } } ~ProductBusiness() { Dispose(false); } } 

ProductRepository.cs:

 public class ProductRepository : IProductRepository, IDisposable { private readonly IProductDbContext _context; public ProductRepository(IProductDbContext context) { if (context == null) throw new ArgumentNullException("context"); _context = context; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_isDisposed) { if (disposing) { if (_context != null) _context.Dispose(); } _isDisposed = true; } } ~ProductRepository() { Dispose(false); } } 
+6
source share
4 answers

You really do not need to delete objects unless you do something specific during the recycling process. DI containers take care of the life of the facility for you.

Sometimes a UoW implementation involves saving changes to a disposal. Try to avoid this. But if this is unavoidable, you can implement a factory for this:

 public interface IUnitOfWorkFactory { IUnitOfWork Create(); } public class UnitOfWorkFactory : IUnitOfWorkFactory { public IUnitOfWork Create() { return new UnitOfWork(); } } public class UnitOfWork : IUnitOfWork, IDisposable { // other implementation public void Dispose() { context.SaveChanges(); } } 

and then the consumer will do the following:

 public MyController(IUnitOfWorkFactory workFactory) { this.workFactory = workFactory; } public ActionResult DoSomething() { using(var uow = workFactory.Create()) { //do work } } 

This chapter of the DI book talks about disposable objects.

+2
source

While Unity will manage the deletion of your registered types , you still have to call dispose on your IOC container to do this for them.

Make it from Application_End and everything should be fine.

  protected void Application_End() { _unityContainer .Dispose(); } 
+1
source

I believe that if you install the correct LifetimeManager, then no, you do not need to implement IDisposable, as unity will handle the removal of your objects. This explains the different types of lifetime manager http://msdn.microsoft.com/en-us/library/ff660872(v=pandp.20).aspx .

Personally, I have not used singletones, but just let me create objects and then delete them in every request.

+1
source

You really do not need IDisposable for these classes, since the main task of IDisposable is to clean up unmanaged resources. Look at them:

http://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx

Proper use of the IDisposable interface

Using a singleton template for your container can be useful if you use your container in service location mode. However, there are other (and possibly better) ways to use Unity or other DI / IoC containers. There are several boot machines. For instance:

https://www.nuget.org/packages/Unity.Mvc/

0
source

All Articles