依賴注入技術(shù)就是將一個對象注入到一個需要它的對象中,同時它也是控制反轉(zhuǎn)的一種實現(xiàn),顯而易見,這樣可以實現(xiàn)對象之間的解耦并且更方便測試和維護,依賴注入的原則早已經(jīng)指出了,應(yīng)用程序的高層模塊不依賴于低層模塊,而應(yīng)該統(tǒng)一依賴于抽象或者接口。
在 .Net Framework 4.7.2
中引入了對依賴注入的支持,終于在 ASP.Net Web Forms
中可以使用依賴注入機制了,這篇文章將會討論如何在 ASP.Net Web Forms 中去使用。
創(chuàng)建 WebForm 項目
在 ASP.Net Web Forms 中使用依賴注入,一定要記得將項目框架設(shè)為 4.7.2 以上,要么右鍵項目在屬性面板上選擇 4.7.2 版本。
也可以直接在 web.config 做如下設(shè)置。
1
2
3
4
5
|
< 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
命令行工具輸入以下命令:
1
|
dotnet add package AspNet.WebFormsDependencyInjection.Unity |
創(chuàng)建實體 和 接口
現(xiàn)在創(chuàng)建一個名為 Author 實體類 和 IAuthorRepository 接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 類實現(xiàn)一下 IAuthorRepository 接口,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)建容器和類型注冊
現(xiàn)在我們創(chuàng)建 依賴注入容器,然后注入我們想要的類型,下面的代碼用于創(chuàng)建 Unity容器。
1
|
var container = this .AddUnity(); |
然后在 Application_Start 事件中進行對象的 依賴配置,如下代碼所示:
1
|
container.RegisterType<IAuthorRepository, AuthorRepository>(); |
對了,記的引入一下如下兩個命名空間。
- AspNet.WebFormsDependencyInjection.Unity
- Unity
下面是 Global 類的完整代碼,僅供參考。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } } |
WebForms 使用依賴注入
現(xiàn)在容器,對象依賴都配置好了,接下來怎么在 Page 中用呢? 可以參考下面的代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public partial class _Default : Page { private IAuthorRepository _authorRepository; public _Default(IAuthorRepository authorRepository) { _authorRepository = authorRepository; } protected void Page_Load( object sender, EventArgs e) { } } |
上面的圖很明顯的顯示了,authorRepository 實例在運行時中已被成功注入。
在 .Net Framework 4.7.2 框架以上,終于將 依賴注入機制
帶入到了 ASP.Net Web Forms 中,需要明白的是,微軟自帶的Unity包是一個輕量級的依賴注入容器,可以在 頁面,控件,handler,module 上使用,在 ASP.Net Web Forms 中使用依賴注入可以輕松創(chuàng)建對象,然后在運行時獲取依賴,可讓你輕松構(gòu)建靈活,松散的應(yīng)用程序。
以上就是在ASP.Net Web Forms中使用依賴注入的步驟的詳細內(nèi)容,更多關(guān)于ASP.Net Web Forms中使用依賴注入的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://www.cnblogs.com/ireadme/p/14543095.html