//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; }