Skip to main content

Claude/ChatGPT Prompt to Review .NET DI Patterns That Scale

Audit ASP.NET Core dependency injection: service lifetimes, keyed services, typed HttpClient, options validation, and common anti-patterns to flag.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model a senior .NET architect reviewing dependency injection in a large ASP.NET Core app, returning concrete recommendations with code. You supply [dotnet_version], [app_shape], [pain_points], and [registration_style], and it audits service lifetimes for captive-dependency risks, recommends module-style registration, demonstrates keyed services, sets up a typed HttpClient with resilience, applies the options pattern with ValidateOnStart, and flags anti-patterns.

The structure works because it starts where the expensive, subtle bugs actually live — service lifetimes. A Singleton holding a Scoped service leaks state across requests and nobody notices until production, so the audit leads with lifetime mismatches before touching anything cosmetic. [registration_style] tells the model what your Program.cs looks like now (for example, everything registered inline), [pain_points] focuses the review on your known issues, [app_shape] sets the scale, and [dotnet_version] decides which features like keyed services are available. Because it returns before/after registration snippets rather than abstract advice, you can compare your current wiring against the recommended module-style version directly and apply the change with full context for why it matters.

When to use it

  • Your Program.cs has grown into a tangled wall of inline registrations.
  • You suspect captive dependencies — a Singleton capturing a Scoped service.
  • You want module-style registration to keep startup clean and testable.
  • You need keyed services to swap implementations by key on a modern .NET version.
  • You're wiring a typed HttpClient and want named config plus resilience handlers.
  • You want config classes validated at startup with the options pattern.

Example output

Expect a prioritized findings list led by lifetime and captive-dependency issues, followed by before/after registration snippets: the tangled inline Program.cs versus module-style extension methods, keyed-service examples, a typed HttpClient registration with resilience, and options-pattern config with ValidateOnStart. Anti-patterns like service locator and over-Singleton-ing are called out with the fix.

Pro tips

  • Describe [pain_points] honestly — "suspected captive deps" or "bloated Program.cs" steers the audit to what actually hurts.
  • Set [dotnet_version] accurately; keyed services and some options features only exist on newer versions.
  • Start with the step 1 lifetime findings; mismatched lifetimes cause more incidents than any missing feature.
  • Use [registration_style] to show the current state so the before/after snippets reflect your real code.
  • Apply ValidateOnStart so misconfigured options fail fast at boot instead of mid-request.
  • After refactoring registration into modules, re-run the audit to confirm no new captive dependencies crept in.
  • Use the typed HttpClient with resilience handlers for outbound calls; a raw HttpClient created per request is a classic source of socket exhaustion.
  • Reach for keyed services only where you genuinely swap implementations by key — overusing them just trades one form of complexity for another.
  • Treat the anti-pattern flags as a checklist for the whole team, since service locator and over-Singleton-ing tend to spread by copy-paste once one example exists.

Frequently Asked Questions

What is a captive dependency and why does this prompt prioritize it?
A captive dependency happens when a longer-lived service, like a Singleton, holds a shorter-lived one, like a Scoped service, leaking state across requests. The audit leads with it because these mismatches cause subtle production bugs that are hard to trace later.
Does it require a specific .NET version for keyed services?
Keyed services require a recent .NET version. Set `[dotnet_version]` accurately, since older versions lack keyed-service support and some options-pattern features, and the recommendations adjust to what your target version actually offers.
Will it rewrite my Program.cs or just suggest changes?
It returns before/after registration snippets and a prioritized findings list, including module-style extension methods to replace inline registration. You apply the changes yourself, which lets you review each lifetime decision rather than accepting a blind rewrite.
Can it catch anti-patterns beyond lifetimes?
Yes. It flags service locator usage, over-Singleton-ing, and captive dependencies specifically. Describing your known issues in `[pain_points]` helps it focus the audit, though it still checks the standard anti-pattern set regardless.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in C# & .NET Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support