【发布时间】:2011-07-09 03:50:30
【问题描述】:
我刚开始将 Ninject 与 MVC3 一起使用,所以这是我的问题: - 我从 Nuget 安装了 Ninject 2.2.1.4 和 Ninject.MVC3 2.2.2.0 - 在我的 WebUI(MVC3 项目)中:
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 数据上下文,我想在我的 WebUI 中使用我的 Web.Config 中的连接字符串加载上下文,所以我必须传递构造函数参数,我也有我的域项目中的一些服务
public class LotteryDataService { LinQ.WebDataContext _context; public LotteryDataService(LinQ.WebDataContext context) { _context = context; } public IEnumerable<LinQ.LotteryData> Get() { return _context.LotteryDatas.Take(10); } }
如何使用构造函数参数(这里是连接字符串)将数据上下文与 Ninject 绑定?
【问题讨论】:
标签: asp.net-mvc-3 dependency-injection ninject