IoC with Ninject
This will show how to use Ninject in a web application in Layered architecture.
DAL is made up of data access connections and commands.
Modules are the binding points of Ninject to the appropriate implementation.
using DAL.Data;
using Ninject;
using System.Data.SqlClient;
namespace DAL.Modules
{
public class DataModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<idbconnection>().To<sqlconnection>().WithConstructorArgument
("connectionString", @"Data Source=E:\Test Projects\Net\MvcApp\MvcApp\App_Data\MyDatabase.sdf");
Bind<idbcommand>().To<sqlcommand>();
}
}
}
Modules can be more than one, coz we can maintain clear separation different modules in the application. Modules should be implement the abstract Load method of the abstract “NinjectModule” class.
namespace DAL.Modules
{
public class RepositoryModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<icustomerrepository>().To<customerrepositorysql>();
}
}
}
In the code behind of the page “INJECT” attribute can be used to point out the
The property, The method, or The constructor you wanted to inject the dependency.
Page which we want to be injected has to be inherited from the “PageBase” class
using DAL.Interfaces;
using Ninject;
using Ninject.Web;
namespace NinjectWebTest
{
public partial class _Default : PageBase
{
[Inject]
public ICustomerRepository CustomerRepo { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(CustomerRepo.GetCustomer(1) + "
");
CustomerRepo.GetAll().ForEach(new Action(ShowWindowsMessage));
}
private void ShowWindowsMessage(string message)
{
Response.Write(message+"
");
}
}
}
Other most important thing is to invoke the kernel in the global.asax of the application and inherit “NinjectHttpApplication”
using Ninject;
using Ninject.Web;
using Ninject.Modules;
namespace NinjectWebTest
{
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[]{
new DAL.Modules.DataModule(),
new DAL.Modules.RepositoryModule()
};
IKernel kernal = new StandardKernel(modules);
return kernal;
}
protected void Application_Start(object sender, EventArgs e)
{
DAL is made up of data access connections and commands.
Mock connections and command has been introduced implementing IDataConnection & IDataCommand interfaces.
Modules are the binding points of Ninject to the appropriate implementation.
using DAL.Data;
using Ninject;
using System.Data.SqlClient;
namespace DAL.Modules
{
public class DataModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<idbconnection>().To<sqlconnection>().WithConstructorArgument
("connectionString", @"Data Source=E:\Test Projects\Net\MvcApp\MvcApp\App_Data\MyDatabase.sdf");
Bind<idbcommand>().To<sqlcommand>();
}
}
}
Modules can be more than one, coz we can maintain clear separation different modules in the application. Modules should be implement the abstract Load method of the abstract “NinjectModule” class.
namespace DAL.Modules
{
public class RepositoryModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<icustomerrepository>().To<customerrepositorysql>();
}
}
}
In the code behind of the page “INJECT” attribute can be used to point out the
The property, The method, or The constructor you wanted to inject the dependency.
Page which we want to be injected has to be inherited from the “PageBase” class
using DAL.Interfaces;
using Ninject;
using Ninject.Web;
namespace NinjectWebTest
{
public partial class _Default : PageBase
{
[Inject]
public ICustomerRepository CustomerRepo { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(CustomerRepo.GetCustomer(1) + "
");
CustomerRepo.GetAll().ForEach(new Action
}
private void ShowWindowsMessage(string message)
{
Response.Write(message+"
");
}
}
}
Other most important thing is to invoke the kernel in the global.asax of the application and inherit “NinjectHttpApplication”
using Ninject;
using Ninject.Web;
using Ninject.Modules;
namespace NinjectWebTest
{
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[]{
new DAL.Modules.DataModule(),
new DAL.Modules.RepositoryModule()
};
IKernel kernal = new StandardKernel(modules);
return kernal;
}
protected void Application_Start(object sender, EventArgs e)
{
Comments