This article will introduce:
- how to create a generic repository and using UnitofWork
- how to use Ninject to inject context into controller
- how to perform unit test with Moq on the UnitOfWork using generic repository
Prerequisite: get below package from NuGet
- Moq
- Ninject
In this example, I will use Product and Category models.
Step1 : Create model class
Step 2: Create Db Context
In Web.Config, configure the default connection string. In my case, I set the "Initial Catalog=MvcRepository" in the connection string.
Create a "ProductDBContext" class referenced from DbContext

Step 3: Create Generic Repository Interface

To understand more about constraint on type parameters, view here
Step 4: Create Repository Class
The repository class will implement the IRepository interface.
Repository class has two generic parameters: TEntity, and TContext.
The purpose of adding TContext is to enable the repository class used by different DbContext.
Step 5: Create the Product and Category Interface and Repository
- The interface is created for each model. In each interface, you can add any required functions for specific purpose.
- Each repository class of each model will implement the new interface of the model. And all of the classes inherit from the Repository class (which implemented the IRepository interface).
- In the "ProductRepository" class below, we pass the "Product" class as the TEntity to repository. And keep the TContext as the generic param (this param will be passed from the unitofwork, unit test, or any controller/ class that invoke ProductRepository).


Step 6: Create UnitOfWork
Using UnitOfWork is to ensure that all the DbSets are saved at the same time (in case you need to update more than one table/ dbSet in the transaction).
In this example, I will use UnitOfWork to wrap the Product and Category repositories.
First, create an interface for unitofwork. The interface implements the IDisposable interface. The "SaveChange()" method is for saving data to the context.
Next, create a UnitOfWork class to implement the IUnitOfWork interface. Now, the "ProductDbContext" is used and passed to the Product and Category repositories.
Step 7: Using UnitOfWork in Controller
Create Product controller by using scaffold and replace the origional code by using UnitOfWork.
We inject the UnitOfWork to the controller via constructor. This constructor is used for passing the context from unit test.
Note: if you run the application now, you will get error. Because, MVC needs a parameterless constructor. We are going to manage this by using Ninject in next step.
Step 8: Ninject IoC
After installing Ninject from NuGet, you should get "NinjectWebCommon.cs" in the App_Start directory.
In the "RegisterServices" method, bind the UnitWork to IUnitWork. This statement tells the system that when the contoller is looking for IUnitOfWork, it will pass UnitOfWork.

Now, the application should be successfully built and work as normal.
We have completed the application by using generic repository, unitofwork, and Ninject.
Next step, we will see how we can perform unit test with the generic repository.
Step 9: Unit Test
- how to create a generic repository and using UnitofWork
- how to use Ninject to inject context into controller
- how to perform unit test with Moq on the UnitOfWork using generic repository
Prerequisite: get below package from NuGet
- Moq
- Ninject
In this example, I will use Product and Category models.
Step1 : Create model class
Step 2: Create Db Context
In Web.Config, configure the default connection string. In my case, I set the "Initial Catalog=MvcRepository" in the connection string.
Create a "ProductDBContext" class referenced from DbContext
Step 3: Create Generic Repository Interface
To understand more about constraint on type parameters, view here
Step 4: Create Repository Class
The repository class will implement the IRepository interface.
Repository class has two generic parameters: TEntity, and TContext.
The purpose of adding TContext is to enable the repository class used by different DbContext.
- The interface is created for each model. In each interface, you can add any required functions for specific purpose.
- Each repository class of each model will implement the new interface of the model. And all of the classes inherit from the Repository class (which implemented the IRepository interface).
- In the "ProductRepository" class below, we pass the "Product" class as the TEntity to repository. And keep the TContext as the generic param (this param will be passed from the unitofwork, unit test, or any controller/ class that invoke ProductRepository).
Step 6: Create UnitOfWork
Using UnitOfWork is to ensure that all the DbSets are saved at the same time (in case you need to update more than one table/ dbSet in the transaction).
In this example, I will use UnitOfWork to wrap the Product and Category repositories.
First, create an interface for unitofwork. The interface implements the IDisposable interface. The "SaveChange()" method is for saving data to the context.
Next, create a UnitOfWork class to implement the IUnitOfWork interface. Now, the "ProductDbContext" is used and passed to the Product and Category repositories.
Step 7: Using UnitOfWork in Controller
Create Product controller by using scaffold and replace the origional code by using UnitOfWork.
We inject the UnitOfWork to the controller via constructor. This constructor is used for passing the context from unit test.
Note: if you run the application now, you will get error. Because, MVC needs a parameterless constructor. We are going to manage this by using Ninject in next step.
Step 8: Ninject IoC
After installing Ninject from NuGet, you should get "NinjectWebCommon.cs" in the App_Start directory.
In the "RegisterServices" method, bind the UnitWork to IUnitWork. This statement tells the system that when the contoller is looking for IUnitOfWork, it will pass UnitOfWork.
Now, the application should be successfully built and work as normal.
We have completed the application by using generic repository, unitofwork, and Ninject.
Next step, we will see how we can perform unit test with the generic repository.
Step 9: Unit Test
No comments:
Post a Comment