1

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

1 Answers1

1

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>();
    }    
Abdul Hadi
  • 1,229
  • 1
  • 11
  • 20
  • so I need to provide references of data layer in the web layer – blackwind090 Jun 25 '16 at 10:15
  • 1
    Yes. Your [composition root is normally completely unaware that any layers exist](http://stackoverflow.com/a/9503612/181087), which means it needs references to all libraries involved in DI. As far as it's concerned, it is only mapping services to abstractions. It doesn't really care where the services actually live. Quite often, you need to put (at least some of) the abstractions into a common library so the rest of the layers can be plugged together easily without creating circular dependencies. – NightOwl888 Jun 25 '16 at 10:51
  • 1
    @blackwind090, take a look at this: http://programmers.stackexchange.com/a/300896/201695 – Yacoub Massad Jun 25 '16 at 15:19
  • @yacoub thanks for the explanation can you please provide me a link of a working example – blackwind090 Jun 25 '16 at 15:39
  • @blackwind090, you mean an example where the composition root is separate from the web project? I don't have any. My answer that I shared is simply explaining the confusion about ASP.NET projects and the composition roots. You might simply decide for your self that you reference the data project from the web/composition root project. But now you understand why you did so. – Yacoub Massad Jun 25 '16 at 15:47
  • @yacoub I mean an example in which composition root is in the web project. So that I can take reference – blackwind090 Jun 25 '16 at 15:50
  • @blackwind090, by default, all ASP.NET/MVC projects have the composition root in the web project. In such project, you have controllers and views which makes it a web project. You also have code related to the `IDependencyResolver` or `IControllerFactory` in that same project. – Yacoub Massad Jun 25 '16 at 15:54
  • @Yacoub I mean any working example on dependency injection for beginners as I have never created an architect – blackwind090 Jun 25 '16 at 16:02
  • Take a look at this: http://www.dotnetcurry.com/aspnet-mvc/786/dependency-injection-aspnet-mvc-introduction – Yacoub Massad Jun 25 '16 at 16:06