There is a unitofWork class with one constructor.

public UnitOfWork(DbContext context) { _context = context; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _context.Dispose(); } } _disposed = true; } ~UnitOfWork() { Dispose(false); } 

UnityConfig.cs has the following code.

 container.RegisterType<DbContext, MyContext>(new PerResolveLifetimeManager()); container.RegisterType<IUnitOfWork, UnitOfWork>(); 

What tools can be used to do after I leave this constructor to dispose for the unitOfWork of each UnitOfWork (since it should be different)?

  public HomeController(IUnitOfWork unitOfWork,IUnitOfWork u) { _unitOfWork = unitOfWork; u2 = u; var a = unitOfWork == u; } 
  • using(unitOfWork)using(u){....} ? - tym32167
  • @ tym32167 So I will do it, not DI, it’s all there and the problem is that the dispose () call should occur automatically - Nikita
  • Why suddenly? How can DI know if you need an object or not? The maximum that can be calculated is that the GC will call this method and that only if the class has a correctly written finalizer, and it is not known when it will do it. But in general, since you seize some resource in your class, then free it in your class. - tym32167
  • Dismantle pattern - Tym32167
  • @ tym32167 I implemented it, but still the problem remains - Nikita

0