4 ms·
These days I find composition via dependency injection (of interfaces, not concrete implementations) to be a much better way to solve #3.
by twblalock 7y ago
These days I find composition via dependency injection (of interfaces, not concrete implementations) to be a much better way to solve #3.
- caseymarquis 7y agoI would argue you should inject concrete implementations until you need an interface. Why add unnecessary indirection?
- mrkeen 7y agoIt lets you constrain your logic code to depend as loosely as possible on its dependencies. If I start from a concrete 'FooDatabase' (and wait until later to interface it), I might start putting things like open(), close(), flush(), etc. into my logic code. Then when I interface it, it will just be unnecessary indirection. If I start from the other end and think "I just need somewhere to put my Foos", then I'll pass in a Consumer<Foo> and be done with it.