0

I am using a generic repository pattern in asp.net mvc. I am familiar with repository pattern but not with Unit of work. I am reading several articles from google about this. But I am not sure if I use unit of work with asp.net mvc, where would I use Commit() method that will call the ef SaveChanges() method? Will I use it on repository layer or service layer or in Controller. Also, many others are saying that, Microsoft is already using Unit Of work built in with Entity Framework. So no need to use it separately or use it controller level or anywhere?

Thanks, Abdus Salam Azad.

1
  • I am voting to close this as there are already dozens of SO threads on that with different opinions. Commented Oct 15, 2016 at 13:16

2 Answers 2

1

Yes,you have to do it inside the UnitOfWork.cs class.

UnitOfWork.cs

 public sealed class UnitOfWork : IUnitOfWork
   {

       private DbContext _dbContext;

       public UnitOfWork(DbContext context)
       {         
          _dbContext = context;
       }

       public int Commit()
       {
           return _dbContext.SaveChanges();// Save changes 
       }

       public void Dispose()
       {
           Dispose(true);
           GC.SuppressFinalize(this);
       }

      private void Dispose(bool disposing)
       {
           if (disposing)
           {
               if (_dbContext != null)
               {
                   _dbContext.Dispose();
                   _dbContext = null;
               }
           }
       }
   }

After that you can call UnitOfWork's Commit method inside the Service Layer.

EntityService.cs

public abstract class EntityService<T> : IEntityService<T> where T : BaseEntity
   {
       IUnitOfWork _unitOfWork;
       IGenericRepository<T> _repository;

       public EntityService(IUnitOfWork unitOfWork, IGenericRepository<T> repository)
       {
           _unitOfWork = unitOfWork;
           _repository = repository;
       }  

     public virtual void Create(T entity)
           {
               _repository.Add(entity);
               _unitOfWork.Commit(); //saving point        
           }

}

You can learn more about this pattern : Generic Repository and Unit of Work Pattern

Sign up to request clarification or add additional context in comments.

2 Comments

What if you want to make multiple changes and save all the modifications at the end? How would you handle that? I'm in this situation and I cannot find a proper way to do the save method
@IonutEnache You need to use Transactions then: learn.microsoft.com/en-us/ef/core/saving/transactions
0

If you have multiple operation on a same transaction. Then after finishing all your operation, you will need to put your savechanges() finally. If it succeeded, all operation will be succeeded and if failed, all operations will be failed and reverted.

            _employeeService.UpdateVehicleDetail(model);
            _employeeService.UpdateLicenseDetail(model);
            await CurrentUnitOfWork.SaveChangesAsync();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.