日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC中使用Autofac实现简单依赖注入

發布時間:2025/3/11 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

前言?

關于IoCDI(依賴注入)的概念網上一搜一大把。簡單來說,IoC即“控制反轉”,是一種設計原則,一個抽象的概念。而依賴注入是實現IoC的一種設計。DI容器有很多,比如Unity、Autofac等。利用DI容器,可以將代碼解耦,高分離度的代碼將更有利于維護。利用上述參考資料,文本將依樣畫葫蘆,實現一個簡單的DI案例。


項目結構?

? ?

項目名稱類型框架
Wangxc.AutoFac.Infrasturacute.IocDI 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实现简单依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。