How to Implement Repository Design Pattern in C#

Design patterns are used as a solution to recurring problems in your applications, and the repository pattern is one of the most widely used design patterns. It will persist your objects without needing to know how those objects would actually be persisted in the underlying database, i.e. without having to worry about how data persistence happens underneath. The knowledge of this persistence, i.e. the persistence logic, is encapsulated in the repository.

Essentially, the repository design pattern makes it easy to decouple the business logic and data access layers in your application, the former having no knowledge of how data persistence would actually happen.

By using the Repository design pattern, you can hide the details of how data is ultimately stored or retrieved to and from the data store. This data store can be a database, an xml file, etc. You can apply this design pattern to even hide how data exposed by a web service or ORM is accessed. Martin Fowler states, “Intermediate between the domain and data mapping layers using a collection-like interface to access domain objects.”

A repository is defined as a collection of domain objects that reside in memory. The MSDN states: “Use a repository to separate the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model. The business logic should be independent of the data type that comprises the source layer data source layer. For example, the data source layer can be a database, a SharePoint list, or a web service.”

Implementing the repository design pattern in C#

In this section, we’ll explore how to program the repository design pattern. In our implementation of the Repository design pattern, participating types include the following:

  1. IRepository interface — this interface is the base type for all repository types
  2. Repository class — this is the generic Repository class
  3. One or more Repository classes that implement the IRepository interface

Now let’s dive into some code. The following class shows how you can define a base entity class from which all your entity classes should be derived.

public abstract class EntityBase

   {

       public Int64 Id { get; protected set; }

   }

The class is defined as abstract with a single field – named “Id”. The “Id” field is common to all the entities you typically use, isn’t it? This is what the generic IRepository interface would look like.

public interface IRepository where T : EntityBase

   {

       T GetById(Int64 id);

       void Create(T entity);

       void Delete(T entity);

       void Update(T entity);

   }

The Repository generic class implements the IRepository interface and implements the members of the interface.

public class Repository : IRepository where T : EntityBase

   {

       public void Create(T entity)

       {

           //Write your logic here to persist the entity

       }

       public void Delete(T entity)

       {

           //Write your logic here to delete an entity

       }

       public T GetById(long id)

       {

           //Write your logic here to retrieve an entity by Id

           throw new NotImplementedException();

       }

       public void Update(T entity)

       {

           //Write your logic here to update an entity

       }

   }

Create repositories for specific classes

If you want to create a repository for a specific entity, you need to create a class that implements the IRepository generic interface. The following code listing shows how this can be achieved.

public class CustomerRepository : IRepository

   {

       //Write your code here to implement each of the methods of the IRepository interface.

   }

Similarly, if you were to create a ProductRepository, you would first create a Product entity class that extends the EntityBase class.

public class Product: EntityBase

   {

       public string ProductName { get; set; }

       public string Category { get; set; }

   }

The ProductRepository class must implement the IRepository generic interface. This is what the ProductRepository class would look like.

public class ProductRepository : IRepository

   {

       //Write your code here to implement each of the methods of the IRepository interface.

   }

Copyright © 2016 IDG Communications, Inc.

Abdul J. Gaspar