RajasekharwhY / Build-your-muscle

Learning concepts and sample practice code - C#, MVC, SQL etc..
2 stars 0 forks source link

Unit of work And Repository pattern #11

Closed RajasekharwhY closed 5 years ago

RajasekharwhY commented 5 years ago

The Repository and unit of work patterns are intended to create an abstraction layer between Data Access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and can felicitate unit testing or test-driven development (TDD).

Example project can be found on MS web site : https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

there are many ways 👍 you can use repository and unit of work patttern. 1. you can implement single repository for all entity types. or one for each (see point 2) 2.. create and implement repository for each entity type. (create repository interface and repository class)
3. you can include business logic in your repository or restrict it to data access logic. 4. you can also build an abstraction layer into your dbcontext class by using IDbSet interface there instead of dbset types for your entity sets. this approach to implementing abstraction layer shown in bleow link of MS. this can be on option to consider , not a recommendation for all scenarios. (https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application)

RajasekharwhY commented 5 years ago

Unit of work:

Unit of work is the concept related to the effective implementation of the repository pattern. Unit of work is referred to as a single transaction that involves multiple operations of insert/update/delete ..etc.. (https://www.c-sharpcorner.com/UploadFile/b1df45/unit-of-work-in-repository-pattern/). The above link has used repository pattern ( where single repository interface and implementation of that repository interface and explained by showing what happens with out Unit of work pattern and with unit of work pattern)

as we discussed in the first comment : We can implement the repository pattern by having single repository for each entity, and one repository for all the entitires. below link shows (http://www.janholinka.net/Blog/Article/9) the repository pattern for all the entities (meaning on repository for all entities and use of Unit of work pattern) with this kind of repository pattern.

RajasekharwhY commented 5 years ago

Example implantation will be in other repository.