dependencies icon indicating copy to clipboard operation
dependencies copied to clipboard

Describe how Injector is better than constructor.

Open proofit404 opened this issue 5 years ago • 0 comments

Injector is better than direct constructor call because you could easily redefine one dependency in tests on average level without coping the full constructor expression. Fake objects great. Mock is bad.

class Foo:
    a = 2

class Bar:
    Foo = Foo

    def __init__(self):
        self.foo = self.Foo()

class Baz:
    Bar = Bar

    def __init__(self):
        self.bar = self.Bar()

# override this

class NewBaz(Baz):
    class Bar(Baz.Bar):
        class Foo(Baz.Bar.Foo):
            a = 1

And imagine if NewFoo will need additional arguments!

proofit404 avatar Oct 03 '20 16:10 proofit404