C# Repository Pattern - Design Patterns in Action

Repository Pattern

Repository pattern is one of the pattern which is most frequently used in Data access layer. This provides an abstraction to underlying database. This is also useful when database is not ready and we want to test the application by building an stub.

So repository pattern provides testability, abstration and  dependency injection.


Repository is defined as "an abstraction that provides us with persistence ignorance and a separation of concerns where the responsibility of persisting domain objects is encapsulated by the Repository that  leaving the domain objects to deal entirely with the domain model and domain logic."

I am going to show a sample of repository pattern implementation with c# and entity framework. Our entity framework DataContext object will be inside the constructor of the repository class. This will implement IDisposable interface to make sure that object is disposed properly in case of any failure or when object is no more required.

We will delcare some of the most frequenlty used operations in interface such as GetOne, GetMany, Filter, Save, Delete.

So IRepository and RepositoryImplementation Class will be the building blocks of repository pattern.More...

Learning C# Design Patterns Series -Why Design Patterns

Design Patterns - The Best Practices

Few years back, design patterns for .net developers were less known though it is widely being used by java developers.

So as .net languages and developers got more matured over the time, people started looking for .net code optimization, performance, best practices, extensibility and maintainability of the code side.

First obvious question about making use of design patters is why? Why we should implement design patterns in our solutions/applications.

Well I will try to read and make notes of why and how of design patterns and share in this series. So let begin.

1. Why design patterns? What benefit I will get?

Productivity: Design pattern helps to build an architecture which can enhance the productivity.

Extensibility: When a software is released and becomes successful and widely accepted it is obvious that it will receive lots of request change and updates. So if while building the application proper design patterns is implented it will be easy to supply those updates and request. Most design patterns promote the idea of coding against interfaces and base classes, which makes it easier to change and enhance your application at a later stage.

Simplicity: Our solution architecture should be simple, well thought out, clean, crisp and easy to understand. This can be acheived by knowing the design pattern in advance.More...

Posts By Month