Comparison
Dependency injectionvsDependency inversion
Dependency injection
the class asks for what it needs in its constructor and never goes looking, so a test can hand it anything.
Supplying a component's collaborators from outside instead of letting it construct or locate them. It is not a framework — passing an argument is dependency injection — and the whole benefit is that the dependency becomes visible in the signature and replaceable at the call site. It is the mechanical half of dependency inversion, which is the design principle about which direction the abstraction points.
Full entry →Dependency inversion
the business rules import the Postgres driver, so testing a discount calculation needs a database.
Making the important code depend on an interface it defines, and the infrastructure depend on that interface too, so the arrow points inward instead of outward. The practical effect is that the rules of your business can be tested and understood without a network, a database or a cloud account. It is the backbone of the ports-and-adapters argument, and the reason a senior engineer will push back on a domain object that knows what a HTTP status code is.
Full entry →