閱讀目錄html
前面學習了AutoFac的註冊、解析、生命週期,這裏寫一個AutoFac在ASP.NET MVC中的簡單使用。mvc
基本結構:AutoFacMvc做爲ui層 ,IService類庫(各類服務接口),Service類庫(IService中接口的實現),Model類庫(數據模型,這裏使用EF)框架
咱們的目的:實現MVC中的Controller和Service中的具體實現類解耦函數
獲取用戶列表的簡單例子:學習
IService中的接口:測試
namespace IService { public interface IUserInfoService { List<UserInfo> GetUsers(); } }
Service中的實現:ui
namespace Service { public class UserInfoService : IUserInfoService { //獲取用戶列表 public List<Model.UserInfo> GetUsers() { DbContext context = DbFactory.GetDbContext(); return context.Set<UserInfo>().ToList<UserInfo>(); } } }
1 public class AutofacConfig 2 { 3 /// <summary> 4 /// 負責調用autofac框架實現業務邏輯層和數據倉儲層程序集中的類型對象的建立 5 /// 負責建立MVC控制器類的對象(調用控制器中的有參構造函數),接管DefaultControllerFactory的工做 6 /// </summary> 7 public static void Register() 8 { 9 //實例化一個autofac的建立容器 10 var builder = new ContainerBuilder(); 11 //告訴Autofac框架,未來要建立的控制器類存放在哪一個程序集 (AutoFacMvcDemo) 12 Assembly controllerAss = Assembly.Load("AutoFacMvcDemo"); 13 builder.RegisterControllers(controllerAss); 14 15 //若是有Dal層的話,註冊Dal層的組件 16 //告訴autofac框架註冊數據倉儲層所在程序集中的全部類的對象實例 17 //Assembly dalAss = Assembly.Load("Dal"); 18 //建立respAss中的全部類的instance以此類的實現接口存儲 19 //builder.RegisterTypes(dalAss.GetTypes()).AsImplementedInterfaces(); 20 21 //告訴autofac框架註冊業務邏輯層所在程序集中的全部類的對象實例 22 Assembly serviceAss = Assembly.Load("Service"); 23 //建立serAss中的全部類的instance以此類的實現接口存儲 24 builder.RegisterTypes(serviceAss.GetTypes()).AsImplementedInterfaces(); 25 26 27 //建立一個Autofac的容器 28 var container = builder.Build(); 29 //將MVC的控制器對象實例 交由autofac來建立 30 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 31 } 32 }
1 public class MvcApplication : System.Web.HttpApplication 2 { 3 protected void Application_Start() 4 { 5 AreaRegistration.RegisterAllAreas(); 6 RouteConfig.RegisterRoutes(RouteTable.Routes); 7 //註冊組件 8 AutofacConfig.Register(); 9 } 10 }
UserInfoController :
1 namespace AutoFacMvcDemo.Controllers 2 { 3 public class UserInfoController : Controller 4 { 5 private IUserInfoService userinfoService; 6 //經過構造器注入依賴 7 public UserInfoController(IUserInfoService _userinfoService) 8 { 9 userinfoService = _userinfoService; 10 } 11 12 public ActionResult Index() 13 { 14 //不使用autofac時,service層和mvc ui層強耦合 15 //UserInfoService userInfoService = new UserInfoService(); 16 //List<UserInfo> users= userInfoService.GetUsers(); 17 18 List<UserInfo> users= userinfoService.GetUsers(); 19 ViewBag.users = users; 20 return View(); 21 } 22 } 23 }
Viewspa
@{ ViewBag.Title = "Index"; } <h2>用戶列表</h2> @* 簡單展現用戶 *@ <table> @foreach (var item in ViewBag.users) { <tr> <td>@item.UId</td> <td>@item.UserName</td> <td>@item.Age</td> </tr> } </table>
運行結果:code
補充:能夠逐個進行註冊代碼以下,在Application_Start方法中添加以下代碼 :htm
//建立autofac管理註冊類的容器實例 var builder = new ContainerBuilder(); //註冊組件(注意:在註冊時添加了UserInfoService的引用,其實仍是耦合的,在不分層的項目中好用) builder.RegisterType<UserInfoService>().As<IUserInfoService>(); //使用Autofac提供的RegisterControllers擴展方法來對程序集中全部的Controller一次性的完成註冊 builder.RegisterControllers(Assembly.GetExecutingAssembly()); //生成具體的實例 var container = builder.Build(); //下面就是使用MVC的擴展 更改了MVC中的注入方式. DependencyResolver.SetResolver(new AutofacDependencyResolver(container));