Repository Pattern in ASP.Net MVC

21Jan, 2019

The Repository Pattern is one of the most popular patterns to create an enterprise level application

It restricts us to work directly with the data in the application and creates new layers for database operations, business logic, and the application’s UI. If an application does not follow the Repository Pattern, it may have the following problems:

  • Duplicate database operations codes
  • Need of UI to unit test database operations and business logic
  • Need of External dependencies to unit test business logic
  • Difficult to implement database caching, etc.

Repository Pattern has many advantages:

  • business logic can be unit tested without data access logic
  • database access code can be reused
  • database access code is centrally managed so easy to implement any database access policies, like caching
  • It’s easy to implement domain logic
  • domain entities or business entities are strongly typed with annotations

The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. Repository directly communicates with data access layer [DAL] and gets the data and provides it to business logic layer [BAL]. Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD).

Repository provides an in-memory like collection interface for accessing domain objects. So as far as the consuming component is concerned, it uses the repository just like a collection when working with Domain objects.

The repository then neatly abstracts the internal mechanics of how the Add / Remove calls to the repository translate to the actual data access calls to the data store. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes.

Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × two =