Skip to content

Conversation

RyanLiu99
Copy link
Contributor

@RyanLiu99 RyanLiu99 commented Aug 17, 2025

Over years I have seen multiple companies use singleton as default IoC scope.

So I want to update guide line to make clear what is the cost, suggest at least avoid singleton for stateless service

Summary

I add a list to list what are the drawbacks of singleton, emphasis when and esp. when not to use Singleton IoC scope.


Internal previews

📄 File 🔗 Preview link
docs/core/extensions/dependency-injection-guidelines.md Dependency injection guidelines

…for stateless service

Over years I have seen multiple companies use singleton as default IoC scope. 

So I want to update guide line to make clear what is the cost, suggest at least avoid singleton for stateless service
@RyanLiu99 RyanLiu99 requested review from a team and IEvangelist as code owners August 17, 2025 22:32
@dotnetrepoman dotnetrepoman bot added this to the August 2025 milestone Aug 17, 2025
Copy link
Contributor

PoliCheck Scan Report

The following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 issues. Other issues are also a high priority. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans.

✅ No issues found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Aug 17, 2025
@IEvangelist IEvangelist requested a review from Copilot October 13, 2025 13:17
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the dependency injection guidelines to provide more comprehensive guidance on singleton service lifetimes, specifically advising against using singleton for stateless services. The update addresses common misconceptions where singleton is used as a default IoC scope by explaining the costs and drawbacks.

  • Clarifies when to use factory pattern for IDisposable instances
  • Adds detailed guidance on singleton usage with emphasis on avoiding singleton for stateless services
  • Lists specific considerations and drawbacks of singleton lifetime including thread safety, coupling, testing challenges, and performance implications

@IEvangelist IEvangelist enabled auto-merge (squash) October 13, 2025 13:22
@IEvangelist IEvangelist merged commit 89c72fe into dotnet:main Oct 13, 2025
8 checks passed
@RyanLiu99 RyanLiu99 deleted the patch-5 branch October 13, 2025 17:03
@andriy-genndy
Copy link

IMO this updated guideline 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 you probably see as a safe "lowest common denominator" approach that'd keep them 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, makes it hard to impossible to advocate for clean stateless-by-default design now that "Microsoft said it's a bad idea".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants