I have 6 projects.
- Asp.NET MVC 5 project, which depense from Service project (e.g., Asp.NET project needed IAccountService, which is implemented in Service project).
- DependencyInjection project, which have to manage dependencies in whole solution. This project have referense to Ninject, Ninject.Web.Common and Ninject.Web.Mvc. Now, this project do nothing.
- Domain project, which contains POCO classes (e.g., class Account) and Contracts (e.g., IAccountService).
- Infrastructure project, which contains Entity Framework, Repositories and UnitOfWork.
- Service project, which implements Domain Contracts (e.g., Service project have class AccountService, which implement IAccountService). This project depends from Infrastructure.UnitOfWork.
- Test project. It will test Service project.
Other words, there are the following diagram of dependencies:
- Asp.NET MVC 5 -> Domain.Contracts
- Domain have not any dependencies.
- Infrastructure.UnitOfWork -> UnitOfWork.ApplicationDbContext
- Service -> Infrastructure.UnitOfWork
- Test project -> Domain.Contracts
I want to put all dependencies in DependencyInjection project. Abstractly, I want to put this code:
Bind<UnitOfWork>().To<UnitOfWork>();
Bind<IAccountService>().To<AccountService>();
......
anywhere in DependencyInjection project. Then I want to write:
public class HomeController : BaseController
{
private readonly IAccountService accountService;
public HomeController(IAccountService accountService)
{
this.accountService = accountService;
}
public ActionResult Index()
{
accountService.DepositMoney(10);
return View();
}
}
and have work application.
How I do this? I tried create classes, which inherit from NinjectModule (e.g., I have the following class:
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<UnitOfWork>().To<UnitOfWork>();
Bind<IAccountService>().To<AccountService>();
}
}
Then, In Global.asax.cs (ASP.NET MVC 5 project) I write the following code:
public class MvcApplication : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected override IKernel CreateKernel()
{
var modules = new INinjectModule[]
{
new InfrastructureModule(),
new ServiceModule()
};
var kernel = new StandardKernel(modules);
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
}
Then, I add the following code in Web.config:
<system.web>
<httpModules>
<add name="NinjectHttpModule" type="Ninject.Web.Common.NinjectHttpModule"/>
</httpModules>
</system.web>
As a result, I have an exeption:
Error activating IntPtr No matching bindings are available, and the type is not self-bindable. Activation path:
3) Injection of dependency IntPtr into parameter method of constructor of type Func{IKernel}
2) Injection of dependency Func{IKernel} into parameter lazyKernel of constructor of type HttpApplicationInitializationHttpModule
1) Request for IHttpModule
I get confused. I'll be glad to any advice.