ASP.NET MVC中使用Autofac实现简单依赖注入
本文參考資料:
1、https://www.cnblogs.com/RayWang/p/11128554.html。
2、https://www.cnblogs.com/eedc/p/6127181.html
3、https://www.cnblogs.com/ancupofcoffee/p/5007649.html#top
前言?
關于IoC和DI(依賴注入)的概念網上一搜一大把。簡單來說,IoC即“控制反轉”,是一種設計原則,一個抽象的概念。而依賴注入是實現IoC的一種設計。DI容器有很多,比如Unity、Autofac等。利用DI容器,可以將代碼解耦,高分離度的代碼將更有利于維護。利用上述參考資料,文本將依樣畫葫蘆,實現一個簡單的DI案例。
項目結構?
? ?
| 項目 | 名稱 | 類型 | 框架 |
| Wangxc.AutoFac.Infrasturacute.Ioc | DI Framework 容器 | 庫類 | .NET FrameWork 4.6 |
| Wangxc.AutoFac.Model | 實體層 | 庫類 | .NET FrameWork 4.6 |
| Wangxc.AutoFac.Repository | 倉儲層 | 庫類 | .NET FrameWork 4.6 |
| Wangxc.AutoFac.Service | 業務邏輯層 | 庫類 | .NET FrameWork 4.6 |
| Wangxc.AtuoFac.MvcApp | .NET Framework MVC主程序 | ASP.NET MVC 項目 | .NET FrameWork 4.6 |
| 名稱 | 職責 | 舉例 |
| 界面層 | 負責展示數據 | StudentController |
| 業務邏輯層 | 負責業務邏輯 | StudentService |
| 數據訪問層 | 負責數據訪問 | StudentRepository |
| 名稱 | 職責 | 舉例 |
| 界面層UI | 負責展示數據 | StudentController |
| 業務邏輯抽象層 (Ineterface BLL) | 業務邏輯運算抽象接口 | IStudentService |
| 業務邏輯層 (BLL) | 業務邏輯運算 | StudentServcie |
| 數據訪問抽象層 (InterfaceDAL) | 數據訪問抽象接口 | IStudentRepository |
| 數據訪問層 (DAL) | 負責提供數據 | StudentRepository |
?
?
?
?
?
? ? ?
?
項目具體實現?
實體層:
namespace Wangxc.AutoFac.Model {public class StudentEntity{public int Id { get; set; }public string Name { get; set; }public string Grade { get; set; }} }?
倉儲層:
namespace Wangxc.AutoFac.Repository.IRepository {public interface IStudentRepository{string GetName(int id);} } using Wangxc.AutoFac.Repository.IRepository;namespace Wangxc.AutoFac.Repository.Repository {public class StudentRepository : IStudentRepository{public string GetName(int id){switch (id){case 3: return "張三";case 4: return "李四";case 5: return "王五";default:return "趙六";}; }} }?
邏輯層:
namespace Wangxc.AutoFac.Service.IService {public interface IStudentService{string GetName(int id);} } using Wangxc.AutoFac.Service.IService; using Wangxc.AutoFac.Repository.IRepository;namespace Wangxc.AutoFac.Service.Service {public class StudentService : IStudentService{private readonly IStudentRepository _studentRepository;public StudentService(IStudentRepository studentRepository){this._studentRepository = studentRepository;}public string GetName(int id){return this._studentRepository.GetName(id);}} }?
AutoFac IoC容器層:?
通過NuGet程序包引入Autofac包:
? ? ? ? ?
通過NuGet程序包引入Autofac mvc5 程序包:?
? ? ? ??
新建類文件MvcContainer.cs用于注冊批量對象:?
? ? ? ? ? ? ? ? ? ? ? ?
using System; using System.Linq; using Autofac; using Autofac.Integration.Mvc; using System.Reflection;namespace Wangxc.AutoFac.Infrasturcture.Ioc {public static class MvcContainer{public static IContainer Instance;public static System.Web.Mvc.IDependencyResolver Init(Func<ContainerBuilder, ContainerBuilder> func = null){var builder = new ContainerBuilder();//新建容器用于注冊組件MyBuild(builder);//注冊組件func?.Invoke(builder);Instance = builder.Build();//利用構建器創建容器return new AutofacDependencyResolver(Instance);//返回針對MVC的解析器}public static void MyBuild(ContainerBuilder builder){//注冊倉儲層Assembly repositoryAssembly = Assembly.Load("Wangxc.AutoFac.Repository");builder.RegisterAssemblyTypes(repositoryAssembly).PublicOnly() //只要public訪問權限的.Where(cc => cc.IsClass) //只要class類型的(排除值和interface類型).AsImplementedInterfaces(); //自動以其實現的接口暴露(包括Dispose接口)//注冊邏輯層Assembly serviceAssembly = Assembly.Load("Wangxc.AutoFac.Service");builder.RegisterAssemblyTypes(serviceAssembly).PublicOnly().Where(cc => cc.IsClass).AsImplementedInterfaces();Assembly MvcAssembly = Assembly.Load("Wangxc.AutoFac.MvcApp");builder.RegisterControllers(MvcAssembly);//注冊MVC項目中的Controller}} }MVC項目UI層:
通過NuGet程序包引入Autofac包:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
?
? ? ? ???
在Global.asax.cs中添加如下代碼:?
? ? ? ??
新增StudentController.cs控制器用于測試Autofac是否搭建成功,如下圖所示為構造函數注入:?
using System.Web.Mvc; using Wangxc.AutoFac.Service.IService;namespace Wangxc.AutoFac.MvcApp.Controllers {public class StudentController : Controller{private readonly IStudentService _studentService;public StudentController(IStudentService studentService){this._studentService = studentService;}[HttpGet]public string GetNameById(int id){return this._studentService.GetName(id);}} }試運行?
運行程序,并導航到Student/GetNameById,如下圖所示:
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ??
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的ASP.NET MVC中使用Autofac实现简单依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sentry + vue实现错误日志监控
- 下一篇: 精通ASP.NET MVC ——属性路由