# Injection through constructor (not using include...)

**URL:** <https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438>\
**Category:** Ideas\
**Created:** [December 19, 2017, 8:06pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438 "2017-12-19T20:06:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsoncastilho](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/robsoncastilho/32/189_2.png) [@robsoncastilho](https://discourse.dry-rb.org/u/robsoncastilho)\
**Post date:** [December 19, 2017, 8:06pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/1 "2017-12-19T20:06:00Z")

</div>

Hi, everyone

I’m coming from .Net “world” and I’m having a hard time finding dependency injection information with Ruby.

Recently, I’ve discovered Dry Container and started studying it. So I got to the point where I’d like to design my classes simply using constructor injection:

class SomeClass  
def initialize(some\_repository)  
@some\_repository = some\_repository  
end  
end

And the container should be able to resolve the dependencies. Is it possible?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [December 20, 2017, 5:48pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/2 "2017-12-20T17:48:38Z")

</div>

dry-auto\_inject uses constructor injection, it literally defines a constructor for you.

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [December 28, 2017, 1:00am UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/3 "2017-12-28T01:00:31Z")

</div>

The [How does it work?](http://dry-rb.org/gems/dry-auto_inject/how-does-it-work/) page from the docs might also help clarify how it works 🙂

---

<div class="post-metadata">

**Author:** ![robsoncastilho](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/robsoncastilho/32/189_2.png) [@robsoncastilho](https://discourse.dry-rb.org/u/robsoncastilho)\
**Post date:** [January 3, 2018, 4:28pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/4 "2018-01-03T16:28:55Z")

</div>

Got it!

I don’t like the idea of forcing my classes to include that constant. I think this pollutes the classes and may force changes to all of them if I change the container in the future (for some other that uses a different injection strategy).

I wish I could use constructor injection (coded by me) and let the container create all the object graph from the application entry point (concept named Composition Root/Agent). The container would be responsible to create the first object required (the web controller, for instance) and all dependencies, but I don’t know if it’s possible to intercept this creation in Ruby frameworks. I couldn’t find anything like that in any Ruby applications.

Sorry if I didn’t make myself clear (feel free to ask). I’ll keep studying Ruby and searching more content about DI in Ruby.

Thanks!

---

<div class="post-metadata">

**Author:** ![Philo](https://avatars.discourse-cdn.com/v4/letter/p/e495f1/32.png) [@Philo](https://discourse.dry-rb.org/u/Philo)\
**Post date:** [March 1, 2024, 1:01pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/5 "2024-03-01T13:01:06Z")

</div>

A bit of time has passed. But I consider the use of the Composition-Root pattern as well. You can absolutely define your own Composition-Root class. In rails you could introduce it in your application\_controller. So you would have all the creational behavior near the entry point of your application.

What are the key aspects?

- get rid of the dependency container and the auto\_inject.
- don’t pollute the classes with these depenency-constants.
- creational behavior in one single place
- when moving files/refactoring you do not have to search the whole codebase for the occurrences, you only have to change it in one place

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [March 1, 2024, 6:21pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/6 "2024-03-01T18:21:08Z")

</div>

I’m not certain I see the value of this in Ruby.

“Pollute” is too vague, can you articulate what that actually means? Multiple inheritance through modules is absolutely a normal thing to do.

> creational behavior in one single place

this is what the dependency container is for!

> when moving files/refactoring you do not have to search the whole codebase for the occurrences, you only have to change it in one place

this is what the dependency container is for!

---

<div class="post-metadata">

**Author:** ![Philo](https://avatars.discourse-cdn.com/v4/letter/p/e495f1/32.png) [@Philo](https://discourse.dry-rb.org/u/Philo)\
**Post date:** [March 1, 2024, 7:44pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/7 "2024-03-01T19:44:03Z")

</div>

Thanks for your response!

I am not particularly sure if this is the right approach for ruby or dynamically typed languages. But I am in the process of gathering more information. 🙂

> “Pollute” is too vague, can you articulate what that actually means?

Everywhere where you want to use the DI-Container, you have to introduce the container dependency:

```auto
class CreateUser
  include Import["users_repository"]

  def call(user_attrs)
    users_repository.create(user_attrs)
  end
end

```

If you want to auto-register these dependencies, then you need the full namespace of the dependency within the include of the container. My premise is, that if you would need to move the dependencies to another folder, then you have to search in all files where the dependency is included. Maybe that is a tradeoff with the auto-register, and you could name the dependency independently of its path. But there is still the need for the container to include.

The composition root pattern is pretty similar. But you would only use it in the beginning of your application/request.

---

<div class="post-metadata">

**Author:** ![Philo](https://avatars.discourse-cdn.com/v4/letter/p/e495f1/32.png) [@Philo](https://discourse.dry-rb.org/u/Philo)\
**Post date:** [March 1, 2024, 7:46pm UTC](https://discourse.dry-rb.org/t/injection-through-constructor-not-using-include/438/8 "2024-03-01T19:46:06Z")

</div>

I am reading this book: [Dependency Injection Principles, Practices, and Patterns](https://www.manning.com/books/dependency-injection-principles-practices-patterns)

Maybe it is not that practical for ruby. 🙂
