//autofac注入 ContainerBuilder builder = new ContainerBuilder(); // Register your MVC controllers. (MvcApplication is the name of // the class in Global.asax.) builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterType<DAL.StudentRepository>().As<DAL.IStudent>(); //这是自己写的接口及实现 //移除原本的mvc的容器,使用AutoFac的容器,将MVC的控制器对象实例交由autofac来创建 var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
private readonly IStudent dal; public HomeController(IStudent dal) { this.dal = dal; }