简体   繁体   English

在ASP.NET Core DI容器中注入一条记录是否可以?

[英]Is it ok to inject a record in the ASP.NET Core DI container?

Records make my code shorter.记录使我的代码更短。 For example, instead of例如,而不是

public class OrderService
{
  private readonly EcommerceContext _context;

  public OrderService(EcommerceContext context)
  {
    _context = context;
  }
}

I just have我只有

public record OrderService(EcommerceContext Context)

So, my question is, is it ok to inject a record in the DI container, or does it have any disadvantage?所以,我的问题是,在 DI 容器中注入一条记录是否可以,或者它有什么缺点吗?

A record is just a class with a auto-generated constructor;一条记录只是一个带有自动生成的构造函数的 class; The DI Container doesn't distinguish between the two of them. DI 容器不区分它们两者。

Personally, I find record types a great way to remove the infrastructural code/cruft needed to practice DI and loose coupling.就我个人而言,我发现记录类型是一种消除实践 DI 和松散耦合所需的基础结构代码/垃圾的好方法。 Private fields, constructors, and private field assignments are just boilerplate code required to allow our code to be loosely coupled.私有字段、构造函数和私有字段分配只是允许我们的代码松散耦合所需的样板代码。 Records allow reducing this code to the bare minimum, which is great.记录允许将此代码减少到最低限度,这很棒。

The only downside that record types have is that, with the current version of C#, there's no way to guard incoming dependencies from being null .记录类型的唯一缺点是,对于 C# 的当前版本,无法防止传入的依赖项成为null For me this isn't big of a deal in most cases, in which case I like to use record types.对我来说,在大多数情况下这没什么大不了的,在这种情况下我喜欢使用记录类型。 If null checks are mandatory (for instance because you are writing a reusable library, eg something you ship through NuGet), you need to fall back to old-school classes with manually implemented constructors.如果 null 检查是强制性的(例如因为您正在编写一个可重用的库,例如您通过 NuGet 发送的东西),您需要回退到使用手动实现的构造函数的老式类。

When writing our book on DI , Mark Seemann and I actually discussed the use of record types as primary demonstration of constructor injection in our book.在撰写有关 DI 的书时,Mark Seemann 和我实际上讨论了使用记录类型作为我们书中构造函数注入的主要演示。 We, however, decided not to use record types in our book, mainly for three reasons:然而,我们决定不在我们的书中使用记录类型,主要出于三个原因:

  • At the time of publishing, C# 9, which included record types, was not yet available在发布时,包含记录类型的 C# 9 尚不可用
  • The loss of the ability to do pre-condition checking was a deal breaker for the examples of the book, because we didn't want to assume a DI Container was used for composing instances;丧失进行前提条件检查的能力是本书示例的一大障碍,因为我们不想假设 DI 容器用于组合实例; there's also a practice called Pure DI which we describe extensively in the book.还有一种称为Pure DI的实践,我们在本书中对其进行了广泛描述。 With Pure DI there is no tooling that guarantees not injecting null values into constructors.使用 Pure DI,没有工具可以保证不将null值注入构造函数。
  • We wanted the book to be readable for developers working in other OOP languages as well.我们希望这本书对于使用其他 OOP 语言的开发人员来说也是可读的。 Even though record types will be very common within the C# community within a few years, that might not be the case for many other OOP languages.尽管记录类型将在几年内在 C# 社区中非常普遍,但对于许多其他 OOP 语言来说情况可能并非如此。

Perhaps we'll change our mind in a (hypothetical) third edition of the book.也许我们会在本书的(假设的)第三版中改变主意。 This might especially happen when a future version of C# allows record type constructors to automatically check (and prevent) null arguments.当 C# 的未来版本允许记录类型构造函数自动检查(并阻止)null arguments 时,尤其可能会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将方法注入 ASP.NET 内核中的 DI 容器? - How to inject method into DI Container in ASP.NET Core? 如何使用 ASP.NET Core DI 注入连接字符串 - How to inject connection string using ASP.NET Core DI 无法使用 ASP.NET Core DI 注入委托 - Can't inject a delegate using ASP.NET Core DI ASP.NET Core 中的 Serilog DI,要注入哪个 ILogger 接口? - Serilog DI in ASP.NET Core, which ILogger interface to inject? 如何使用默认的ASP.NET Core DI容器在类中注入单个接口的多个服务类实现 - How to inject multiple service class implementation of single interface in class using default asp.net core DI container ASP.Net Core从DI容器获取服务 - ASP.Net Core Get Services From DI Container 如何验证 ASP.NET Core 中的 DI 容器? - How do I validate the DI container in ASP.NET Core? 在 ASP.net 核心应用程序中使用 IMemoryCache 和 Unity DI Container - Use IMemoryCache with Unity DI Container in ASP.net core application 使用 ASP.NET 核心 DI 容器注册“类型化”gRPC 客户端 - Registering a “typed” gRPC client with the ASP.NET Core DI container 如何在Asp.Net Core DI上注入具有不同生命周期的同一个类? - How can I inject same class with different Life Cycles on Asp.Net Core DI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM