dotnet-di

Explain Dependency Injection principles and patterns for .NET applications.

Updated Feb 5, 2026
One-click install
npx skills add https://github.com/Objective-Arts/lens --skill dotnet-di
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-di
Source: https://github.com/Objective-Arts/lens/tree/main/canon/csharp/dotnet-di
Command: npx skills add https://github.com/Objective-Arts/lens --skill dotnet-di

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers understand and implement robust Dependency Injection (DI) patterns in .NET, leading to more testable, maintainable, and loosely coupled code.

Core Features & Use Cases

  • Core Principles: Explains constructor injection, service lifetimes, and the Composition Root.
  • Anti-Patterns: Identifies and provides solutions for common DI pitfalls like Service Locator and Captive Dependencies.
  • Testing: Demonstrates how to effectively unit and integration test DI-enabled applications.
  • Use Case: A developer struggling with complex object wiring and unit testing in their ASP.NET Core application can use this Skill to refactor their code following best practices.

Quick Start

Review the core principles of constructor injection and service lifetimes for .NET applications.

Frequently Asked Questions about dotnet-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I configure dependency injection in ASP.NET Core to make my C# code testable?

To configure dependency injection in ASP.NET Core, use constructor injection at the Composition Root to wire services, ensuring loosely coupled C# code that is highly testable and maintainable. This approach simplifies mocking during unit testing.

What are common dependency injection anti-patterns in .NET and how do I fix them?

Common dependency injection anti-patterns in .NET include the Service Locator pattern and Captive Dependencies. Fix them by applying constructor injection and correctly matching service lifetimes to prevent captive dependency issues.

How do service lifetimes work in .NET dependency injection?

Service lifetimes in .NET dependency injection define how long a service instance persists. Selecting the correct lifetime prevents Captive Dependencies and ensures resources are managed effectively throughout your application's execution scope.

What is the Composition Root in .NET application design?

The Composition Root in .NET application design is the single location where object graphs are wired together. It applies dependency injection principles near the application's entry point to centralize configuration and maintain loose coupling.

Why does my unit testing fail with complex object wiring in C#?

Unit testing fails with complex object wiring in C# when dependencies are tightly coupled. Refactoring to use constructor injection and dependency injection design patterns allows effective mocking and yields easily testable code.