python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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!!

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions