标签:prot typeof 新建 icon 自定义 tab structure autofac ctc
Autofac跟Ninject一样,都是用于IOC,实现依赖注入的。主要有两个步骤,其余跟Ninject差不多。
1、主要的操作是在MVC项目下的Infrastructure(基础结构/公共建设)文件夹下,新建一人注册Autofac的IOC的类
namespace BookShop.WebUI.Infrastructure { public class AutofacReg { public static void RegisterDenpendencies() { var build = new ContainerBuilder(); build.RegisterControllers(typeof(MvcApplication).Assembly); build.RegisterType<BookRepository>().As<IBookRepository>(); //注册接口IBookRepository所依赖的实现类型BookRepository IContainer container = build.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } }
2、在Global.asax文件里调用1、里的静态方法RegisterDenpendencies(),使用我们自定义的AutofacDependencyResolver(container)
来替换mvc5中默认的DependencyResolver,这样就能够让mvc5支持DI,依赖注入。
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //设置Controller工厂 //ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); //设置Ninject DependencyResolver //DependencyResolver.SetResolver(new NinjectDependencyResolver()); AutofacReg.RegisterDenpendencies(); } }
标签:prot typeof 新建 icon 自定义 tab structure autofac ctc
原文地址:http://www.cnblogs.com/yxlq/p/6746218.html