• RedFrank24@piefed.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    13 hours ago

    If you’ve used Dependency Injection before, you’ve used the principle of composition over inheritance. So, if you’ve ever used .Net (C#), Spring Boot (Java) or Laravel (PHP), you’ve likely used it. Modern C++ also has the DI pattern.

    Rust and Go force you to use composition and don’t support inheritance at all, so if you’ve used either of those languages, you’ve followed the practice, though Go doesn’t support DI out of the box. Functional languages like Haskell also use composition over inheritance.

    • aaaaaaaaargh@feddit.org
      link
      fedilink
      arrow-up
      0
      ·
      9 hours ago

      I think you’re confusing composition with aggregation. DI can’t be composition because the injected object is shared/borrowed, strong composition on the other hand requires the object to be owned. A composed child does not exist without its parent.

      • RedFrank24@piefed.social
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        6 hours ago

        Only if you’re going by the strict UML definition of composition, which doesn’t really apply here, since the industry has moved on a bit since UML was king.

        Either way, you can use DI to do composition in the strictest UML way, provided every single dependency is transient and creates a new instance every single time. Even then though, when most devs talk about composition, they aren’t referring to the strict UML definition.