Injecting Wisely Or Pros Of This And Cons Of That In Symfony

Why inject?

injectionIf your class’s dependencies are precise and demanding thus easy to inject you are facing with multiple bonuses like:

  • The class is more reusable
  • Easier to test
  • Easier to decouple from its buddies

If something is as powerful people are usually coming up with different ways of achieving it. The same here as multiple ways of injecting dependencies are available. Yet thy may not be considered equal as each way has its strong sides and weaknesses. You are to consider them all in order to choose the way that is fitting your current case. Thus we begin!

Setter Injection

A method I am finding quite nice and handy. Although, as any thing created by men, it has both pleasant features and those you never want to think about before getting in bed. So what are the strong sides here?

  • A setter injection is amazing with optional dependencies.
  • You may call it various times and it will be there to assist. When to use this? Perhaps when you are adding your dependency to your collection. It grants you with a variable amount of dependencies.

How’s it bad?

  • Its strong side is what makes it weaker. As usual. Regarding to the fact that you are able of calling it more than once while construction is in progress. Thus you will not be 100% sure your dependency is not being replaced somewhere during your object’s lifetime. But you are always able of distinctly writing setter to double-check if it was called before. 
  • Well, you will be forced to add checks (to know if the dependencies are actually injected) anyway as there is no way to be certain setter was called.

Property Injection

This is quite the injection to talk about. I was working hard, tried to be open-minded yet I am still finding a disadvantage after disadvantage. No positive thinking here, only hard truth:

  • You are not in control. When is the dependency set?
  • A dependency may get changed at practically any point of your object’s lifetime.
  • Type hinting is something you are simply incapable of using. That makes it harder to make sure your dependency is indeed injected. 

But sure all that may be done with a service container. A helpful fact, nothing more.

Constructor Injection

This one is actually the most shared way lots of people are using. You will be injecting dependencies with the assistance of your class constructor. What are the strong sides of such an approach?

  • Lest consider a case when the dependency actually is a strong must-have. The class will not be working without it, etc. In such a case the constructor will make your life a bit easier.
  • You are certain your dependency will not be changed as the constructor may be called once and once only. (which may as well be a weakness)

The disadvantages are that constructor injections are not just the right thing for optional dependences. It is also quite challenging to use it along with class hierarchies.

So now you know what you are dealing with and what to use actually.