How Autofac helps .NET Core MVC DI - 圖解概述

閱讀時間約 3 分鐘
Autofac為可以幫忙達成DI的套件,其運作可以參考運作描述,與如何協助ASP.NET MVC達成DI
這次要了解如何為Dot NET Core MVC應用程式達成DI,勢必也要先了解Dot NET Core原本是如何運作的。可以參考 “ ASP.NET Core Host Outline diagram of Framework operation - With Startup Class” ,以圖來觀察比較容易。
所以會挑出原本 Dot NET Core 產生 Service Provider 的地方做比較,然後替換成Autofac套件所使用的物件。
原本Dot NET Core 產生 Service Provider如下:
aspnet core di - generate provider
利用預設的 ServiceFactoryAdapter 與 DefaultServiceProviderFactory,建立了Provider。
其以ServiceCollection為參數透過DynamicServiceProviderEngine與父類別ServiceProviderEngine產生CallSiteFactory實體為屬性,存回HostBuilder的_appServices。如果要實現已經註冊的服務,可以呼叫透過DynamicServiceProviderEngine繼承的ServiceProvider中的函示GetService來進行resolve。
所以整個藍色的 Microsoft.Extensions.DependencyInjection 區域就是產生 Provider 的關鍵處。如果要取代成由Autofac來做DI,HostBuilder的_appServices 勢必要取到由 Autofac 所產生的 Provider。
所以替換如下:
asp.net core di by autofac
經由替換成 AutofacServiceProviderFactory後,其CreateBuilder函式所產生的容器就是 Autofac 的 ContainerBuilder 了,在這個函式會將原本Dot NET Core 的 ServiceCollection 所包含註冊好的 service 的東西轉成註冊到 ContainerBuilder 上。
接下來呼叫AutofacServiceProviderFactory的CreateServiceProvider函式,會將 Autofac 的 ContainerBuilder做 build的動作,將註冊好的的服務設置好。關於此可以參考 “Autofac 套件如何達成IOC 目的- 概述圖解”。
接下來開發者可以於專案中的 Startup 類別 加入下列函式,註冊想要resolve的服務至 Autofac Container 中:
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}
此段函式會在 Startup.ConfigureServices 在做 services.AddControllersWithViews() 或 services.AddController() 時,會執行Microsoft.AspNetCore.Hosting 下的 GenericWebHostBuilder 的 UseStartup 會抓出此 method 來執行。
最後將範例做簡單的展示:

0會員
2內容數
留言0
查看全部
發表第一個留言支持創作者!
Czxdas的沙龍 的其他內容
你可能也想看
How to Make a Big Cake Box?Creating a big cake box is essential for protecting and showcasing your cake.-HS Packaging
Thumbnail
avatar
Huaisheng Packaging
2024-06-19
How Biochar Sequesters Carbon?Biochar has garnered significant attention as a potent tool for carbon sequestration. This ancient technique, modernized for contemporary environmenta
avatar
Wayne Shen
2024-06-15
How to Integrating Social Media into Your WordPress SiteIn the digital age, social media is a vital component of online marketing and audience engagement.
Thumbnail
avatar
Digiworld
2024-05-31
How a Web Design Agency Can Transform Your Business in HKIn the digital age, a company's website often serves as the first point of contact between the business and potential customers.
Thumbnail
avatar
Digiworld
2024-05-28
How Financial Services are Transforming via IT OutsourcingIn the rapidly evolving landscape of the banking and financial services industry, digital transformation is no longer a luxury but a necessity.
Thumbnail
avatar
Digiworld
2024-04-26
How to set up a digital signage business?Like all other businesses — understand the industry, think about your value, find partners and build the channel.
avatar
JW
2021-07-06
How to integrate Tarot into daily lifeMost people think of Tarot as a tool for answering questions or predicting the future. Whenever I take a deck of cards out of my bag, I am asked to di
Thumbnail
avatar
穆欣
2021-06-26
How to study history - see "Three Pigs"!This is an idea tells you how to study  History (or what ever) correctly and usefully. Some students like me are confused about their graduating and
Thumbnail
avatar
小歌
2021-06-26
How To Taste Coffee 如何品嚐咖啡您是否常困擾,喜歡喝咖啡或當每次喝到好喝的咖啡時,但都不知該如何描述他的滋味與風味呢,想知道怎麼細細品嚐一杯咖啡嗎?看這篇就對了!
Thumbnail
avatar
TheCoffeeSoul
2021-02-17
How are you?你最近過得怎麼樣? 最近還好嗎? 一句問候要怎樣回應呢……? 該是禮貌地說:「還不錯」「差不多」「還可以」…… 還是說出事實:「很不好」 雖然沒有標準答案,每個人會有不一樣的想法,但近年我大多會說出最真實的那部分。 一聲問候, 有時眼淚會缺堤, 有時鼻開始發酸, 有時覺得莫名, 有時覺得煩躁,
Thumbnail
avatar
朦卡Kaiar
2021-01-17