From c5ebc871dc801f4acb171a2f677e86d470d5f551 Mon Sep 17 00:00:00 2001 From: happyaccidents Date: Tue, 26 Nov 2013 14:35:06 +0000 Subject: [PATCH 1/5] Renamed posts to allow Dependency Injection to be added --- _posts/{06-01-01-Databases.md => 08-01-01-Databases.md} | 0 _posts/{07-02-01-Errors.md => 08-02-01-Errors.md} | 0 _posts/{07-03-01-Exceptions.md => 08-03-01-Exceptions.md} | 0 _posts/{08-01-01-Security.md => 09-01-01-Security.md} | 0 ...plication-Security.md => 09-02-01-Web-Application-Security.md} | 0 ...{08-03-01-Password-Hashing.md => 09-03-01-Password-Hashing.md} | 0 _posts/{08-04-01-Data-Filtering.md => 09-04-01-Data-Filtering.md} | 0 ...-01-Configuration-Files.md => 09-05-01-Configuration-Files.md} | 0 ...{08-06-01-Register-Globals.md => 09-06-01-Register-Globals.md} | 0 .../{08-07-01-Error-Reporting.md => 09-07-01-Error-Reporting.md} | 0 _posts/{09-01-01-Testing.md => 10-01-01-Testing.md} | 0 ...-Driven-Development.md => 10-02-01-Test-Driven-Development.md} | 0 ...ven-Development.md => 10-03-01-Behavior-Driven-Development.md} | 0 ...y-Testing-Tools.md => 10-04-01-Complementary-Testing-Tools.md} | 0 ...rvers-and-Deployment.md => 11-01-01-Servers-and-Deployment.md} | 0 ...Platform-as-a-Service.md => 11-02-01-Platform-as-a-Service.md} | 0 ...icated-Servers.md => 11-03-01-Virtual-or-Dedicated-Servers.md} | 0 _posts/{10-04-01-Shared-Servers.md => 11-04-01-Shared-Servers.md} | 0 ...-your-Application.md => 11-05-01-Building-your-Application.md} | 0 _posts/{11-01-01-Caching.md => 12-01-01-Caching.md} | 0 _posts/{11-02-01-Bytecode-Cache.md => 12-02-01-Bytecode-Cache.md} | 0 _posts/{11-03-01-Object-Caching.md => 12-03-01-Object-Caching.md} | 0 _posts/{12-01-01-Resources.md => 13-01-01-Resources.md} | 0 _posts/{12-02-01-Frameworks.md => 13-02-01-Frameworks.md} | 0 _posts/{12-03-01-Components.md => 13-03-01-Components.md} | 0 _posts/{13-01-01-Community.md => 14-01-01-Community.md} | 0 26 files changed, 0 insertions(+), 0 deletions(-) rename _posts/{06-01-01-Databases.md => 08-01-01-Databases.md} (100%) rename _posts/{07-02-01-Errors.md => 08-02-01-Errors.md} (100%) rename _posts/{07-03-01-Exceptions.md => 08-03-01-Exceptions.md} (100%) rename _posts/{08-01-01-Security.md => 09-01-01-Security.md} (100%) rename _posts/{08-02-01-Web-Application-Security.md => 09-02-01-Web-Application-Security.md} (100%) rename _posts/{08-03-01-Password-Hashing.md => 09-03-01-Password-Hashing.md} (100%) rename _posts/{08-04-01-Data-Filtering.md => 09-04-01-Data-Filtering.md} (100%) rename _posts/{08-05-01-Configuration-Files.md => 09-05-01-Configuration-Files.md} (100%) rename _posts/{08-06-01-Register-Globals.md => 09-06-01-Register-Globals.md} (100%) rename _posts/{08-07-01-Error-Reporting.md => 09-07-01-Error-Reporting.md} (100%) rename _posts/{09-01-01-Testing.md => 10-01-01-Testing.md} (100%) rename _posts/{09-02-01-Test-Driven-Development.md => 10-02-01-Test-Driven-Development.md} (100%) rename _posts/{09-03-01-Behavior-Driven-Development.md => 10-03-01-Behavior-Driven-Development.md} (100%) rename _posts/{09-04-01-Complementary-Testing-Tools.md => 10-04-01-Complementary-Testing-Tools.md} (100%) rename _posts/{10-01-01-Servers-and-Deployment.md => 11-01-01-Servers-and-Deployment.md} (100%) rename _posts/{10-02-01-Platform-as-a-Service.md => 11-02-01-Platform-as-a-Service.md} (100%) rename _posts/{10-03-01-Virtual-or-Dedicated-Servers.md => 11-03-01-Virtual-or-Dedicated-Servers.md} (100%) rename _posts/{10-04-01-Shared-Servers.md => 11-04-01-Shared-Servers.md} (100%) rename _posts/{10-05-01-Building-your-Application.md => 11-05-01-Building-your-Application.md} (100%) rename _posts/{11-01-01-Caching.md => 12-01-01-Caching.md} (100%) rename _posts/{11-02-01-Bytecode-Cache.md => 12-02-01-Bytecode-Cache.md} (100%) rename _posts/{11-03-01-Object-Caching.md => 12-03-01-Object-Caching.md} (100%) rename _posts/{12-01-01-Resources.md => 13-01-01-Resources.md} (100%) rename _posts/{12-02-01-Frameworks.md => 13-02-01-Frameworks.md} (100%) rename _posts/{12-03-01-Components.md => 13-03-01-Components.md} (100%) rename _posts/{13-01-01-Community.md => 14-01-01-Community.md} (100%) diff --git a/_posts/06-01-01-Databases.md b/_posts/08-01-01-Databases.md similarity index 100% rename from _posts/06-01-01-Databases.md rename to _posts/08-01-01-Databases.md diff --git a/_posts/07-02-01-Errors.md b/_posts/08-02-01-Errors.md similarity index 100% rename from _posts/07-02-01-Errors.md rename to _posts/08-02-01-Errors.md diff --git a/_posts/07-03-01-Exceptions.md b/_posts/08-03-01-Exceptions.md similarity index 100% rename from _posts/07-03-01-Exceptions.md rename to _posts/08-03-01-Exceptions.md diff --git a/_posts/08-01-01-Security.md b/_posts/09-01-01-Security.md similarity index 100% rename from _posts/08-01-01-Security.md rename to _posts/09-01-01-Security.md diff --git a/_posts/08-02-01-Web-Application-Security.md b/_posts/09-02-01-Web-Application-Security.md similarity index 100% rename from _posts/08-02-01-Web-Application-Security.md rename to _posts/09-02-01-Web-Application-Security.md diff --git a/_posts/08-03-01-Password-Hashing.md b/_posts/09-03-01-Password-Hashing.md similarity index 100% rename from _posts/08-03-01-Password-Hashing.md rename to _posts/09-03-01-Password-Hashing.md diff --git a/_posts/08-04-01-Data-Filtering.md b/_posts/09-04-01-Data-Filtering.md similarity index 100% rename from _posts/08-04-01-Data-Filtering.md rename to _posts/09-04-01-Data-Filtering.md diff --git a/_posts/08-05-01-Configuration-Files.md b/_posts/09-05-01-Configuration-Files.md similarity index 100% rename from _posts/08-05-01-Configuration-Files.md rename to _posts/09-05-01-Configuration-Files.md diff --git a/_posts/08-06-01-Register-Globals.md b/_posts/09-06-01-Register-Globals.md similarity index 100% rename from _posts/08-06-01-Register-Globals.md rename to _posts/09-06-01-Register-Globals.md diff --git a/_posts/08-07-01-Error-Reporting.md b/_posts/09-07-01-Error-Reporting.md similarity index 100% rename from _posts/08-07-01-Error-Reporting.md rename to _posts/09-07-01-Error-Reporting.md diff --git a/_posts/09-01-01-Testing.md b/_posts/10-01-01-Testing.md similarity index 100% rename from _posts/09-01-01-Testing.md rename to _posts/10-01-01-Testing.md diff --git a/_posts/09-02-01-Test-Driven-Development.md b/_posts/10-02-01-Test-Driven-Development.md similarity index 100% rename from _posts/09-02-01-Test-Driven-Development.md rename to _posts/10-02-01-Test-Driven-Development.md diff --git a/_posts/09-03-01-Behavior-Driven-Development.md b/_posts/10-03-01-Behavior-Driven-Development.md similarity index 100% rename from _posts/09-03-01-Behavior-Driven-Development.md rename to _posts/10-03-01-Behavior-Driven-Development.md diff --git a/_posts/09-04-01-Complementary-Testing-Tools.md b/_posts/10-04-01-Complementary-Testing-Tools.md similarity index 100% rename from _posts/09-04-01-Complementary-Testing-Tools.md rename to _posts/10-04-01-Complementary-Testing-Tools.md diff --git a/_posts/10-01-01-Servers-and-Deployment.md b/_posts/11-01-01-Servers-and-Deployment.md similarity index 100% rename from _posts/10-01-01-Servers-and-Deployment.md rename to _posts/11-01-01-Servers-and-Deployment.md diff --git a/_posts/10-02-01-Platform-as-a-Service.md b/_posts/11-02-01-Platform-as-a-Service.md similarity index 100% rename from _posts/10-02-01-Platform-as-a-Service.md rename to _posts/11-02-01-Platform-as-a-Service.md diff --git a/_posts/10-03-01-Virtual-or-Dedicated-Servers.md b/_posts/11-03-01-Virtual-or-Dedicated-Servers.md similarity index 100% rename from _posts/10-03-01-Virtual-or-Dedicated-Servers.md rename to _posts/11-03-01-Virtual-or-Dedicated-Servers.md diff --git a/_posts/10-04-01-Shared-Servers.md b/_posts/11-04-01-Shared-Servers.md similarity index 100% rename from _posts/10-04-01-Shared-Servers.md rename to _posts/11-04-01-Shared-Servers.md diff --git a/_posts/10-05-01-Building-your-Application.md b/_posts/11-05-01-Building-your-Application.md similarity index 100% rename from _posts/10-05-01-Building-your-Application.md rename to _posts/11-05-01-Building-your-Application.md diff --git a/_posts/11-01-01-Caching.md b/_posts/12-01-01-Caching.md similarity index 100% rename from _posts/11-01-01-Caching.md rename to _posts/12-01-01-Caching.md diff --git a/_posts/11-02-01-Bytecode-Cache.md b/_posts/12-02-01-Bytecode-Cache.md similarity index 100% rename from _posts/11-02-01-Bytecode-Cache.md rename to _posts/12-02-01-Bytecode-Cache.md diff --git a/_posts/11-03-01-Object-Caching.md b/_posts/12-03-01-Object-Caching.md similarity index 100% rename from _posts/11-03-01-Object-Caching.md rename to _posts/12-03-01-Object-Caching.md diff --git a/_posts/12-01-01-Resources.md b/_posts/13-01-01-Resources.md similarity index 100% rename from _posts/12-01-01-Resources.md rename to _posts/13-01-01-Resources.md diff --git a/_posts/12-02-01-Frameworks.md b/_posts/13-02-01-Frameworks.md similarity index 100% rename from _posts/12-02-01-Frameworks.md rename to _posts/13-02-01-Frameworks.md diff --git a/_posts/12-03-01-Components.md b/_posts/13-03-01-Components.md similarity index 100% rename from _posts/12-03-01-Components.md rename to _posts/13-03-01-Components.md diff --git a/_posts/13-01-01-Community.md b/_posts/14-01-01-Community.md similarity index 100% rename from _posts/13-01-01-Community.md rename to _posts/14-01-01-Community.md From 79334026462b892c8a984708ddb4bc9e36d471c2 Mon Sep 17 00:00:00 2001 From: happyaccidents Date: Tue, 26 Nov 2013 14:37:09 +0000 Subject: [PATCH 2/5] Missed a rename --- _posts/{08-01-01-Databases.md => 07-01-01-Databases.md} | 0 ...Errors-and-Exceptions.md => 08-01-01-Errors-and-Exceptions.md} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename _posts/{08-01-01-Databases.md => 07-01-01-Databases.md} (100%) rename _posts/{07-01-01-Errors-and-Exceptions.md => 08-01-01-Errors-and-Exceptions.md} (100%) diff --git a/_posts/08-01-01-Databases.md b/_posts/07-01-01-Databases.md similarity index 100% rename from _posts/08-01-01-Databases.md rename to _posts/07-01-01-Databases.md diff --git a/_posts/07-01-01-Errors-and-Exceptions.md b/_posts/08-01-01-Errors-and-Exceptions.md similarity index 100% rename from _posts/07-01-01-Errors-and-Exceptions.md rename to _posts/08-01-01-Errors-and-Exceptions.md From 192a207e2db94a2050b025e168b4a729fef4a517 Mon Sep 17 00:00:00 2001 From: happyaccidents Date: Thu, 28 Nov 2013 12:17:44 +0000 Subject: [PATCH 3/5] First draft - Resolves #280 --- _posts/06-01-01-Dependency-Injection.md | 8 ++++ _posts/06-02-01-Basic-Concept.md | 57 +++++++++++++++++++++++++ _posts/06-03-01-Complex-Problem.md | 49 +++++++++++++++++++++ _posts/06-04-01-Containers.md | 14 ++++++ _posts/06-05-01-Further-Reading.md | 11 +++++ 5 files changed, 139 insertions(+) create mode 100644 _posts/06-01-01-Dependency-Injection.md create mode 100644 _posts/06-02-01-Basic-Concept.md create mode 100644 _posts/06-03-01-Complex-Problem.md create mode 100644 _posts/06-04-01-Containers.md create mode 100644 _posts/06-05-01-Further-Reading.md diff --git a/_posts/06-01-01-Dependency-Injection.md b/_posts/06-01-01-Dependency-Injection.md new file mode 100644 index 0000000..3bac06e --- /dev/null +++ b/_posts/06-01-01-Dependency-Injection.md @@ -0,0 +1,8 @@ +--- +title: Dependency Injection +--- + +# Dependency Injection {#dependency_injection_title} + +Dependency Injection is a basic concept that solves a complex problem. As a software design pattern it is being adopted in +some way or another by all major PHP projects. diff --git a/_posts/06-02-01-Basic-Concept.md b/_posts/06-02-01-Basic-Concept.md new file mode 100644 index 0000000..1748afe --- /dev/null +++ b/_posts/06-02-01-Basic-Concept.md @@ -0,0 +1,57 @@ +--- +isChild: true +--- + +## Basic Concept {#basic_concept_title} + +From [Wikipedia](http://en.wikipedia.org/wiki/Dependency_injection): + +> Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it +> possible to change them, whether at run-time or compile-time. + +This quote makes the concept sound much more complicated than it actually is. Dependency Injection is providing a component +with it's dependencies either through constructor injection, method calls or the setting of properties. It is that simple. + +We can demonstrate the concept with a simple, yet naive, example. + +{% highlight php %} +adapter = new MySqlAdapter; + } +} + +class MysqlAdapter {} +{% endhighlight %} + +Here we have a `Database` class that requires an adapter to speak to the database. We instantiate the +adapter in the constructor and create a hard dependency. This code can be refactored to use Dependency Injection +and therefore loosen the dependency. + +{% highlight php %} +adapter = $adapter; + } +} + +class MysqlAdapter {} +{% endhighlight %} + +Now we are giving the `Database` class it's dependency rather than it creating it itself. We could even create a method +that would accept an argument of the dependency and set it that way, or if the `$adapter` property was `public` we could +set it directly. diff --git a/_posts/06-03-01-Complex-Problem.md b/_posts/06-03-01-Complex-Problem.md new file mode 100644 index 0000000..07050d2 --- /dev/null +++ b/_posts/06-03-01-Complex-Problem.md @@ -0,0 +1,49 @@ +--- +isChild: true +--- + +## Complex Problem {#complex_problem_title} + +If you have ever read about Dependency Injection then you have probably seen the terms *"Inversion of Control"* or *"Dependency Inversion Principle"*. +These are the complex problems that Dependency Injection solves, or to be more precise, elegantly solves. + +For years, PHP frameworks have been achieving Inversion of Control, however, the question became, which part of control +are you inverting, and where to? For example, MVC frameworks would generally provide a super object or base controller that other +controllers must extend to gain access to it's dependencies. This **is** Inversion of Control, however, instead of loosening +dependencies, this method simply moved them. + +Dependency Injection allows us to more elegantly solve this problem by only injecting the dependencies we need, when we need them, +without the need for any hard coded dependencies at all. + +Dependency Inversion Principle is the "D" in the S.O.L.I.D set of object oriented design principles that states one should +*"Depend on Abstractions. Do not depend on concretions."*. Put simply, this means our dependencies should be interfaces/contracts or abstract +classes rather than concrete implementations. We can easily refactor the above example to follow this principle. + +{% highlight php %} +adapter = $adapter; + } +} + +interface AdapterInterface {} + +class MysqlAdapter implements AdapterInterface {} +{% endhighlight %} + +There are several benefits to the Database class now depending on an interface rather than a concretion. + +Consider that you are working in a team and the adapter is being worked on by a colleague. In our first example, we would have +to wait for said colleague to finish the adapter before we could properly mock it for our unit tests. Now that the dependency +is an interface/contract we can happily mock that interface knowing that our colleague will build the adapter based on that contract. + +An even bigger benefit to this method is that our code is now much more scalable. If a year down the line we decide that we +want to migrate to a different type of database, we can write an adapter that implements the original interface and inject that instead, +no more refactoring would be required as we can ensure that the adapter follows the contract set by the interface. diff --git a/_posts/06-04-01-Containers.md b/_posts/06-04-01-Containers.md new file mode 100644 index 0000000..b3eca74 --- /dev/null +++ b/_posts/06-04-01-Containers.md @@ -0,0 +1,14 @@ +--- +isChild: true +--- + +## Containers {#containers_title} + +The first thing you should understand about Dependency Injection Containers is that they are not the same thing as Dependency +Injection. A container is a convenience utility that helps us implement Dependency Injection, however, they can be and often +are misused to implement an anti pattern, Service Location. Using a container as a Service Locator within your classes arguably +creates a harder dependency on the container than the dependency you are replacing. It also makes your code much less transparent +and ultimately harder to test. + +Most modern frameworks have their own Dependency Injection Container that allows you to wire your dependencies together through configuration. +What this means in practice is that you can write application code that is as clean and de-coupled as the framework it is built on. diff --git a/_posts/06-05-01-Further-Reading.md b/_posts/06-05-01-Further-Reading.md new file mode 100644 index 0000000..e1d3645 --- /dev/null +++ b/_posts/06-05-01-Further-Reading.md @@ -0,0 +1,11 @@ +--- +isChild: true +--- + +## Further Reading {#further_reading_title} + +- [Learning about Dependency Injection and PHP](http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php) +- [What is Dependency Injection?](http://fabien.potencier.org/article/11/what-is-dependency-injection) +- [Dependency Injection: An analogy](http://mwop.net/blog/260-Dependency-Injection-An-analogy.html) +- [Dependency Injection: Huh?](http://net.tutsplus.com/tutorials/php/dependency-injection-huh/) +- [Dependency Injection as a tool for testing](http://www.happyaccidents.me/dependency-injection-as-a-tool-for-testing/) From ea4a6bebe236b7437cd0afba54233204cb3ef83e Mon Sep 17 00:00:00 2001 From: happyaccidents Date: Thu, 28 Nov 2013 14:00:56 +0000 Subject: [PATCH 4/5] Revisions --- _posts/06-01-01-Dependency-Injection.md | 9 +++++++-- _posts/06-02-01-Basic-Concept.md | 16 +++++----------- _posts/06-03-01-Complex-Problem.md | 9 ++++++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/_posts/06-01-01-Dependency-Injection.md b/_posts/06-01-01-Dependency-Injection.md index 3bac06e..e69c4a2 100644 --- a/_posts/06-01-01-Dependency-Injection.md +++ b/_posts/06-01-01-Dependency-Injection.md @@ -4,5 +4,10 @@ title: Dependency Injection # Dependency Injection {#dependency_injection_title} -Dependency Injection is a basic concept that solves a complex problem. As a software design pattern it is being adopted in -some way or another by all major PHP projects. +From [Wikipedia](http://en.wikipedia.org/wiki/Dependency_injection): + +> Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it +> possible to change them, whether at run-time or compile-time. + +This quote makes the concept sound much more complicated than it actually is. Dependency Injection is providing a component +with it's dependencies either through constructor injection, method calls or the setting of properties. It is that simple. diff --git a/_posts/06-02-01-Basic-Concept.md b/_posts/06-02-01-Basic-Concept.md index 1748afe..520415b 100644 --- a/_posts/06-02-01-Basic-Concept.md +++ b/_posts/06-02-01-Basic-Concept.md @@ -4,15 +4,11 @@ isChild: true ## Basic Concept {#basic_concept_title} -From [Wikipedia](http://en.wikipedia.org/wiki/Dependency_injection): +We can demonstrate the concept with a simple, yet naive. -> Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it -> possible to change them, whether at run-time or compile-time. - -This quote makes the concept sound much more complicated than it actually is. Dependency Injection is providing a component -with it's dependencies either through constructor injection, method calls or the setting of properties. It is that simple. - -We can demonstrate the concept with a simple, yet naive, example. +Here we have a `Database` class that requires an adapter to speak to the database. We instantiate the +adapter in the constructor and create a hard dependency. This makes testing difficult and means the `Database` class is +very tightly coupled to the adapter. {% highlight php %} Date: Fri, 29 Nov 2013 09:46:15 +0000 Subject: [PATCH 5/5] Typos and further change re comments --- _posts/06-03-01-Complex-Problem.md | 26 +++++++++++++------------- _posts/06-04-01-Containers.md | 10 +++++----- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/_posts/06-03-01-Complex-Problem.md b/_posts/06-03-01-Complex-Problem.md index 69e69fb..9fd0f21 100644 --- a/_posts/06-03-01-Complex-Problem.md +++ b/_posts/06-03-01-Complex-Problem.md @@ -4,26 +4,26 @@ isChild: true ## Complex Problem {#complex_problem_title} -If you have ever read about Dependency Injection then you have probably seen the terms *"Inversion of Control"* or *"Dependency Inversion Principle"*. +If you have ever read about Dependency Injection then you have probably seen the terms *"Inversion of Control"* or *"Dependency Inversion Principle"*. These are the complex problems that Dependency Injection solves. ### Inversion of Control -Inversion of Control is as it says, "inverting the control" of a system by keeping organisational control entirely separate from our objects. +Inversion of Control is as it says, "inverting the control" of a system by keeping organisational control entirely separate from our objects. In terms of Dependency Injection, this means loosening our dependencies by controlling and instantiating them elsewhere in the system. -For years, PHP frameworks have been achieving Inversion of Control, however, the question became, which part of control -are you inverting, and where to? For example, MVC frameworks would generally provide a super object or base controller that other -controllers must extend to gain access to it's dependencies. This **is** Inversion of Control, however, instead of loosening +For years, PHP frameworks have been achieving Inversion of Control, however, the question became, which part of control +are you inverting, and where to? For example, MVC frameworks would generally provide a super object or base controller that other +controllers must extend to gain access to its dependencies. This **is** Inversion of Control, however, instead of loosening dependencies, this method simply moved them. -Dependency Injection allows us to more elegantly solve this problem by only injecting the dependencies we need, when we need them, +Dependency Injection allows us to more elegantly solve this problem by only injecting the dependencies we need, when we need them, without the need for any hard coded dependencies at all. ### Dependency Inversion Principle -Dependency Inversion Principle is the "D" in the S.O.L.I.D set of object oriented design principles that states one should -*"Depend on Abstractions. Do not depend on concretions."*. Put simply, this means our dependencies should be interfaces/contracts or abstract +Dependency Inversion Principle is the "D" in the S.O.L.I.D set of object oriented design principles that states one should +*"Depend on Abstractions. Do not depend on concretions."*. Put simply, this means our dependencies should be interfaces/contracts or abstract classes rather than concrete implementations. We can easily refactor the above example to follow this principle. {% highlight php %} @@ -45,12 +45,12 @@ interface AdapterInterface {} class MysqlAdapter implements AdapterInterface {} {% endhighlight %} -There are several benefits to the Database class now depending on an interface rather than a concretion. +There are several benefits to the `Database` class now depending on an interface rather than a concretion. -Consider that you are working in a team and the adapter is being worked on by a colleague. In our first example, we would have -to wait for said colleague to finish the adapter before we could properly mock it for our unit tests. Now that the dependency +Consider that you are working in a team and the adapter is being worked on by a colleague. In our first example, we would have +to wait for said colleague to finish the adapter before we could properly mock it for our unit tests. Now that the dependency is an interface/contract we can happily mock that interface knowing that our colleague will build the adapter based on that contract. -An even bigger benefit to this method is that our code is now much more scalable. If a year down the line we decide that we -want to migrate to a different type of database, we can write an adapter that implements the original interface and inject that instead, +An even bigger benefit to this method is that our code is now much more scalable. If a year down the line we decide that we +want to migrate to a different type of database, we can write an adapter that implements the original interface and inject that instead, no more refactoring would be required as we can ensure that the adapter follows the contract set by the interface. diff --git a/_posts/06-04-01-Containers.md b/_posts/06-04-01-Containers.md index b3eca74..f0e5466 100644 --- a/_posts/06-04-01-Containers.md +++ b/_posts/06-04-01-Containers.md @@ -4,11 +4,11 @@ isChild: true ## Containers {#containers_title} -The first thing you should understand about Dependency Injection Containers is that they are not the same thing as Dependency -Injection. A container is a convenience utility that helps us implement Dependency Injection, however, they can be and often -are misused to implement an anti pattern, Service Location. Using a container as a Service Locator within your classes arguably -creates a harder dependency on the container than the dependency you are replacing. It also makes your code much less transparent +The first thing you should understand about Dependency Injection Containers is that they are not the same thing as Dependency +Injection. A container is a convenience utility that helps us implement Dependency Injection, however, they can be and often +are misused to implement an anti-pattern, Service Location. Injecting a DI container as a Service Locator in to your classes arguably +creates a harder dependency on the container than the dependency you are replacing. It also makes your code much less transparent and ultimately harder to test. -Most modern frameworks have their own Dependency Injection Container that allows you to wire your dependencies together through configuration. +Most modern frameworks have their own Dependency Injection Container that allows you to wire your dependencies together through configuration. What this means in practice is that you can write application code that is as clean and de-coupled as the framework it is built on.