Z
Z
zlodiak2019-06-09 20:51:01
Python
zlodiak, 2019-06-09 20:51:01

What is property injection?

dependency injection is divided into 3 types:
1. constructor injection. in this case, the dependency is passed through the class constructor and then assigned to the class property

class A:
  def __init__(self, logger):
    self.logger = logger

2. method injection. in this case, the dependency is passed through the class method and is not needed in the bolt? In most cases, it is not assigned to a property of the class, but is assigned to a local variable of the method
class A:
  def set_logger(self, logger):
    logger_obj = logger
    logger_obj.write('blabla')

3. but with the third view, I don’t understand everything. Please help me understand what it is and, if possible, also give a small piece of code.
In general, for python, this type of dependency injection exists, or due to the peculiarities of the language, it is simple

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Romashkan, 2019-06-09
@zlodiak

Changing the public variable in which the embedded object lies. More precisely, the replacement of this object.

a = new A();
a.logger = new Logger();

In fact, of the three methods, constructor injection is preferable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question