Data Access Using a Generic Repository in C#

The repository pattern is an abstraction layer which provides a well-organised approach to maintaining a separation between an applications data access and business logic layers.  This gives us the important advantages of making code more maintainable and readable and improving the testability of our code.  It also works great with dependency injection!

UPDATE: I have written a new article which provides a better implementation of a generic repository. Check it out here.

When I started looking at the repository pattern I found that a lot of the samples on the internet used explicitly typed repositories such as ICustomerRepository and IOrderRepository.  However for a website I’m currently working on all of my CRUD operations were pretty much the same and I wanted to reduce the amount of code I needed to write so I implemented a basic generic repository which would work with any of my data model classes.

The following code snippet  is the interface for the basic repository I’ve been using:

This interface only defines the most basic data access operations and should probably be expanded on to provide support for beginning, committing and rolling back transactions.

The following class is an implementation of IRepository which has been implemented to abstract access to an Entity Framework Code First data context:

There’s nothing particularly complicated about this class so  I’m just going to give a quick example on how to use it and leave it at that!  If you have the following class defined in an Entity Framework Code First data context:

You could query your customer data really easily using a LINQ expression or a lambda expression:

For more information about the repository pattern see: http://msdn.microsoft.com/en-us/library/ff649690.aspx