Repository Pattern Step by Step Explanation

Repository Pattern Step by Step Explanation [What is Repository Pattern?]

Why need Repository Pattern?
It is not a good idea to access the database logic directly in the business logic (MVC controller). The Direct access to the data in the business logic creates multiple problems.

1.      Duplicate data access code throughout the business layer.
2.      Business logic cannot be tested properly.
3.      So may difficulty for Unit Test of the business logic.
4.      And so on.

What is Repository Pattern?

The Repository Pattern separates the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model.

The Repository Pattern hides the details of how exactly the data is being fetched or persisted from/to the database.

The Repository Pattern works between the data source layer and the business layers of the application.

In the Repository Pattern, the data access logic and the business logic talk to each other using interfaces.

The main purpose of the Repository Pattern is to hide the details of accessing data.
Repository Pattern use to improve the code's maintainability and readability by separating business logic from data access logic.

Using Repository Pattern we can identify problems at compile time instead of at run time.

Advantages of the Repository Pattern,
1.      No duplication of code.
2.      Centralizes the data access logic
3.      Database access logic can be tested separately without using external source.
4.      Domain driven development is easier.
5.      Provides a flexible architecture that can be adapted anywhere
6.      And so on.

Repository Pattern Examples,
namespace DEMO.Core.Repositories.Interface
    public interface IUserRepository
        /// <summary>
        /// </summary>
        bool UpdateAuthenticationToken(string EmailID, string AuthToken);

using DEMO.Core.Repositories.Interface;
using DEMO.Entities.Model;
using System.Linq;
using System.Data;
using System;

namespace DEMO.Core.Repositories
    public class UserRepository : GenericRepository<DEMO_Entities, Users>, IUserRepository
        /// <summary>
        /// </summary>
        /// <param name="EmailID"></param>
        /// <param name="AuthToken"></param>
        /// <returns></returns>
        public bool UpdateAuthenticationToken(string EmailID, string AuthToken)
            Users netUser = base.Context.Users.Where(x => x.UserEmail == EmailID).SingleOrDefault();
            netUser.AuthenticationToken = AuthToken;
            return true;

public ActionResult ForgotPassword(ForgotPasswordViewModel model)
    if (ModelState.IsValid)
      bool result = (new UserRepository()).UpdateAuthenticationToken(To, guid);
      if (result)
         EmailManager.SendEmail(subject, body, To);
     return View(model);


I hope you are enjoying with this post! Please share with you friends. Thank you!!

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2 Powered by Blogger.