jeudi 17 juin 2021

How to implement SaveChanges() method at Service layer level in Asp.Net Core MVC app

Inside the project I work on (ASP.NET Core with EF), I have implemented Repository Pattern with UnitOfWork, and in my UnitOfWork class I have the SaveChanges() method.

Now I must add a new layer of services which will contain the app logic and the calls to the repository classes. All the things were good until I reached the SaveChanges() method.

I don't want to call UnitOfWork.SaveChanges() at the end of every method from my service classes. Another thing that I want to avoid is to call the SaveChanges() when all the modifications are done, lets say EmployeeService.SaveChanges(), but the changes are made by multiple services, eg: ShopService, ClientService and EmployeeService. To call the EmployeeService.SaveChanges() for me it seems contra intuitive because the changes that should be saved are also related to ShopService and ClientService, not just with the service class from where the save method is called.

In order to make a summary of my problem, I could say that I'm asking for a solution to save the changes at the service layer level when I have multiple changes made by multiple services. I want to save them just once at the end.

Aucun commentaire:

Enregistrer un commentaire