Is dependency injection really that useful in this situation?

Referring to : https://github.com/gotar/dry-web-skeleton/blob/master/lib/commands/update_post.rb

Is there really a significant advantage to doing this :

include Blog::Import[‘validations.post_validator’]
result = post_validator.call(params)

instead of this ?

result = PostValidator.call(params)

I appreciate that it’s decoupled from the concrete class being used, so in some theoretical sense it is a better design, but what real word benefits will this have?

The only real advantage I can think of is that I can change the dependency in configuration without touching this class. That doesn’t seem like much of an advantage to me. A disadvantage is that it’s now not clear what my validator class is, so I can’t jump directly to it using my IDE - so i’ll be slower. If I want to change the validator class then what’s wrong with just changing the class name in the code?

There are many difficult challenges I experience during software development and none of them are related to the problem using DI seems to solve. To me, it just seems like something nice-to-have but doesn’t have any real world impact. Maybe I’m missing something here? You could argue that it’s easier to test, but in this case I think you’d still want to use the PostValidator class during testing.

Thanks

Yep, I recommend referring to schema constants directly. DI is an overkill in this case.

1 Like