How To Implement Repository Design Pattern in C# with Examples

Repository Pattern is a software design pattern that implements a logical layer between the business logic of an application and its data access mechanism. The Repository Pattern separates the business logic from the persistence of data. This leads to a cleaner and more flexible design, but it also requires well-defined interfaces for using the repository.

The Repository Pattern is used when you want to decouple your application logic from how data is stored and retrieved.

The Repository Pattern is an implementation of the Data Mapper Pattern. In this pattern, the object-oriented equivalent of tables and rows are encapsulated as objects. These objects represent a row in a table or any other relational structure. The methods that manipulate these objects are called CRUD (create, read, update, delete).
Let’s start the coding

public interface IRepository<T> where T : class
{
	void Add(T entity);
	void Delete(T entity);
	void Update(T entity);
	IQueryable<T> GetAll();
}

In general, repositories implement an interface or base class that define the public methods or properties of the repository object. A repository object is used to store and retrieve data from a data source. Plainly, it provides storage for anything related with the use of data. For example, a repository could be used to connect to a database, XML file, Web service, etc.

public class InMemoryRepository<T> : IRepository<T> where T : class
{
	private List<T> _entities = new List<T>();

	public void Add(T entity)
	{
		_entities.Add(entity);
	}

	public void Delete(T entity)
	{
		_entities.Remove(entity);
	}

	public IQueryable<T> GetAll()
	{
		return _entities.AsQueryable();
	}

	public void Update(T entity)
	{
		//do nothing
	}

}

Service layer is a layer that encapsulates the business logic of an application.

In a repository pattern, service layer is responsible for interacting with the database and providing data to other layers. Its main purpose is to hide the complexity involved in accessing data from the database and exposing it in an easy-to-use API. This way, if you want to change the data source, you don’t need to modify your code.

Todo.cs

public class Todo
{
	public int Id { get; set; }
	public string Name { get; set; }
	public bool IsComplete { get; set; }
}

TodoService.cs

public class TodoService
{
	private IRepository<Todo> _todoRepository;

	public TodoService(IRepository<Todo> todoRepository)
	{
		_todoRepository = todoRepository;
	}

	public void Add(Todo todo)
	{
		_todoRepository.Add(todo);
	}

	public void Delete(Todo todo)
	{
		_todoRepository.Delete(todo);
	}

	public IQueryable<Todo> GetAll()
	{
		return _todoRepository.GetAll();
	}

	public void Update(Todo todo)
	{
		_todoRepository.Update(todo);
	}
}

How to use

Our Implementation of Repository pattern is done now lets use the repository pattern. For the sake of simplicity I am using the console application.

Program.cs

void Main()
{

	var todoService = new TodoService(new InMemoryRepository<Todo>());
	todoService.Add(new Todo { Id = 1, Name = "Todo 1", IsComplete = false });
	todoService.Add(new Todo { Id = 2, Name = "Todo 2", IsComplete = false });
	todoService.Add(new Todo { Id = 3, Name = "Todo 3", IsComplete = false });

	var todos = todoService.GetAll();
	foreach (var todo in todos)
	{
		Console.WriteLine(todo.Name);
	}

	Console.ReadLine();
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru