Dependency Injection Root Bind Scenario

System Design · class diagram · MIT

Illustrates a basic dependency injection setup using interfaces, concrete classes, and a composition root, showcasing how a service depends on a singleton

Source: https://github.com/DevTeam/Pure.DI/blob/59be676eb57880ba0eebc809653965fcfd0908b7/readme/root-binding.md
Curated by DevTeam
Dependency Injection DI IoC Class Diagram Software Architecture Design Patterns Pure.DI

Mermaid source

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	DbConnection --|> IDbConnection
	OrderService --|> IOrderService
	Composition ..> OrderService : IOrderService OrderService
	OrderService o-- "Singleton" DbConnection : IDbConnection
	namespace Pure.DI.UsageTests.Basics.RootBindScenario {
		class Composition {
		<<partial>>
		+IOrderService OrderService
		}
		class DbConnection {
				<<class>>
			+DbConnection()
		}
		class IDbConnection {
			<<interface>>
		}
		class IOrderService {
			<<interface>>
		}
		class OrderService {
				<<class>>
			+OrderService(IDbConnection connection)
		}
	}

What this diagram shows

This class diagram depicts a fundamental dependency injection pattern. It shows OrderService depending on IDbConnection, with DbConnection as its concrete implementation. The Composition class acts as a composition root, responsible for resolving IOrderService and managing the DbConnection as a singleton.

When to use it

Use this diagram to explain core dependency injection principles, demonstrate how interfaces decouple components, or illustrate the role of a composition root in an application's startup. It's suitable for onboarding new developers to a DI-enabled codebase.

How to adapt it for your project

Modify the diagram to introduce more services and dependencies, change the scope of dependencies (e.g., from singleton to transient), or add different implementations for existing interfaces. You can also expand the Composition class to show more complex dependency graphs.

Key concepts