dotnet / dotnet/docs

dependency-injection-guidelines.md blanket advice on avoiding singletons is misleading

Open
#49,140 1 comment 0 reactions 0 assignees View on GitHub
:watch: Not Triaged
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Describe the issue or suggestion

The following PR introduced new guidelines specifically around using Singleton lifetime in DI-centric application designs: https://github.com/dotnet/docs/pull/47991

The guideline clearly sides with "make everything transient by default" as the safer (major emphasis) and more performant (the last bullet point) approach.

IMO this seems to be giving a dangerously blanket advice to 6-8 million .net developers, nearly-encouraging them to swipe state management problems under the carpet of transient lifetime, instead of striving for clean stateless design and zero-allocations as seems to be the default guideline in the Java/Spring community. I understand the pragmatism of giving .net developers what the author likely saw as a safe "lowest common denominator" approach that'd keep an average LOB development team out of trouble, and in the legacy codebases with no design governance making everything transient by default may indeed be the only viable survival strategy, but framing it as THE best practice for all codebases (no qualification), makes it hard to impossible to advocate for clean stateless-by-default designs now that "Microsoft said it's a bad idea".

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 8d1c8ef1-6a01-df24-34ad-570f88df9303
* Version Independent ID: 7fb2be80-cb54-27bf-4f93-423d8540429e
* Platform ID: df1bff59-742c-a7b3-cfc6-4fc1f6510458
* Content: [Dependency injection guidelines - .NET](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines)
* Content Source: [docs/core/extensions/dependency-injection-guidelines.md](https://github.com/dotnet/docs/blob/main/docs/core/extensions/dependency-injection-guidelines.md)
* Service: **dotnet-fundamentals**
* GitHub Login: @IEvangelist
* Microsoft Alias: **dapine**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.