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

歡迎訪問 生活随笔!

生活随笔

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

asp.net

ASP.NET Core MVC 之依赖注入 Controller

發布時間:2023/12/2 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET Core MVC 之依赖注入 Controller 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  ASP.NET Core MVC 控制器應通過構造函數明確地請求它們地依賴關系,在某些情況下,單個控制器地操作可能需要一個服務,在控制器級別上的請求可能沒有意義。在這種情況下,也可以將服務作為? Action 的參數。

  依賴注入是一種如 Dependency Inversion Principle 所示的技術,允許應用程序松散耦合的模塊組成。

1.構造函數注入

  ASP.NET Core 內置的基于構造函數的依賴注入支持擴展到 MVC 控制器。通過只添加一個服務類型作為構造函數參數到控制器中,ASP.NET Core 將會嘗試使用內置服務容器解析這個類型。服務通常(但不總是)使用接口定義。例如,如果應用程序定義一個檢索時間的服務,然后依賴注入而不是硬編碼:

  定義接口和實現:

namespace MVCTest.Services {public interface IDateTime{DateTime Now { get; }}public class SystemDateTime: IDateTime{public DateTime Now{get { return DateTime.Now; }}} }

  在?ConfigureServices 中注冊服務到容器:

services.AddTransient<IDateTime, SystemDateTime>();

  在控制其中使用:

public class DateTimeController : Controller{private IDateTime _dateTime;public DateTimeController(IDateTime dateTime){_dateTime = dateTime;}// GET: DateTimepublic ActionResult Index(){var serverTime = _dateTime.Now;if (serverTime.Hour < 12){ViewData["Message"] = "Good Morning";}return View();} }

  ASP.NET Core 內置的依賴注入支持用于請求服務的類型只能有一個構造函數,如果多于一個會報異常。使用第三方實現替換默認依賴注入,可以實現支持多個構造函數。

?

2.使用 FromServices 操作注入

  有時,不需要在控制器為多個操作提供服務。在這種情況下,將服務注入到操作方法的參數是有意義的。通過 [FromServices] 標記參數來實現:

public ActionResult Index([FromServices] IDateTime _dateTime){var serverTime = _dateTime.Now;if (serverTime.Hour < 12){ViewData["Message"] = "Good Morning";}return View();}

?

3.在控制器中訪問設置

  在控制器中訪問應用程序設置或者配置設置時常見的模式。此訪問應當使用在?Configuration 中描述的訪問模式。通常不應從控制器中使用依賴注入直接請求設置,更好的方式是請求?IOptions<T> 實例,T是你需要的配置類型。例如:

  創建選項類:

public class AppSettingOptions{public DefaultConnec ConnectionStrings { get; set; }public string AllowedHosts { get; set; }}public class DefaultConnec{public string DefaultConnection { get; set; }}

appsettings.json:

{"ConnectionStrings": {"DefaultConnection": "Data Source=.;Initial Catalog=Test;Integrated Security=True"},"Logging": {"LogLevel": {"Default": "Information"}},"AllowedHosts": "*" }

  配置應用程序使用選項模型,在?ConfigureServices 中添加配置類到服務容器:

public Startup(IConfiguration configuration,IHostingEnvironment env){//Configuration = configuration;var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json",optional:true,reloadOnChange:true)//.AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional:true) ;//配置環境變量//builder.AddEnvironmentVariables();Configuration = builder.Build();}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.AddOptions();services.Configure<AppSettingOptions>(Configuration);//通過代碼編寫services.Configure<AppSettingOptions>(options=>{options.AllowedHosts = "test";});}

  示例是從?appsettings.json 讀取設置,也可以在代碼中添加設置。

  一旦指定了請類型的配置對象?AppSettingOptions,并將其添加到服務容器,就可以在控制器或操作方法通過請求?IOptions<AppSettingOptions>? 的實例獲取它:

public class HomeController : Controller{private readonly IOptions<AppSettingOptions> _options;public HomeController(IOptions<AppSettingOptions> options){_options = options;} }

  遵循選項模式允許將設置和配置彼此分離,并且確保控制器遵循關注點分離,因為不需要知道如何在哪里找到設置信息。由于控制器類中沒有靜態附著或者直接實例化設置類,因此使得控制器更容易使用單元測試。

?

?

?

?

轉載于:https://www.cnblogs.com/afei-24/p/11367425.html

總結

以上是生活随笔為你收集整理的ASP.NET Core MVC 之依赖注入 Controller的全部內容,希望文章能夠幫你解決所遇到的問題。

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