Andrew Embler: A Concrete Guide to Dependency Injection
In a new post to his site Andrew Embler provides a concrete guide to dependency injection, a look at some of the foundational principles behind dependency injection, some problems and how they could be resolved.
As we develop concrete5, we are very conscious about keeping the code base modern, so that we don't have to go through a massive, painful rewrite like we did with version 5.7. This means that we're frequently deep in the weeds, applying new concepts and functionality to new and old code alike.
[...] Here, we're going to talk about Dependency Injection. There are numerous tutorials, blog posts and Stack Overflow questions, regarding Dependency Injection – but there's always room for some more.
He starts with a look at the "old days" where dependencies were created inside of the class methods as needed and not shared. He then covers some of the problems with this approach and how a move to dependency injection (DI) can help. In his example a database object instance is created inside of the method each time it's' needed. The move to DI helps to resolve those issues by passing in the database instance instead. He finishes the post trying to clear up some of the terminology confusions around DI and briefly mentions containers (dependency injection containers) and the role they play.