ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自帶的IOC容器)

原文: ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自帶的IOC容器)

前言

本文主要是詳解一下在ASP.NET Core中,自帶的IOC容器相關的使用方式和注入類型的生命週期.html

這裏就不詳細的贅述IOC是什麼 以及DI是什麼了.. emm..不懂的能夠自行百度.框架

目錄

ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自帶的IOC容器)函數

ASP.NET Core中使用IOC三部曲(二.採用Autofac來替換IOC容器,並實現屬性注入)post

ASP.NET Core中使用IOC三部曲(三.採用替換後的Autofac來實現AOP攔截)測試

nbsp;ui

正文

今天咱們主要講講如何使用自帶IOC容器,emm..雖然自帶的功能不是那麼強大,可是勝在輕量級..並且..不用引用別的庫..spa

在新的ASP.NET Core中,大量的採用了依賴注入的方式來編寫代碼.code

好比,在咱們的Startup類中的ConfigureServices裏,就能夠看到:htm

AddMvcnbsp;nbsp;AddDbContextnbsp; 包括咱們以前目錄遊覽用到的AddDirectoryBrowser..對象

都是框架提供好的服務,咱們直接注入就可使用了.

nbsp;

1.如何注入本身的服務

下面咱們就來說講如何注入本身的服務.

首先,咱們編寫咱們本身的測試服務以下:

    public class TestService: ITestService
    {
        public TestService()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public Listlt;stringgt; GetList(string a)
        {
            return new Listlt;stringgt;() { "LiLei", "ZhangSan", "LiSi" };
        }
    }

編寫對應的接口代碼以下:

    public interface ITestService
    {
        Guid MyProperty { get; }
        Listlt;stringgt; GetList(string a);
    }

nbsp;

而後,咱們要在Startup類引用nbsp;Microsoft.Extensions.DependencyInjection(ps,這命名已經很直白了..微軟..擴展...依賴注入 - - ,)

修改ConfigureServices方法,以下:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDbContextlt;BloggingContextgt;();
             //這裏就是注入服務
            services.AddTransientlt;ITestService, TestServicegt;();
            services.AddDirectoryBrowser();
        }

AddTransient就是注入的方法之一,泛型參數,前面一個是你服務的接口,第二個是服務的實現類..

這樣,咱們就完成了初步的注入操做.

那麼咱們如何使用咱們注入的服務呢?

咱們到控制器,編寫代碼以下:

 public class DITestController : Controller
    {
            private readonly ITestService _testService;
            public DITestController(ITestService testService)
            {
                   _testService = testService;
             }
             public IActionResult Index()
            {
                ViewBag.date = _testService.GetList("");
                return View();
             }
    }

注入的方式通常有三種,構造函數注入, 方法注入,屬性注入..微軟自帶的這個IOC容器,默認採用了構造函數注入的方式(不支持屬性注入,不過能夠用第三方容器替換來實現,下篇講)

咱們編寫咱們的index視圖以下:

@{
    ViewData["Title"] = "Index";
}

lt;h2gt;Indexlt;/h2gt;
@foreach (var item in ViewBag.date)
{

    lt;h2gt;@itemlt;/h2gt;
}

最終效果以下:

2.注入服務的生命週期

微軟給自行注入的服務,提供了3種生命週期.

Transient(瞬時的)

nbsp;每次請求時都會建立的瞬時生命週期服務。這個生命週期最適合輕量級,無狀態的服務。

Scoped(做用域的)

在同做用域,服務每一個請求只建立一次。

Singleton(惟一的)

全局只建立一次,第一次被請求的時候被建立,而後就一直使用這一個.

如何使用這三種生命週期呢?.咱們直接在注入的時候用不一樣的方法就好了,代碼以下:

 services.AddTransientlt;ITestService, TestServicegt;();
 services.AddScopedlt;ITestService2, TestService2gt;();
 services.AddSingletonlt;ITestService3, TestService3gt;();

nbsp;

下面,咱們就來測試一下這三種生命週期的具體生成狀況

咱們編寫三個不一樣名稱的接口以下:

    public interface ITestService
    {
        Guid MyProperty { get; }
        Listlt;stringgt; GetList(string a);
    }
    public interface ITestService2
    {
        Guid MyProperty { get; }
        Listlt;stringgt; GetList();
    }
    public interface ITestService3
    {
        Guid MyProperty { get; }
        Listlt;stringgt; GetList();
    }

nbsp;

而後用3個類來分別實現他們.

public class TestService: ITestService
    {
        public TestService()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public Listlt;stringgt; GetList(string a)
        {
            return new Listlt;stringgt;() { "LiLei", "ZhangSan", "LiSi" };
        }
    }

    public class TestService2 : ITestService2
    {
        public TestService2()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public Listlt;stringgt; GetList()
        {
            return new Listlt;stringgt;() { "LiLei", "ZhangSan", "LiSi" };
        }
    }
    public class TestService3 : ITestService3
    {

        public TestService3()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public Listlt;stringgt; GetList()
        {
            return new Listlt;stringgt;() { "LiLei", "ZhangSan", "LiSi" };
        }
    }

nbsp;

nbsp;每一個實現類的構造函數中,咱們都產生了一個新的guid,經過這個GUID,咱們能夠判斷這個類到底從新執行過構造函數沒有.

咱們編寫注入代碼以下:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDbContextlt;BloggingContextgt;();
            services.AddTransientlt;ITestService, TestServicegt;();
            services.AddScopedlt;ITestService2, TestService2gt;();
            services.AddSingletonlt;ITestService3, TestService3gt;();
            services.AddDirectoryBrowser();
        }

咱們修改控制器以下:

    public class DITestController : Controller
    {

        private readonly ITestService _testService;
        private readonly ITestService2 _testService2;
        private readonly ITestService3 _testService3;
        public DITestController(ITestService testService, ITestService2 testService2, ITestService3 testService3)
        {
            _testService = testService;
            _testService2 = testService2;
            _testService3 = testService3;
        }
        //這裏採用了Action注入的方法
        public IActionResult Index([FromServices]ITestService testService11, [FromServices]ITestService2 testService22)
        {
            ViewBag.date = _testService.GetList("");
            ViewBag.guid = _testService.MyProperty;
            ViewBag.guid11 = testService11.MyProperty;
            ViewBag.guid2 = _testService2.MyProperty;
            ViewBag.guid22 = testService22.MyProperty;
            ViewBag.guid3 = _testService3.MyProperty;
            return View();
        }
}

這裏說明一下,咱們採用了Action注入的方法,新注入了一個ITestService2 ,來保證2個ITestService2 在同一個做用域.

nbsp;

nbsp;

咱們編寫相關的index頁面,來展現這些信息以下:

@{
    ViewData["Title"] = "Index";
}

lt;h2gt;Indexlt;/h2gt;
@foreach (var item in ViewBag.date)
{

    lt;h2gt;@itemlt;/h2gt;
}

lt;h1gt;瞬時的:@ViewBag.guidlt;/h1gt;
lt;h1gt;瞬時的2:@ViewBag.guid11lt;/h1gt;
lt;h1gt;做用域的:@ViewBag.guid2lt;/h1gt;
lt;h1gt;做用域的2:@ViewBag.guid22lt;/h1gt;
lt;h1gt;全局惟一的:@ViewBag.guid3lt;/h1gt;

咱們運行代碼,第一次訪問,效果以下:

nbsp;

咱們發現瞬時生命週期的,2次生成的GUID都不一致,說明對象不是同一個.

然而做用域生命週期的,由於在同一個做用域下,2次使用服務的GUID都是一致的,說明用的同一個對象.

咱們直接刷新頁面進行第二次訪問.

效果以下:

瞬時的和做用域的,都繼續符合咱們的預期,

全局惟一輩子命週期的和上面第一次訪問的GUID保持一致.說明2次訪問,都使用的同一個對象.也符合咱們的預期.

nbsp;

nbsp;

nbsp;

寫在最後

本篇到此就結束了,下篇咱們講解,如何使用第三方的Autofac來替換咱們默認的IOC容器,而且使用Autofac的屬性注入,來注入咱們的服務.nbsp; 喜歡的請點個推薦和關注,~有問題也但願各位批評指正~.

相關文章
相關標籤/搜索