如何在 Web Forms 中引入依赖注入机制
依賴注入技術(shù)就是將一個(gè)對(duì)象注入到一個(gè)需要它的對(duì)象中,同時(shí)它也是控制反轉(zhuǎn)的一種實(shí)現(xiàn),顯而易見,這樣可以實(shí)現(xiàn)對(duì)象之間的解耦并且更方便測試和維護(hù),依賴注入的原則早已經(jīng)指出了,應(yīng)用程序的高層模塊不依賴于低層模塊,而應(yīng)該統(tǒng)一依賴于抽象或者接口。
在 .Net Framework 4.7.2 中引入了對(duì)依賴注入的支持,終于在 ASP.Net Web Forms 中可以使用依賴注入機(jī)制了,這篇文章將會(huì)討論如何在 ASP.Net Web Forms 中去使用。
創(chuàng)建 WebForm 項(xiàng)目
在 ASP.Net Web Forms 中使用依賴注入,一定要記得將項(xiàng)目框架設(shè)為 4.7.2 以上,要么右鍵項(xiàng)目在屬性面板上選擇 4.7.2 版本。
也可以直接在 web.config 做如下設(shè)置。
<system.web><compilation?debug="true"?targetFramework="4.7.2"?/><httpRuntime?targetFramework="4.7.2"?/> ... </system.web>接下來就可以通過 Nuget 安裝 AspNet.WebFormsDependencyInjection.Unity 包,可以通過 Visual Studio 2019 的 NuGet package manager 可視化界面安裝 或者 通過 NuGet package manager 命令行工具輸入以下命令:
dotnet?add?package?AspNet.WebFormsDependencyInjection.Unity創(chuàng)建實(shí)體 和 接口
現(xiàn)在創(chuàng)建一個(gè)名為 Author 實(shí)體類 和 IAuthorRepository 接口。
public?class?Author{public?int?Id?{?get;?set;?}public?string?FirstName?{?get;?set;?}public?string?LastName?{?get;?set;?}}public?interface?IAuthorRepository{bool?Create(Author?author);Author?Read(int?id);List<Author>?Read();}然后再用 AuthorRepository 類實(shí)現(xiàn)一下 IAuthorRepository 接口,代碼如下:
public?class?AuthorRepository?:?IAuthorRepository{public?bool?Create(Author?author){throw?new?System.NotImplementedException();}public?Author?Read(int?id){throw?new?System.NotImplementedException();}public?List<Author>?Read(){throw?new?System.NotImplementedException();}}創(chuàng)建容器和類型注冊(cè)
現(xiàn)在我們創(chuàng)建 依賴注入容器,然后注入我們想要的類型,下面的代碼用于創(chuàng)建 Unity容器。
var?container?=?this.AddUnity();然后在 Application_Start 事件中進(jìn)行對(duì)象的 依賴配置,如下代碼所示:
container.RegisterType<IAuthorRepository,?AuthorRepository>();對(duì)了,記的引入一下如下兩個(gè)命名空間。
AspNet.WebFormsDependencyInjection.Unity
Unity
下面是 Global 類的完整代碼,僅供參考。
using?Microsoft.AspNet.WebFormsDependencyInjection.Unity; using?System; using?System.Web; using?System.Web.Optimization; using?System.Web.Routing; using?Unity;namespace?WebformsDIDemo {public?class?Global?:?HttpApplication{void?Application_Start(object?sender,?EventArgs?e){var?container?=?this.AddUnity();container.RegisterType<IAuthorRepository,?AuthorRepository>();//?Code?that?runs?on?application?startupRouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);}} }WebForms 使用依賴注入
現(xiàn)在容器,對(duì)象依賴都配置好了,接下來怎么在 Page 中用呢?可以參考下面的代碼。
public?partial?class?_Default?:?Page{private?IAuthorRepository?_authorRepository;public?_Default(IAuthorRepository?authorRepository){_authorRepository?=?authorRepository;}protected?void?Page_Load(object?sender,?EventArgs?e){}}上面的圖很明顯的顯示了,authorRepository 實(shí)例在運(yùn)行時(shí)中已被成功注入。
在 .Net Framework 4.7.2 框架以上,終于將 依賴注入機(jī)制 帶入到了 ASP.Net Web Forms 中,需要明白的是,微軟自帶的Unity包是一個(gè)輕量級(jí)的依賴注入容器,可以在 頁面,控件,handler,module 上使用,在 ASP.Net Web Forms 中使用依賴注入可以輕松創(chuàng)建對(duì)象,然后在運(yùn)行時(shí)獲取依賴,可讓你輕松構(gòu)建靈活,松散的應(yīng)用程序。
譯文鏈接:https://www.infoworld.com/article/3397003/how-to-use-dependency-injection-in-aspnet-web-forms.html
總結(jié)
以上是生活随笔為你收集整理的如何在 Web Forms 中引入依赖注入机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Blazor VS 传统Web应用程序
- 下一篇: 如何成功搞垮一个团队?