In this article, I will help you to understand how to create a Repository Pattern that is mainly used for large enterprise application. The Repository pattern divides the application’s UI, components of data access and business logic into several layers which are easy to test and maintain.

A Employees application will be created which will help to get a better understanding of repository pattern. We can develop any project structure which can implement a repository pattern. Like for instance, we can develop the repository class in the folder of the MVC project, or we may also create diverse projects of the class library in a similar solution, or we may also use the onion architecture for implementing it which consists of different class of library projects and even the MVC project in the perfect solution. Hence, for simplicity, we would create the Repository class of library project with the MVC project.

In different patterns of Repository, we can also use the generic repository for implementing the CRUD operations which may be further used by the project entity and customized repository to perform particular actions of the entity.

To get started, create an ASP.NET MVC project( C# or vb.net), then add the Employees Class as shown below:

C#

    public class Employees
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Salary { get; set; }
        public string Occupation { get; set; }
        public string Department { get; set; }
    }

VB.NET

Public Class Employees
    Public Property Id As Integer
    Public Property Name As String
    Public Property Salary As Decimal
    Public Property Occupation As String
    Public Property Department As String
End Class

After this, we will create the interface in the project of the Repository class library. The interface consists of general operations of the database for the domain. Let us name this as IRepository.

C#

 public interface IRepository<T> where T : class
    {
        IEnumerable<T> SelectAll();
        T SelectById(int id);
        void Insert(T obj);
        void Update(T obj);
        void Delete(int id);
        void Save();
    }

VB.NET

Public Interface IRepository(Of T As  class)
    Function SelectAll() As IEnumerable(Of T)
    Function SelectById(ByVal id As Integer) As T
    Sub Insert(ByVal obj As T)
    Sub Update(ByVal obj As T)
    Sub Delete(ByVal id As Integer)
    Sub Save()
End Interface

Subsequently, we will now develop a repository class for implementing the IRepository interface. Let us name this class as a Repository.

C#

    public class Repository<T> : IRepository<T> where T : class
    {
        protected readonly DbContext database;
        public Repository(DbContext _database)
        {
            database = _database;
        }
        public IEnumerable<T> SelectAll()
        {
            return database.Set().ToList();
        }
        public T SelectById(int id)
        {
            return database.Set().Find(id);
        }
        public void Insert(T obj)
        {
            database.Set().Add(obj);
        }
        public void Update(T obj)
        {
            database.Entry(obj).State = EntityState.Modified;
        }
        public void Delete(int id)
        {
            T obj = database.Set().Find(Id);
            this.Remove(obj);
        }
        public void Save()
        {
            database.SaveChanges();
        }
    }

VB.NET

Public Class Repository(Of T As  class)
    Implements IRepository(Of T)
    
    Protected database As DbContext
    
    Public Sub New(ByVal _database As DbContext)
        MyBase.New
        Me.database = _database
    End Sub
    
    Public Function SelectAll() As IEnumerable(Of T)
        Return Me.database.Set.ToList
    End Function
    
    Public Function SelectById(ByVal id As Integer) As T
        Return Me.database.Set.Find(id)
    End Function
    
    Public Sub Insert(ByVal obj As T)
        Me.database.Set.Add(obj)
    End Sub
    
    Public Sub Update(ByVal obj As T)
        Me.database.Entry(obj).State = EntityState.Modified
    End Sub
    
    Public Sub Delete(ByVal id As Integer)
        Dim obj As T = Me.database.Set.Find(Id)
        Me.Remove(obj)
    End Sub
    
    Public Sub Save()
        Me.database.SaveChanges
    End Sub
End Class

We use the Entity framework code which is approach initially for data access, and hence we developed the DbContext class.

Now let create the class of EmployeesContext:

C#

    public class ClientContext : DbContext
    {

        public EmployeesContext()
            : base("name=EmployeesContext")
        {
        }

        public virtual DbSet<Employees> Employees { get; set; }
    }

VB.NET

Public Class ClientContext
    Inherits DbContext
End Class
Unknown
    
    Public Overridable Property Employees As DbSet(Of Employees)
        Get
        End Get
        Set
        End Set
    End Property

Using the repository

We now need to refer the library project of reference Repository class in the MVC Web project. To get this done, you need to right-click on the Employees Web project, and then you need to add the repository project of reference Employees. We would now scaffold the controller in the Employees’ project. For scaffolding, you need to right-click the controller folder and choose a new controller having the views by using the entity framework. Let us now name this as EmployeesController. For using the IRepository class for the operations of the database, we would soon create an object for the Repository class which also call the methods for Repository class. Let us now build and run this application.

Last modified: March 8, 2019

Comments

Write a Reply or Comment

Your email address will not be published.