MVC3,Ninject和Ninject.MVC3问题 [英] MVC3, Ninject and Ninject.MVC3 problem

查看:234
本文介绍了MVC3,Ninject和Ninject.MVC3问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Ninject与MVC3所以这里是我的问题:
- 我从安装的NuGet 2.2.1.4 Ninject和Ninject.MVC3 2.2.2.0
- 在我的WebUI中(MVC3项目):

I just start using Ninject with MVC3 so here is my problem: - I installed Ninject 2.2.1.4 and Ninject.MVC3 2.2.2.0 from Nuget - In my WebUI (MVC3 project):

的Global.asax.cs

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }


}


  • 在我的域名(类项目),我有我的LINQ to SQL的DataContext,我想从我的web.config加载与连接字符串的背景下,我WebUI中,所以我必须通过构造函数的参数,我也有一些服务,我的域名项目

    • In my Domain (class project), i have my LinQ to SQL datacontext, i want to load the context with the connection string from my Web.Config in my WebUI, so i have to pass the constructor parameter, i also have some services in my Domain project

      public class LotteryDataService
      {
          LinQ.WebDataContext _context;
      
          public LotteryDataService(LinQ.WebDataContext context)
          {
              _context = context;
          }
      
          public IEnumerable<LinQ.LotteryData> Get()
          {
              return _context.LotteryDatas.Take(10);
          }
      }
      


    • 如何到DataContext与Ninject与构造函数的参数(这里是连接字符串)?结合

      How to bind the datacontext with Ninject with constructor parameter (here is connection string)?

      推荐答案

      这是你如何通过构造函数的参数。 Ninject将解决指定的构造函数的参数相匹配的构造函数。

      This is how you pass a constructor parameter. Ninject will resolve the constructor that matches the specified constructor arguments.

      public class DataModule : NinjectModule
      {
          public override void Load()
          {
              string connectionString = "...";
              Bind<WebDataContext>().ToSelf()
                  .WithConstructorArgument("connection", connectionString);
          }
      }
      

      的第一个参数 .WithConstructorArgument()应该是构造函数参数的名称。这是 fileOrServerOrConnection 基类,但连接在派生类中。

      The first argument to .WithConstructorArgument() should be the name of the constructor parameter. This is fileOrServerOrConnection in the base class, but connection in derived class.

      这篇关于MVC3,Ninject和Ninject.MVC3问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆