Reputation: 63
I am starting a project in ASP.NET MVC using Ninject to implement dependency injection with Ninject MVC5 . Here is my current architecture:
--Data Layer
Repository
public class AccountRepository : IAccountRepository
{
public bool Test(bool a)
{
return a;
}
}
IRepository
public interface IAccountRepository
{
bool Test(bool a);
}
----Service Layer
service
IAccountRepository _IAccountRepository = null;
public AccountService(IAccountRepository AccountRepository)
{
this._IAccountRepository = AccountRepository;
}
public bool test(bool a)
{
return _IAccountRepository.Test(a);
}
IService
public interface IAccountService
{
bool test(bool a);
}
Ninject Common Service Layer
public static class NinjectWebCommon
{
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAccountRepository>().To<AccountRepository>();
}
}
---Web Layer
Controller
IAccountService _IAccountService = null;
public HomeController (IAccountService IAccountService)
{
this._IAccountService = IAccountService;
}
public ActionResult Index()
{
bool value = _IAccountService.test(true);
return View();
}
Ninject Common in web layer
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using Service.IService;
using Service.Service;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAccountService>().To<AccountService>();
}
I am getting the following error
Error activating IAccountRepository No matching bindings are available, and the type is not self-bindable. Activation path: 3) Injection of dependency IAccountRepository into parameter AccountRepository of constructor of type AccountService 2) Injection of dependency IAccountService into parameter IAccountService of constructor of type HomeController 1) Request for HomeController
Suggestions: 1) Ensure that you have defined a binding for IAccountRepository. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.
please tell me what i am doing wrong i have updated ninject to the latest version also in case there is an error related to the version
Upvotes: 1
Views: 1135
Reputation: 1228
As the error message suggest the class AccountService
depend on AccountRepository
so you have to inject AccountRepository
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAccountRepository>().To<AccountRepository>();
kernel.Bind<IAccountService>().To<AccountService>();
}
Upvotes: 1