简体   繁体   English

使用服务集合来配置设置或在.Net Core中添加单例之间有什么区别?

[英]What is the difference between using the service collection to configure settings or adding a singleton in .Net Core?

On startup of our asp net core applications we read a whole bunch of configuration from our appsettings.json and map it to a concrete type that we inject in our service layer. 在启动ASP网络核心应用程序时,我们从appsettings.json读取了一堆配置并将其映射到我们注入服务层的具体类型。

Consider the two examples below : 考虑以下两个示例:

A : services.AddSingleton(Configuration.GetSection("Auth0").Get<Auth0Settings>()); 答: services.AddSingleton(Configuration.GetSection("Auth0").Get<Auth0Settings>());

B : services.Configure<Auth0Settings>(Configuration); B: services.Configure<Auth0Settings>(Configuration);

I know using option AI can simply inject the concrete type into my constructor and use it whereas with option BI am required to use the Options Pattern 我知道使用选项AI可以简单地将具体类型注入到我的构造函数中并使用它,而使用选项BI则需要使用选项模式

Im unsure what benefits option B provides over option A and why I would use it. 不确定选项B相对于选项A有什么好处,为什么我会使用它。

In the end both approaches give the same result and you cann access an instance of Auth0Settings via DI in eg your controllers. 最后,两种方法都给出相同的结果,您Auth0Settings通过DI在例如控制器中访问Auth0Settings实例。

For me there are two arguments in favor of the Options-Pattern . 对我来说,有两个论点支持Options-Pattern

  1. Expressivness - you make a clear destinction about application logic (services) and configuration (options). 可表达性 -您明确确定了应用程序逻辑(服务)和配置(选项)。 This make your code better to read and your intent easer to understand. 这使您的代码更易于阅读,使您的意图更易于理解。

  2. Flexibility - the Options-Pattern provides additional features (like validation and post-processing) 灵活性 -选项模式提供了其他功能(例如验证和后处理)

Although you might not need the additional features the first argument is still on the plate. 尽管您可能不需要其他功能,但第一个参数仍然存在。

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

相关问题 托管服务和Singleton服务之间的.NET核心差异 - .NET Core Difference between Hosted Service and Singleton Service Z2D50972FECD3761229545mvc507FZ1项目中的static class和singleton有什么区别? - What is the difference between static class and singleton in .net mvc project? .NET Core和PCL有什么区别? - What's the difference between .NET Core and PCLs? 使用 .net core CLI 和 .net core 控制台应用程序有什么区别? - What is the difference between using a .net core CLI and a .net core console app? 在 Asp.Net Core 应用程序的 Singleton 中使用 Scoped 服务 - Using a Scoped service in a Singleton in an Asp.Net Core app “.NET Core”和“.NET Core App”有什么区别? - What is the difference between `.NET Core` and `.NET Core App`? services.Configure()和services.AddOptions有什么区别 <T> ().bind()在ASP.NET Core中加载配置时? - What is the difference between services.Configure() and services.AddOptions<T>().Bind() when loading configuration in ASP.NET Core? 将.settings添加到.NET Core项目 - Adding .settings to .NET Core project .NET Core在另一个单例服务中注入单例服务 - .NET Core inject singleton service in another singleton service .net core 2.1中单例注入方法的区别 - Difference of singleton injection methods in .net core 2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM