注入依赖项

时间:2016-06-25 07:18:17

标签: c# asp.net-mvc dependency-injection

我正在使用Ninject在ASP.NET MVC中启动一个项目,以使用Ninject MVC5实现依赖注入。这是我目前的架构:

- 数据层

存储库

public class AccountRepository : IAccountRepository
{
    public bool Test(bool a)
    {
        return a;
    }
}

IRepository

public interface IAccountRepository
{
    bool Test(bool a);
}

----服务层

服务

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>();
    }        
}

---网络层

控制器

IAccountService _IAccountService = null;

        public HomeController (IAccountService IAccountService)
        {
            this._IAccountService = IAccountService;
        }

        public ActionResult Index()
        {
            bool value = _IAccountService.test(true);
            return View();
        }

网络图层中的Ninject Common

   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>();
    } 

我收到以下错误

  

激活IAccountRepository时出错没有匹配的绑定   可用,并且类型不可自我绑定。激活路径:3)   将依赖项IAccountRepository注入参数   AccountService类型的构造函数的AccountRepository 2)注入   依赖IAccountService到参数IAccountService   HomeController类型的构造函数1)HomeController请求

     

建议:1)确保您已为其定义了绑定   IAccountRepository。 2)如果在模块中定义了绑定,请确保   该模块已加载到内核中。 3)确保你有   不小心创建了多个内核。 4)如果你正在使用   构造函数参数,确保参数名称匹配   构造函数参数名称。 5)如果您使用的是自动模块   加载,确保搜索路径和过滤器正确无误。

请告诉我我做错了我已将ninject更新到最新版本,以防万一与版本相关的错误

1 个答案:

答案 0 :(得分:1)

由于错误消息表明课程AccountService取决于AccountRepository所以您必须注入AccountRepository

 private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IAccountRepository>().To<AccountRepository>();
        kernel.Bind<IAccountService>().To<AccountService>();
    }