Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why Constructor Injection?

constructor injection
0
Posted

Why Constructor Injection?

0

Constructor Injection is hard to swallow for developers who have been living with Interface Lookup (the Avalon or OSGi way) or Setter Injection for long. We think it’s like TDD. Once you get the grasp of it, you don’t go back. Here are some benefits of Constructor Injection: • Constructor Injection makes a stronger dependency contract • It is more succinct in terms of lines of code • It is more succinct in terms dependency-statement-mechanism i.e. no XML, attributes, enabler interfaces etc • A component that is Interface/Implmenetation separated should have the interface be the contract offered to other components, and the implementation potentially having some extra methods that are nothing to do with the contract. • No indeterminate state. For setter injection and alike, not all the post instantiation setters may be called. Consider the non-container case for comp usage, if v1.2 of that comp introduces a new dependancy, with Constructor Injection the compiler will tell you.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123