Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, May 30, 2016 7:00 AM
Hello All,
Trying to learn and code so inversion of control principle can be applied. Now see the difference between property injection and constructor injection. But want to know when to use one or the other?
Can i continue to use only property injection just so i dont need to worry about the constructor injection?
i am a novice and a student
All replies (3)
Monday, May 30, 2016 6:52 PM ✅Answered | 2 votes
>>But want to know when to use one or the other?
If the class itself has a strong dependency upon the interface/object that you inject it with, you should use constructor injection as the class is pretty much useless without the dependency, i.e. it makes no sense to instantiate it without it. A constructor therefore enforces the dependency requirement.
But if it makes sense for the class to be able to do its job without using the dependency, you could use property injection. When using property injection the dependency may or may not used depending on whether the property is actually invoked.
So it depends.
Please refer to the following threads for more information about this:
http://stackoverflow.com/questions/1503584/dependency-injection-through-constructors-or-property-setters
http://stackoverflow.com/questions/21218868/setter-injection-vs-constructor-injection
http://programmers.stackexchange.com/questions/300706/dependency-injection-field-injection-vs-constructor-injection
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
Tuesday, May 31, 2016 2:07 PM ✅Answered | 1 vote
SETTER INJECTION
Setter injection is the client exposes a setter method that the injector uses to inject the dependency. Especially in C#, we use property setter to do so.
CONSTRUCTOR INJECTION
Constructor injection is the dependencies are provided through a class constructor.
THE MUTABILITY OF POLYMORPHISM
The mutability of polymorphism is a concept that I create which means in a segregated changing point, or here we say injection point, if once we inject some concrete service and this injection point seldom change or never change again, I call it immutable polymorphism; if at runtime the injection point may change service according to different condition and it happens frequently, I call it mutable polymorphism; if the injection point may change service or may not change, the change happens somethings but not so frequently, I call it potential polymorphism.
immutable polymorphism – once be injected, the injection point will not change anymore
potential polymorphism – one instance of client may not change the injection service, but different instance of client may choose different injection service
mutable polymorphism – the injection point will change service at runtime
For those potential polymorphism, it is better to choose Constructor injection, because construction injection also has the chance to choose different service according to context by passing different constructor parameter. But once constructor is called and the client is created, the injection point is settled. So different client instance may have different service, but each one of them can not change its injection point at runtime.
For those mutable polymorphism, it is better to use Setter injection, because setter is the most flexible way to inject dependency, you can change it anytime. The injection point can change anytime at runtime by assigning different service to setter.
ref:
Monday, May 30, 2016 3:20 PM | 1 vote
Trying to learn and code so inversion of control principle can be applied. Now see the difference between property injection and constructor injection. But want to know when to use one or the other?
There is no set rules as to which one to use. It's which ever one works for you and you feel comfortable is using.