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拦截)测试

 

正文

今天咱们主要讲讲如何使用自带IOC容器,emm..虽然自带的功能不是那么强大,可是胜在轻量级..并且..不用引用别的库..ui

在新的ASP.NET Core中,大量的采用了依赖注入的方式来编写代码.spa

好比,在咱们的Startup类中的ConfigureServices里,就能够看到:3d

AddMvc  AddDbContext  包括咱们以前目录游览用到的AddDirectoryBrowser..code

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

 

1.如何注入本身的服务

下面咱们就来说讲如何注入本身的服务.

首先,咱们编写咱们本身的测试服务以下:

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

编写对应的接口代码以下:

    public interface ITestService
    {
        Guid MyProperty { get; }
        List<string> GetList(string a);
    }

 

而后,咱们要在Startup类引用 Microsoft.Extensions.DependencyInjection(ps,这命名已经很直白了..微软..扩展...依赖注入 - - ,)

修改ConfigureServices方法,以下:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDbContext<BloggingContext>();
             //这里就是注入服务
            services.AddTransient<ITestService, TestService>();
            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";
}

<h2>Index</h2>
@foreach (var item in ViewBag.date)
{

    <h2>@item</h2>
}

最终效果以下:

2.注入服务的生命周期

微软给自行注入的服务,提供了3种生命周期.

Transient(瞬时的)

 每次请求时都会建立的瞬时生命周期服务。这个生命周期最适合轻量级,无状态的服务。

Scoped(做用域的)

在同做用域,服务每一个请求只建立一次。

Singleton(惟一的)

全局只建立一次,第一次被请求的时候被建立,而后就一直使用这一个.

如何使用这三种生命周期呢?.咱们直接在注入的时候用不一样的方法就好了,代码以下:

 services.AddTransient<ITestService, TestService>();
 services.AddScoped<ITestService2, TestService2>();
 services.AddSingleton<ITestService3, TestService3>();

 

下面,咱们就来测试一下这三种生命周期的具体生成状况

咱们编写三个不一样名称的接口以下:

    public interface ITestService
    {
        Guid MyProperty { get; }
        List<string> GetList(string a);
    }
    public interface ITestService2
    {
        Guid MyProperty { get; }
        List<string> GetList();
    }
    public interface ITestService3
    {
        Guid MyProperty { get; }
        List<string> GetList();
    }

 

而后用3个类来分别实现他们.

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

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

        public TestService3()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public List<string> GetList()
        {
            return new List<string>() { "LiLei", "ZhangSan", "LiSi" };
        }
    }

 

 每一个实现类的构造函数中,咱们都产生了一个新的guid,经过这个GUID,咱们能够判断这个类到底从新执行过构造函数没有.

咱们编写注入代码以下:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDbContext<BloggingContext>();
            services.AddTransient<ITestService, TestService>();
            services.AddScoped<ITestService2, TestService2>();
            services.AddSingleton<ITestService3, TestService3>();
            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 在同一个做用域.

 

 

咱们编写相关的index页面,来展现这些信息以下:

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

<h2>Index</h2>
@foreach (var item in ViewBag.date)
{

    <h2>@item</h2>
}

<h1>瞬时的:@ViewBag.guid</h1>
<h1>瞬时的2:@ViewBag.guid11</h1>
<h1>做用域的:@ViewBag.guid2</h1>
<h1>做用域的2:@ViewBag.guid22</h1>
<h1>全局惟一的:@ViewBag.guid3</h1>

咱们运行代码,第一次访问,效果以下:

 

咱们发现瞬时生命周期的,2次生成的GUID都不一致,说明对象不是同一个.

然而做用域生命周期的,由于在同一个做用域下,2次使用服务的GUID都是一致的,说明用的同一个对象.

咱们直接刷新页面进行第二次访问.

效果以下:

瞬时的和做用域的,都继续符合咱们的预期,

全局惟一辈子命周期的和上面第一次访问的GUID保持一致.说明2次访问,都使用的同一个对象.也符合咱们的预期.

 

 

 

写在最后

本篇到此就结束了,下篇咱们讲解,如何使用第三方的Autofac来替换咱们默认的IOC容器,而且使用Autofac的属性注入,来注入咱们的服务.  喜欢的请点个推荐和关注,~有问题也但愿各位批评指正~.

相关文章
相关标签/搜索