concept loose coupling in category dependency injection

appears as: loose coupling
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

In the previous section, we described the motivational forces that makes one think about DI in the first place. If you’re convinced that loose coupling is a benefit, you may want to make everything loosely coupled. Overall, that’s a good idea. When you need to decide how to package modules, loose coupling proves especially useful. But you don’t have to abstract everything away and make it pluggable. In this section, we’ll provide some decision tools to help you decide how to model your Dependencies.

Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

In the previous section, I described the motivational forces that make us think about DI in the first place. If you’re convinced that loose coupling is a good idea, you may want to make everything loosely coupled. Overall, this is a good idea. When you must decide how to package modules, loose coupling provides especially useful guidance. You don’t have to abstract everything away and make everything pluggable. In this section, I’ll provide you with some decision tools that can help you decide how to model your DEPENDENCIES.

I don’t blame you if you find the previous DI example to be over-engineered, but consider this: by its nature, the classic Hello World example is a simple problem with well-specified and constrained requirements. In the real world, software development is never like this. Requirements change and are often fuzzy. The features you must implement also tend to be much more complex. DI helps address such issues by enabling loose coupling. Specifically, we gain the benefits listed in table 1.1.

Table 1.1. Benefits gained from loose coupling. Each benefit is always available but will be valued differently depending on circumstances.

Benefit

Description

When is it valuable?

Late binding Services can be swapped with other services. Valuable in standard software, but perhaps less so in enterprise applications where the runtime environment tends to be well-defined
Extensibility Code can be extended and reused in ways not explicitly planned for. Always valuable
Parallel development Code can be developed in parallel. Valuable in large, complex applications; not so much in small, simple applications
Maintainability Classes with clearly defined responsibilities are easier to maintain. Always valuable
TESTABILITY Classes can be unit tested. Only valuable if you unit test (which you really, really should)

In table 1.1, I listed the late binding benefit first because, in my experience, this is the one that’s foremost in most people’s minds. When architects and developers fail to understand the benefits of loose coupling, this is most likely because they never consider the other benefits.

Listing 6.4. Enforcing loose coupling with a unit test
[Fact] public void SutShouldNotReferenceSqlDataAccess() { // Fixture setup Type sutRepresentative = typeof(HomeController); var unwanted = "Ploeh.Samples.Commerce.Data.Sql"; // Exercise system var references = sutRepresentative.Assembly .GetReferencedAssemblies(); // Verify outcome Assert.False( references.Any(a => a.Name == unwanted), string.Format( "{0} should not be referenced by SUT", unwanted)); // Teardown }
Listing 6.5. Enforcing loose coupling with an integration test
Dependency Injection: Design Patterns Using Spring and Guice

This is an excerpt from Manning's book Dependency Injection: Design Patterns Using Spring and Guice.

A growing emphasis on unit testing continues to be a natural catalyst to the growth of DI popularity. This made for agile programs, which are easily broken down into discrete, modular units that are simple to test and swap out with alternative behaviors. Consequently, loose coupling is a core driver in the evolution of DI.

Loose coupling via interfaces leads nicely to the idea of programming to contract. Recall that in chapter 1 we described a service as in part defined by a well-understood set of responsibilities called a contract. Programming to contract means that neither client nor service is aware of the other and communicates only via the contract. The only common ground is the understanding provided by this contract (interface). This means that either can evolve independently so long as both abide by the terms of the contract. A contract is also much more than a means of communication: