Creating a Generic Repository for our CRUD Operations with Entity Framework and MVC

Creating a Generic Repository for our CRUD Operations with Entity Framework and MVC

Creating a Generic Repository for our CRUD Operations with Entity Framework and MVC

When we are working with EF is very common that we have to write repeatedly code for the operations known like CRUD, this is the acronym for Create, Read,Update and Delete. In general these operations are always necessary for interacts with our Entities, it’s in this context that probably we would like to reduce the amount of code required for the management of these operations. When we build a generic repository we will have good and interesting benefits like:

. Reduction of Code
. Abstraction Layer for the management of CRUD operations
. We can take advantage of the Dependency Injection

The strategy for creating the generic repositories is the implementation by using Type parameters and interfaces, in this way we will work on any class that implements the interfaces.
For this article we are using only two entities: Employee and Department

Step 1 – Creating our Entities of Domain
These entities would inherit from BaseEntity with the purpose of identify these classes and the same will be able to use our generic repository.

ArticleRepository1

ArticleRepository2

ArticleRepository3

Step 2 – Defining the Interface for Context
The next step is the definition of the Interface, the purpose of this Interface is to provide an abstraction layer, it contains two main methods, SetEntityEF is a generic method which have a restriction to accept only entities that implements the BaseEntity class, later I will show you the logic for returning a DbSet instance for access to the entities.

ArticleRepository4

The signature of SaveChange method in the Interface has the intention of using together with generic in the implementation of this.

Step 3 – Creating the Repository Class
In this code we will build the repository class, this represent an abstraction for the operations in the Database (CRUD).

ArticleRepository5

You can see how we implement the generic code with a validation to accept only entities that inherit from BaseEntity; remember that this class is useful for identifying or mark a class like an Entity to use in the EF solution.

The fields are Interfaces type, in the case of _context is used with dependency injection, later we are going to see the benefit of DI, and the other property is interface type IDBSet that represents the collection of all entities in the context that is _context.

I would like to show you the content of IDbSet interface with .Net Reflector (courtesy by RedGate)

ArticleRepository6

Continuing with the code in this class, the below code is responsible for managing the Dependency Injection through the constructor, in the other hand the property Entities which will return the DbSet instances for access to entities of the given type.

ArticleRepository7

When you request any type of CRUD operation the property Entities is going to the method SetEntityEf and if doesn’t exists in the entities collection it’s added, this method is contained in the EntityContext class and invoke class base DbContext, the next image show you the code:

ArticleRepository8

The next methods corresponding the implementation of signature methods from IRepository, the methods are the CRUD operations.

ArticleRepository9

Is evident the use of Entities property for will be able to work together with the _context and persists the changes in the DB through the use of Entity Framework.

Step 4 – Defining the class that inherited of DBContext
To operate on the main operations and can create and manage the EF context is necessary to have the definition:

ArticleRepository10

The method to implement the interface was SetEntityEF (previously defined) and the rest of code is related with the initialization of the context in our example.

Step 5 – Defining the Services
We are going to use Dependency Injection, I would like to use Ninject, and this has a simplicity and many resources to learn how to use it

From the Nuget Administrator you need to load the Nugget Package

ArticleRepository11

For demonstrative purposes I chose the Ninject.Web.Common, after to install this option we can go to the folder App_Start and you can see a new file called NinjectWebCommon, the next image is an example:

ArticleRepository12

Internally this file has many options inside his configuration, I won’t deep in this topic, I recommend the next link for learning a little bit (http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api).
In our case only need to map the interfaces to their respective class how the next image show:

ArticleRepository13

The last step for testing our solution is the creation of a new Controller (EmployeeController) with a simple action and one operation (insert a new Employee).

ArticleRepository14

When you run you application this would save this new Employee entity in the Database, the most interesting is running the solution step by step to see how the app will resolve different aspects like Dependency Injection and the generic methods interacts with Entity Framework.

You can download the project in the next link, any comment or doubt don’t hesitate in touch with me. Good Code !!!

Leave a Reply

Your email address will not be published. Required fields are marked *