Hey there! So, I really like Generic Repositories. They are a very useful design pattern and are enjoyable to use once configured properly. However, when I first started learning about this design pattern I found a lot of the tutorials out there to be overly convoluted. The goal of this article is to get you up and running with a simple but powerful Generic Repository.
This tutorial assumes general working knowledge of Entity Framework and is based around MVC, though it could be applied to other frameworks and patterns.
As a side note or disclaimer, most of the code shown here is actually slightly modified code from the open source NopCommerce ecommerce platform. I think NopCommerce has a great implementation of a generic repository, so why rewrite the whole thing? The whole point of this in the first place is to avoid rewriting code. Don’t worry if you don’t even know what NopCommerce is – the code here is stand alone and has no dependency on the platform. I have simply extracted some of it out and simplified it for a tutorial.
Let’s quickly review the purpose and benefits of a Generic Repository.