丹阳网站建设,企业建设网站管理制度,天津互联网网页设计招聘,wordpress提示安装构建ASP.NET Core应用程序的时候#xff0c;依赖注入已成为了.NET Core的核心#xff0c;这篇文章#xff0c;我们理一理依赖注入的使用方法。不使用依赖注入首先#xff0c;我们创建一个ASP.NET Core Mvc项目#xff0c;定义个表达的爱服务接口#xff0c;中国小伙类实现… 构建ASP.NET Core应用程序的时候依赖注入已成为了.NET Core的核心这篇文章我们理一理依赖注入的使用方法。不使用依赖注入 首先我们创建一个ASP.NET Core Mvc项目定义个表达的爱服务接口中国小伙类实现这个类如下public interface ISayLoveService { string SayLove(); } public class CNBoyService : ISayLoveService { public string SayLove() { return 安红我喜欢你; } }在LoveController 控制器中调用 ISayLoveService的SayLove方法。public class LoveController : Controller { private ISayLoveService loveService; public IActionResult Index() { loveService new CNBoyService(); //中国小伙对安红的表达 ViewData[SayLove] loveService.SayLove(); return View(); } }输出如图:小结LoveController控制器调用ISayLoveService服务的SayLove方法我们的做法直接在控制器去new CNBoyService()实例对象也就是LoveController依赖ISayLoveService类。思考能不能有种模式new实例不要在使用的时候进行创建而是在外部或者有一个容器进行管理这不就是ioc思想吗好处代码的解耦、代码更好的维护等等。使用依赖注入上面的疑惑答案是肯定的有并且ASP.NET Core 支持依赖关系注入 (DI) 软件设计模式当然也可以兼容第三方。我们还使用上面的代码服务注册 在Startup类ConfigureServices方法中注册服务容器中的依赖关系 public void ConfigureServices(IServiceCollection services) { services.AddSingletonISayLoveService, CNBoyService(); services.AddControllersWithViews(); }在LoveControlle控制器中通过构造函数注入 private readonly ISayLoveService loveService; public LoveController(ISayLoveService loveService) { this.loveService loveService; } public IActionResult Index() { ViewData[SayLove] loveService.SayLove(); return View(); }LoveController 正在将ISayLoveService作为依赖项注入其构造函数中然后在Index方法中使用它。推荐将注入的依赖项分配给只读字段/属性以防止在方法内部意外为其分配另一个值。使用接口或基类抽象化依赖关系实现。小结在控制器中还有几种使用如[FromServices] 标签 、 HttpContext.RequestServices.GetServiceT()我们发现可以使用ASP.NET Core 提供了一个内置的服务容器 IServiceProvider。服务只需要在Startup.ConfigureServices 方法中注册然后在运行时将服务注入 到使用它的类的构造函数中。框架负责创建依赖关系的实例并在不再需要时对其进行处理。思考服务注册的时候使用的是 AddSingleton如services.AddSingletonISayLoveService, CNBoyService();还有其他的吗服务生命周期服务注册的时候ASP.NET Core支持指定三种生命周期如Singleton 单例Scoped 范围Transient 短暂的Singleton 仅创建一个实例。该实例在需要它的所有组件之间共享。因此始终使用同一实例。Scoped 每个范围创建一个实例。在对应用程序的每个请求上都会创建一个范围因此每个请求将创建一次注册为Scoped的任何组件。Transient 在每次被请求时都会创建并且永不共享。为了能够更好的裂解生命周期的概念我们把上面代码稍作改动做一个测试ISayLoveService 新增个属性LoveId类型为guidpublic interface ISayLoveService { Guid LoveId { get; } string SayLove(); } public interface ITransientSayLoveService : ISayLoveService { } public interface IScopedSayLoveService : ISayLoveService { } public interface ISingletonSayLoveService : ISayLoveService { } public interface ISingletonInstanceSayLoveService : ISayLoveService { }BoyService也很简单在构造函数中传入一个Guid,并对它进行赋值。public class BoyService : ITransientSayLoveService, IScopedSayLoveService, ISingletonSayLoveService, ISingletonInstanceSayLoveService { public BoyService():this(Guid.NewGuid()) { } public BoyService(Guid id) { LoveId id; } public Guid LoveId { get; private set; } public string SayLove() { return LoveId.ToString(); } } 每个实现类的构造函数中,我们都产生了一个新的guid,通过这个GUID,我们可以判断这个类到底重新执行过构造函数没有.服务注册代码如下 public void ConfigureServices(IServiceCollection services) { //生命周期设置为Transient因此每次都会创建一个新实例。 services.AddTransientITransientSayLoveService, BoyService(); services.AddScopedIScopedSayLoveService, BoyService(); services.AddSingletonISingletonSayLoveService, BoyService(); services.AddSingletonISingletonInstanceSayLoveService(new BoyService(Guid.Empty)); services.AddControllersWithViews(); }在LifeIndex方法中多次调用ServiceProvider的GetService方法获取到的都是同一个实例。 public IActionResult LifeIndex() { ViewData[TransientSayLove1] HttpContext.RequestServices.GetServiceITransientSayLoveService().SayLove(); ViewData[ScopedSayLove1] HttpContext.RequestServices.GetServiceIScopedSayLoveService().SayLove(); ViewData[SingletonSayLove1] HttpContext.RequestServices.GetServiceISingletonSayLoveService().SayLove(); ViewData[SingletonInstanceSayLove1] HttpContext.RequestServices.GetServiceISingletonInstanceSayLoveService().SayLove(); //同一个HTTP请求 在从容器中获取一次 ViewData[TransientSayLove2] HttpContext.RequestServices.GetServiceITransientSayLoveService().SayLove(); ViewData[ScopedSayLove2] HttpContext.RequestServices.GetServiceIScopedSayLoveService().SayLove(); ViewData[SingletonSayLove2] HttpContext.RequestServices.GetServiceISingletonSayLoveService().SayLove(); ViewData[SingletonInstanceSayLove2] HttpContext.RequestServices.GetServiceISingletonInstanceSayLoveService().SayLove(); return View(); }我们编写view页面来展示这些信息如下{ ViewData[Title] LifeIndex;
} div classrow div classpanel panel-default div classpanel-heading h2 classpanel-titleOperations/h2 /div div classpanel-body h3获取第一次/h3 dl dtTransient1/dt ddViewData[TransientSayLove1] /dd dtScoped1/dt ddViewData[ScopedSayLove1]/dd dtSingleton1/dt ddViewData[SingletonSayLove1] /dd dtInstance1/dt ddViewData[SingletonInstanceSayLove1]/dd /dl h3获取第二次/h3 dl dtTransient2/dt ddViewData[TransientSayLove2]/dd dtScoped2/dt ddViewData[ScopedSayLove2]/dd dtSingleton2/dt ddViewData[SingletonSayLove2]/dd dtInstance2/dt ddViewData[SingletonInstanceSayLove2]/dd /dl /div /div /div 运行代码第一次输出我们发现在一次请求中发现单例、范围的生命周期的guid 没有变化说明分别用的是同一个对象而瞬态guid不同说明对象不是一个。刷新之后查看运行效果我们发现通过刷新之后单例模式的guid还是跟首次看到的一样其他的都不同总结如果您将组件A注册为单例则它不能依赖已注册“作用域”或“瞬态”生存期的组件。一般而言组件不能依赖寿命短于其寿命的组件。如果默认的DI容器不能满足项目需求可以替换成第三方的如功能强大的Autofac。