Illustrates a basic dependency injection setup using interfaces, concrete classes, and a composition root, showcasing how a service depends on a singleton
---
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)
}
}
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.
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.
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.