标签: 依赖注入 Autofac ASPNETCore
关于IoC模式(控制反转)和DI技术(依赖注入),我们已经见过很多的探讨,这里就不再赘述了。比如说必看的Martin Fowler《IoC 容器和 Dependency Injection 模式》,相关资料链接都附于文章末尾。其中我非常赞同Artech的说法 "控制更多地体现为一种流程的控制" ,而依赖注入技术让我们的应用程序实现了松散耦合。
ASP.NET Core本身已经集成了一个轻量级的IOC容器,开发者只需要定义好接口后,在Startup.cs的ConfigureServices方法里使用对应生命周期的绑定方法即可,常见方法如下
services.AddTransient<IApplicationService,ApplicationService> services.AddScoped<IApplicationService,ApplicationService> services.AddSingleton<IApplicationService,ApplicationService>
对于上述的三种DI注入方式,官方也给出了详细的解释,我来简单翻译一下
在这之后,我们便可以将服务通过构造函数注入或者是属性注入的方式注入到Controller,View(通过使用 @inject
),甚至是Filter中(以前使用Unity将依赖注入到Filter真是一种痛苦)。话不多说,先来体验一把
Tips:Startup.cs是什么,详见 ASP.NET Core 介绍和项目解读
大多项目举例依赖注入的生命周期演示时,都会采取可变Guid来作为返回显示,此次示例也会这样处理。我们先定义一个IGuidAppService接口,里面定义基接口和三种注入模式的接口
public interface IGuidAppService { Guid GuidItem(); } public interface IGuidTransientAppService : IGuidAppService { } public interface IGuidScopedAppService : IGuidAppService { } public interface IGuidSingletonAppService : IGuidAppService { }
同样的,在GuidAppService中定义其实现类。这里为了直观显示每次请求的返回值,采取如下代码
public class GuidAppServiceBase : IGuidAppService { private readonly Guid _item; public GuidAppServiceBase() { _item = Guid.NewGuid(); } public Guid GuidItem() { return _item; } } public class GuidTransientAppService : GuidAppServiceBase, IGuidTransientAppService { } public class GuidScopedAppService : GuidAppServiceBase, IGuidScopedAppService { } public class GuidSingletonAppService : GuidAppServiceBase, IGuidSingletonAppService { }
最后是Controller和View视图的代码
# Controller public class HomeController : Controller { private readonly IGuidTransientAppService _guidTransientAppService; //#构造函数注入 //private IGuidTransientAppService _guidTransientAppService { get; } #属性注入 private readonly IGuidScopedAppService _guidScopedAppService; private readonly IGuidSingletonAppService _guidSingletonAppService; public HomeController(IGuidTransientAppService guidTransientAppService, IGuidScopedAppService guidScopedAppService, IGuidSingletonAppService guidSingletonAppService) { _guidTransientAppService = guidTransientAppService; _guidScopedAppService = guidScopedAppService; _guidSingletonAppService = guidSingletonAppService; } public IActionResult Index() { ViewBag.TransientItem = _guidTransientAppService.GuidItem(); ViewBag.ScopedItem = _guidScopedAppService.GuidItem(); ViewBag.SingletonItem = _guidSingletonAppService.GuidItem(); return View(); } } # Index View <div class="row"> <div > <h2>GuidItem Shows</h2> <h3>TransientItem: @ViewBag.TransientItem</h3> <h3>ScopedItem: @ViewBag.ScopedItem</h3> <h3>SingletonItem: @ViewBag.SingletonItem</h3> </div> </div>
之后我们打开两个浏览器,分别刷新数次,也只会发现“TransientItem”和“ScopedItem”的数值不断变化,“SingletonItem”栏的数值是不会有任何变化的,这就体现出单例模式的作用了,示例图如下
但是这好像还不够,要知道我们的Scoped的解读是“生命周期横贯整次请求”,但是现在演示起来和Transient好像没有什么区别(因为两个页面每次浏览器请求仍然是独立的,并不包含于一次中),所以我们采用以下代码来演示下(同一请求源)
# 新建GuidItemPartial.cshtml视图,复制如下代码,使用@inject注入依赖 @using DependencyInjection.IApplicationService @inject IGuidTransientAppService TransientAppService @inject IGuidScopedAppService GuidScopedAppServic @inject IGuidSingletonAppService GuidSingletonAppService <div class="row"> <div> <h2>GuidItem Shows</h2> <h3>TransientItem: @TransientAppService.GuidItem()</h3> <h3>ScopedItem: @GuidScopedAppServic.GuidItem()</h3> <h3>SingletonItem: @GuidSingletonAppService.GuidItem()</h3> </div> </div> # 原先的index视图 @{ ViewData["Title"] = "Home Page"; } @Html.Partial("GuidItemPartial") @Html.Partial("GuidItemPartial")
依然是 Ctrl+F5 调试运行,可以发现“ScopedItem”在同一请求源中是不会发生变化的,但是“TransientItem”依然不断变化,理论仍然是支持的
除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle,Castle也是使用ABP时自带的)。
首先在project.json的 dependency
节点中加入 Autofac.Extensions.DependencyInjection
引用,目前最新版本是 4.0.0-rc3-309
在Startup.cs中创建一个 public IContainer ApplicationContainer { get; private set; }
对象,并把 ConfigureServices
返回类型改为 IServiceProvider
,然后复制以下代码进去,也可以实现相关功能
var builder = new ContainerBuilder(); //注意以下写法 builder.RegisterType<GuidTransientAppService>().As<IGuidTransientAppService>(); builder.RegisterType<GuidScopedAppService>().As<IGuidScopedAppService>().InstancePerLifetimeScope(); builder.RegisterType<GuidSingletonAppService>().As<IGuidSingletonAppService>().SingleInstance(); builder.Populate(services); this.ApplicationContainer = builder.Build(); return new AutofacServiceProvider(this.ApplicationContainer);
值得注意的几点:
ConfigureServices
的返回值修改为 IServiceProvider