如何利用.Net Core MVC内置的Ioc容器实现依赖注入的最佳实践?

2026-04-01 11:081阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1093个文字,预计阅读时间需要5分钟。

如何利用.Net Core MVC内置的Ioc容器实现依赖注入的最佳实践?

本教程基于.NET Core 2.0。鉴于网上相关文章较多,且内容较为分散,本文旨在整理出一篇简洁的Hello World(Demo)版本文章。

目录

1.场景一:简单的使用

2.场景二:包含接口类的使用

3.场景三:涉及引用库的使用

1. 场景一:简单的使用

本节将介绍.NET Core 2.0环境下的基本操作,包括创建项目、编写代码等。

2. 场景二:包含接口类的使用本节将讲解如何在项目中使用接口类,以及如何实现接口。

3. 场景三:涉及引用库的使用本节将介绍如何引入外部库,以及如何使用库中的功能。

本文基于 .NET Core 2.0。

鉴于网上的文章理论较多,鄙人不才,想整理一份 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() 方法中进行注册才能使用,这里提供了三种方法,可以选择自己喜欢的方式进行注册。

//方法一 services.AddSingleton(typeof(DemoService), new DemoService()); //方法二 services.AddSingleton(typeof(DemoService)); //方法三 services.AddSingleton<DemoService>();

执行输出结果,正常:

IOC 的容器目前有三种生命周期 Transient、Scoped 和 Singleton,使用方式大致相同,具体差异不在这里进行叙述:

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

场景二:包含接口类的使用

接口 IDemo2Service.cs:

public interface IDemo2Service { string Test(); }

接口实现 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() 方法内新增的注册方法如下(可选其一):

//方法一 services.AddSingleton(typeof(IDemo2Service), new Demo2Service()); //方法二 services.AddSingleton(typeof(IDemo2Service), typeof(Demo2Service)); //方法三 services.AddSingleton<IDemo2Service, Demo2Service>();

输出结果正常:

如何利用.Net Core MVC内置的Ioc容器实现依赖注入的最佳实践?

场景三:涉及引用类库的使用

我们先新增一个用于标识作用的接口 IServiceSupport.cs,该接口没有包含方法,只是一个标识作用,有点类似 DDD 的聚合根接口 IAggregateRoot:

public interface IServiceSupport { }

接口 IDemo3Service.cs

public interface IDemo3Service { string Test(); }

接口实现 Demo3Service.cs

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

这次我们统一编写一个方法将该类库下的所有接口和实现进行注册:

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); } } }

因为使用了反射,所以需要 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;

如果注入失败,执行结果便会如图所示:

为什么会出现上图的情况呢?因为小编忘记把接口 IDemo3Service 继承自接口 IServiceSupport 了,接下来我们只需要做出一个继承的编写操作即可:

再次执行启动,结果便如你所料:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:IOC

本文共计1093个文字,预计阅读时间需要5分钟。

如何利用.Net Core MVC内置的Ioc容器实现依赖注入的最佳实践?

本教程基于.NET Core 2.0。鉴于网上相关文章较多,且内容较为分散,本文旨在整理出一篇简洁的Hello World(Demo)版本文章。

目录

1.场景一:简单的使用

2.场景二:包含接口类的使用

3.场景三:涉及引用库的使用

1. 场景一:简单的使用

本节将介绍.NET Core 2.0环境下的基本操作,包括创建项目、编写代码等。

2. 场景二:包含接口类的使用本节将讲解如何在项目中使用接口类,以及如何实现接口。

3. 场景三:涉及引用库的使用本节将介绍如何引入外部库,以及如何使用库中的功能。

本文基于 .NET Core 2.0。

鉴于网上的文章理论较多,鄙人不才,想整理一份 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() 方法中进行注册才能使用,这里提供了三种方法,可以选择自己喜欢的方式进行注册。

//方法一 services.AddSingleton(typeof(DemoService), new DemoService()); //方法二 services.AddSingleton(typeof(DemoService)); //方法三 services.AddSingleton<DemoService>();

执行输出结果,正常:

IOC 的容器目前有三种生命周期 Transient、Scoped 和 Singleton,使用方式大致相同,具体差异不在这里进行叙述:

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

场景二:包含接口类的使用

接口 IDemo2Service.cs:

public interface IDemo2Service { string Test(); }

接口实现 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() 方法内新增的注册方法如下(可选其一):

//方法一 services.AddSingleton(typeof(IDemo2Service), new Demo2Service()); //方法二 services.AddSingleton(typeof(IDemo2Service), typeof(Demo2Service)); //方法三 services.AddSingleton<IDemo2Service, Demo2Service>();

输出结果正常:

如何利用.Net Core MVC内置的Ioc容器实现依赖注入的最佳实践?

场景三:涉及引用类库的使用

我们先新增一个用于标识作用的接口 IServiceSupport.cs,该接口没有包含方法,只是一个标识作用,有点类似 DDD 的聚合根接口 IAggregateRoot:

public interface IServiceSupport { }

接口 IDemo3Service.cs

public interface IDemo3Service { string Test(); }

接口实现 Demo3Service.cs

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

这次我们统一编写一个方法将该类库下的所有接口和实现进行注册:

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); } } }

因为使用了反射,所以需要 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;

如果注入失败,执行结果便会如图所示:

为什么会出现上图的情况呢?因为小编忘记把接口 IDemo3Service 继承自接口 IServiceSupport 了,接下来我们只需要做出一个继承的编写操作即可:

再次执行启动,结果便如你所料:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

标签:IOC