dependency-injectionumlsequence-diagram

How do I represent dependency injection in sequence diagram?


We are building an ASP.Net Web API project using Repository pattern with Dependency Injection. I don't want to create an incomplete diagram, but I also don't want to have much details on it.

What would be the right or at least the suggested way to ilustrate DI in sequence diagrams?


Solution

  • You don't present the wiring of your components in a sequence diagram. The wiring of your components is an infrastructural concern, but a sequence diagram is meant to show how components interact with each other. Trying to add the wiring of your components in your sequence diagrams just obfuscates the diagrams without adding any useful information.

    When you take a peek in Dependency Injection, Principles, Practices, and Patterns, you'll see that we (i.e. Mark Seemann and I) sometimes do actually show the DI composition process, but that's mainly because the book tries to teach DI. Take a look at figure 4.5 (page 95), for instance:

    Figure 4.5 of DIPPP

    Besides the interaction between the two components (HomeController and ProductService), the diagram shows how the two components are composed by the Composition Root, before they are used. This is especially useful in this case, as the section teaches how Constructor Injection works.

    For understanding the interaction between components, however, this level of detail is unneeded. Most sequence diagrams in the book, therefore, hide these details, such as figure 6.6 (page 181):

    Figure 6.6 of DIPPP