ASP.NET Core MVC 之依賴注入 Controller

  ASP.NET Core MVC 控制器應經過構造函數明確地請求它們地依賴關係,在某些狀況下,單個控制器地操做可能須要一個服務,在控制器級別上的請求可能沒有意義。在這種狀況下,也能夠將服務做爲  Action 的參數。json

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

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 中註冊服務到容器:ui

services.AddTransient<IDateTime, SystemDateTime>();

  在控制其中使用:this

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

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

 

2.使用 FromServices 操做注入spa

  有時,不須要在控制器爲多個操做提供服務。在這種狀況下,將服務注入到操做方法的參數是有意義的。經過 [FromServices] 標記參數來實現:code

        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;
        }
}

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

相關文章
相關標籤/搜索