欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net Core簡單使用Mvc內(nèi)置的Ioc

 更新時(shí)間:2018年03月31日 11:48:42   作者:反骨仔  
這篇文章主要為大家詳細(xì)介紹了.Net Core簡單使用Mvc內(nèi)置的Ioc,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文基于 .NET Core 2.0。

鑒于網(wǎng)上的文章理論較多,鄙人不才,想整理一份 Hello World(Demo)版的文章。 

目錄

場景一:簡單類的使用
場景二:包含接口類的使用
場景三:涉及引用類庫的使用 

場景一:簡單類的使用

類 DemoService.cs:

public class DemoService
 {
 public string Test()
 {
  return Guid.NewGuid().ToString();
 }
 }


控制器 DemoController.cs:

public class DemoController : Controller
 {
 private readonly DemoService _demoService;

 public DemoController(DemoService demoService)
 {
  _demoService = demoService;
 }

 public IActionResult Index()
 {
  return Json(_demoService.Test());
 }
 }

需要先在 Startup.cs 下的 ConfigureServices() 方法中進(jìn)行注冊才能使用,這里提供了三種方法,可以選擇自己喜歡的方式進(jìn)行注冊。

//方法一
 services.AddSingleton(typeof(DemoService), new DemoService());

 //方法二
 services.AddSingleton(typeof(DemoService));

//方法三
 services.AddSingleton<DemoService>();


執(zhí)行輸出結(jié)果,正常:

IOC 的容器目前有三種生命周期 Transient、Scoped 和 Singleton,使用方式大致相同,具體差異不在這里進(jìn)行敘述:

//范例
 services.AddTransient(typeof(DemoService));
 services.AddScoped<DemoService>();

場景二:包含接口類的使用

接口 IDemo2Service.cs:

public interface IDemo2Service
 {
 string Test();
 }

接口實(shí)現(xiàn) Demo2Service.cs:

public class Demo2Service : IDemo2Service
 {
 public string Test()
 {
  return Guid.NewGuid().ToString();
 }
 }

控制器 Demo2Controller.cs:

public class Demo2Controller : Controller
 {
 private readonly IDemo2Service _demoService;

 public Demo2Controller(IDemo2Service demoService)
 {
  _demoService = demoService;
 }

 public IActionResult Index()
 {
  return Json(_demoService.Test());
 }
 }

目前需要在類 Startup.cs 中的 ConfigureServices() 方法內(nèi)新增的注冊方法如下(可選其一):

 //方法一
 services.AddSingleton(typeof(IDemo2Service), new Demo2Service());

//方法二
 services.AddSingleton(typeof(IDemo2Service), typeof(Demo2Service));

 //方法三
 services.AddSingleton<IDemo2Service, Demo2Service>();

輸出結(jié)果正常:

場景三:涉及引用類庫的使用

我們先新增一個(gè)用于標(biāo)識作用的接口 IServiceSupport.cs,該接口沒有包含方法,只是一個(gè)標(biāo)識作用,有點(diǎn)類似 DDD 的聚合根接口 IAggregateRoot:

 public interface IServiceSupport
 {
 }

接口 IDemo3Service.cs

public interface IDemo3Service
 {
 string Test();
 }

接口實(shí)現(xiàn) Demo3Service.cs

public class Demo3Service : IDemo3Service
 {
 public string Test()
 {
  return Guid.NewGuid().ToString();
 }
 }

這次我們統(tǒng)一編寫一個(gè)方法將該類庫下的所有接口和實(shí)現(xiàn)進(jìn)行注冊:

 private static void AddSingletonServices(IServiceCollection services)
 {
  var asm = Assembly.Load(new AssemblyName("IocCoreDemo.Services"));
  var serviceTypes = asm.GetTypes()
  .Where(x => typeof(IServiceSupport).IsAssignableFrom(x) && !x.GetTypeInfo().IsAbstract);

  foreach (var serviceType in serviceTypes)
  {
  foreach (var serviceInterface in serviceType.GetInterfaces())
  {
   services.AddSingleton(serviceInterface, serviceType);
  }
  }
 }

因?yàn)槭褂昧朔瓷?,所以需?using System.Reflection;

這次我們在 Startup.cs 類中添加和修改的方法如圖所示:

Startup.cs 類中使用的有效命名空間如下:

using IocCoreDemo.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Reflection;

如果注入失敗,執(zhí)行結(jié)果便會(huì)如圖所示:

為什么會(huì)出現(xiàn)上圖的情況呢?因?yàn)樾【幫洶呀涌?IDemo3Service 繼承自接口 IServiceSupport 了,接下來我們只需要做出一個(gè)繼承的編寫操作即可:

再次執(zhí)行啟動(dòng),結(jié)果便如你所料:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論