该实例在 客户端应用、mvc中,xml配置、手动配置 等多种形式 介绍了 Ninject的用法
using System.Web.Mvc;using System.Web.Routing;using LogService;using LogService.Impl;using Ninject;using Ninject.Web.Mvc;using UserService;using UserService.Impl;namespace MVCApp{ 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", // 路由名称 "{controller}/{action}/{id}", // 带有参数的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 ); } protected override void OnApplicationStarted() { base.OnApplicationStarted(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } protected override IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<IUserService>().To<UserInfoService>(); kernel.Bind<ILogService>().To<TxtLogService>(); //kernel.Bind<ILogService>().To<TxtLogService>().InSingletonScope();//单例 return kernel; } }}
评论