How to show Dependency Injection on a UML class diagram?
You display class dependencies by using a dashed line with a pointed arrow that points from the client model element to the supplier model element.
I think that how you provide the dependencies to the client is irrelevent at the UML level.
First of all, UML and DI are very much orthogonal concepts:
- UML concerns itself with describing interactions, relationships and dependencies between concrete entities.
- DI describes how dependencies are to be resolved.
Thus, UML diagrams don't really have a mechanism to describe DI. However, I'm sure if DI is involved, you would see certain patterns evolve, including (but not limited to):
- at least two entities per dependency, that is (1) the interface and (2) at least one concrete implementation
- functions/methods that take in dependency interfaces, but (usually) no functions/methods that take in implementations of dependency implementations
- one or multiple entities to represent the injection mechanism itself
Martin Fowler shows it like this: