Most people think that IOC containers are for large applications only. They are wrong, as they can be used in any size application; i.e, from small to large, because this will make your application maintainable and improve the quality of your application.
Some people have another question in mind, which is: How much does it cost to get IOC Containers?
Oh, most IOC containers are free to download and use.
In this, Ninject is completely free to use for personal and commercial projects.
You can check here for more information.

What is Ninject framework?
The Ninject framework is an ultra-lightweight framework from Ninject, which will help us to resolve dependency.
What is Dependency injection?
Dependency injection is a design pattern, which is used to remove hard code dependency.
Types of Injection
- Constructor Injection
- Property Setter Injection (Properties)
- Method Injection
If you have a look at the internet, you will find most people show an example of Constructor Injection. In this article, I will show you how to use all the three types of injectios.
Tools Required for Demo,
- Visual Studio 2012
- SQL Server 2008
Creating Application
Open Visual Studio IDE on the start page. Select new project.

Figure 1. Start page
After selecting new project link, a new project dialog will appear. Inside it, select templates - Visual C# needs to be selected inside this. Select Web, after which you will see the various project templates (Webforms , MVC , Asp.net AJAX Server control). In this, select “ASP.NET MVC 4 Web Application. ” After selecting, just name your project as “MvcNInject” and finally click the OK button to create the project.
Selecting Templates

Figure 2. Selecting Template
After clicking OK button, another project template selection wizard will pop up with the name “New ASP.NET MVC 4 Project.” In this template, select basic template, set view engine as Razor and we are not going to create unit testing for this project. Hence, do not check this option and finally click OK button and it's done.

Figure 3. Selecting MVC 4 Project
After selecting all the options, as told above, click OK button and your project will be created.
Project structure after creating MvcNInject project

Figure 4. Project structure
After creating our project, we are going to add class Library to the project, so that it feels like a real application when we create it.
Adding [Class library] to Web project
We are going to Add three [Class libraries].
- MvcNInject.Model
- MvcNInject.Interface
- MvcNInject.Concrete
MvcNInject.Model [Class library] will contain all the models.
MvcNInject.Interface [Class library] will contain all the interfaces.
MvcNInject.Concrete [Class library] will contain all the concrete classes.
Let’s start doing it.
Adding MvcNInject.Model [Class library]
First, we are going to add Model layer with the name MvcNInject.Model Layer.
Tips
You might think since we have Model folder in the project, then why are we adding MvcNInject.Model layer? It is because if we have a large project, it is better to move Model into [Class library] and add reference to the main project.
To add [Class library], just right click on your project from the list and select Add, followed by selecting the new project.

Figure 5. Steps to add Class library
After selecting the new project, a new dialog will pop up with the name Add New Project. As shown below, select the type Visual C# inside it. Select Windows and from the template, select [Class Library] and then name your project as MvcNInject.Model and click OK button.

Figure 6. Adding MvcNInject.Model [Class library]
Project structure after Adding MvcNInject.Model

Now we are going to Add Interface to make your application's loose couple.
Adding MvcNInject.Interface [Class library]
We are going to add MvcNInject.Interface in a similar way as we have added MvcNInject.Model [Class library].
To add [Class library] just right click on your project from List and select Add - then select New Project.
After selecting a new project, a new dialog will pop up with the name Add new Project from the panel and select type Visual C#. Inside it, select Windows. From the template, select [Class Library] and then name your project as MvcNInject.Interface and click OK button.

Figure 7. Adding MvcNInject.Interface [Class library]
Project structure after Adding MvcNInject.Interface

Now, we are going to add concrete classes, which will implement an interface.
Adding MvcNInject.Concrete [Class library]
We are going to add MvcNInject.Concrete in a similar way as we have added MvcNInject.Interface [Class library].
To add [Class library], just right click on your project from the list, select Add followed by selecting the New Project.
After selecting New Project, a new dialog will pop up with the name Add new Project from the panel. Select type Visual C#; inside it, select Windows from the template, select [Class Library], name your project as MvcNInject.Concrete and click OK button.

Figure 8. Adding MvcNInject.Concrete [Class library]
Project structure after Adding MvcNInject.Concrete

Now, we have completed adding all [Class library] in the project.
Adding Model in MvcNInject.Model
Next, we are going add a model in [MvcNInject.Model] with the name Event.
For adding model, just right click on MvcNInject.Model, and select Add, followed by selecting the class inside it.
After clicking on the class, add new item and a dialog will pop up with the class. Select and it will ask for the class name. Here, we are going to enter the class name as an “Event”.

After adding the model, let’s add some properties to it.
Adding Properties to EventModel
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvcNInject.Model
- {
- public class EventModel
- {
- [Key]
- public int EventID { get; set; }
- public string EventName { get; set; }
- public string EventStarts { get; set; }
- public string EventEnds { get; set; }
- public string EventLocation { get; set; }
- }
- }
Adding Interface in MvcNInject.Interface
We are going add an interface in MvcNInject.Interface.
To ad an interface, just right click on MvcNInject.Interface followed by selecting add. Inside it, select Add New Item menu.
After clicking Add New item, a new Add New item dialog will pop up. Inside it, select an interface and then we are going to name an interface as an “IEvent”.
sFigure 9. Adding Interface in MvcNInject.Interface

After adding an interface, we are going to add the reference of MvcNInject.Model in MvcNInject.Interface to access the model inside an interface.
Adding Reference of MVCNInject.Model (Event) in MVCNInject.Interface (IEvent)
To add a reference, just right click on MvcNInject.Interface [Class Library], followed by selecting add reference. After selecting, a new dialog will pop up with the name Reference Manager. Inside it, select MvcNInject.Model and click OK button.

Figure 10. Adding reference of MvcNInject.Model to MvcNInject.Interface
- using MvcNInject.Model;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvcNInject.Interface
- {
- public interface IEvent
- {
- void Add(EventModel _Event); // Create New Event
- void Update(EventModel _Event); // Modify Event
- void Delete(EventModel _Event); // Delete Event
- EventModel GetById(int id); // Get an Single Event details by id
- IEnumerable<EventModel> GetAll(); // Gets All Event details
- }
- }
Adding Class in MVCNInject.Concrete
We are going to add class in MvcNInject.Concrete in [Class library] in the project.
To add the model, just right click on MvcNInject.Concrete. Select Add - inside that Select Class.
After Clicking on class, add new item and a dialog will pop up with class select and ask for class name; here we are going to enter class name as “EventConcrete”.

Adding Reference of MvcNInject.Interface (Event) in MvcNInject.Concrete (IEvent)
To Add Reference just right Click on MvcNInject.Concrete [Class Library] then select Add Reference; after selecting a new dialog will pop up with the name Reference Manager; inside that select MvcNInject.Interface and MvcNInject.Model. Both are required, then click on OK button.

Figure 11. Adding reference of MvcNInject.Interface and MvcNInject.Model to MvcNInject.Concrete
Now we have finished adding Reference, now let’s implement all methods declared in IEvent Interface in EventConcrete class.
EventConcrete class will implement IEvent interface
- using MvcNInject.Interface;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvcNInject.Concrete
- {
- public class EventConcrete : IEvent
- {
- }
- }
Adding Dapper ORM to MvcNInject.Concrete [Class Library]
Now to perform all database operations such as CRUD I am going to use Dapper ORM.
We are going add Reference of Dapper ORM from NuGet package manager and we are going add it in MvcNInject.Concrete [Class Library].
To open Manage NuGet Package dialog just right click on MvcNInject.Concrete [Class Library] and from the list select Manage NuGet package.
Snapshot of Manage NuGet package dialog.
In search box enter Dapper. And select Dapper dot net as show below and click on Install button.

Figure 12. Adding DapperORM from NuGet
Project structure after Adding Dapper

After adding Dapper ORM next we Create Database and Table and Store procedure for performing CRUD operation.
Database Part
Here I am going to show the database part.
I have created a Database with the name [EventDB].

After that I have created a table inside EventDB database with name [EventTB].

Figure 13. Table structure
We are going use stored procedure for performing CRUD operation.
Below are the names of the stored procedure which I have created. I have provided this stored procedure to download.

Connection string used
- <connectionStrings>
- <add name="DefaultConnection"
- providerName="System.Data.SqlClient"
- connectionString="Data Source=sai-pc;Database=EventDB;UID=sa;Password=Pass$123" />
- </connectionStrings>
EventConcrete class will implement IEvent interface
In this part I am implementing all methods which are declared in interface and using Dapper to perform Crud operation.
- using Dapper;
- using MvcNInject.Interface;
- using MvcNInject.Model;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvcNInject.Concrete
- {
- public class EventConcrete : IEvent
- {
- public void Add(EventModel Event)
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
- {
- con.Open();
- SqlTransaction sqltans = con.BeginTransaction();
- var param = new DynamicParameters();
- param.Add("@EventID", Event.EventID);
- param.Add("@EventName", Event.EventName);
- param.Add("@EventStarts", Event.EventStarts);
- param.Add("@EventEnds", Event.EventEnds);
- param.Add("@EventLocation", Event.EventLocation);
- var result = con.Execute("sprocEventTBInsertUpdateSingleItem",
- param,
- sqltans,
- 0,
- commandType: CommandType.StoredProcedure);
- if (result > 0)
- {
- sqltans.Commit();
- }
- else
- {
- sqltans.Rollback();
- }
- }
- }
- public void Update(EventModel Event)
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
- {
- con.Open();
- SqlTransaction sqltans = con.BeginTransaction();
- var param = new DynamicParameters();
- param.Add("@EventID", Event.EventID);
- param.Add("@EventName", Event.EventName);
- param.Add("@EventStarts", Event.EventStarts);
- param.Add("@EventEnds", Event.EventEnds);
- param.Add("@EventLocation", Event.EventLocation);
- var result = con.Execute("sprocEventTBInsertUpdateSingleItem",
- param,
- sqltans,
- 0,
- commandType: CommandType.StoredProcedure);
- if (result > 0)
- {
- sqltans.Commit();
- }
- else
- {
- sqltans.Rollback();
- }
- }
- }
- public void Delete(EventModel Event)
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
- {
- con.Open();
- SqlTransaction sqltans = con.BeginTransaction();
- var param = new DynamicParameters();
- param.Add("@EventID", Event.EventID);
- var result = con.Execute("sprocEventTBDeleteSingleItem",
- param,
- sqltans,
- 0,
- commandType: CommandType.StoredProcedure);
- if (result > 0)
- {
- sqltans.Commit();
- }
- else
- {
- sqltans.Rollback();
- }
- }
- }
- public EventModel GetById(int id)
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
- {
- con.Open();
- var param = new DynamicParameters();
- param.Add("@EventID", id);
- return con.Query<EventModel>("sprocEventTBSelectSingleItem",
- param,
- null,
- true,
- 0,
- commandType: CommandType.StoredProcedure).SingleOrDefault();
- }
- }
- public IEnumerable<EventModel> GetAll()
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
- {
- con.Open();
- var param = new DynamicParameters();
- return con.Query<EventModel>("sprocEventTBSelectList",
- null,
- null,
- true,
- 0,
- commandType: CommandType.StoredProcedure).ToList();
- }
- }
- }
- }





















vijayakumar sPosted Dec 19, 2018, 8:15 AM
When I try to add reference for MvcNInject.Concrete in MvcNInject (MVC web project ), I am getting circular dependency. Also Please brief explanation on how the dependency object injected in this project.
Delpin Susai RajPosted Aug 28, 2016, 10:19 AM
Nice
Santhakumar MunuswamyPosted Jun 25, 2016, 1:15 AM
Thank you for nice article. Keep it up
Kuppurasu NagarajPosted Jun 21, 2016, 12:49 PM
Nice Sharing..
Prakash JoshiPosted Jun 21, 2016, 7:32 AM
Thanks for such nicely described and well written article
Vignesh ManiPosted Jun 20, 2016, 6:43 AM
Nice one
RajaPosted Jun 20, 2016, 2:27 AM
Good one....
Debasis SahaPosted Jun 20, 2016, 12:51 AM
Good One..