Information about the Coupling, it's type and how to achieve Loose Coupling, Dependency injection principles (DI, IOC) and different Dependency Inversion Containers and DI Pros and Cons
2. DesignRequirements
Maintainability :- It is the quality of a software system that determines how
easily and how efficiently you can update it.
Testability :- It enables you to effectively test individual parts of the system. In
short, how easy is to test the system. Test driven development is use to write Unit
test before writing any code to implement to improve the quality of applications.
Flexibility and Extensibility :- How easily the system can adapt to work in
different ways is flexibility and add new features is extensibility
3. Definition and Types
Coupling :- It states the dependency of the system. How dependent are sub
systems in the system.
Tight Coupling
Loose Coupling
Tight Coupling :- Strong or direct dependency between components.
Loose Coupling :- Loose or indirect dependency between components.
6. DependencyInjection
WHAT
A Software Design Pattern that follows Dependency inversion principle and
implement Inversion of Control for the resolution of dependencies at run-
time or compile time.
WHY
The “D” is Dependency Injection (in SOLID principles of design) used to
achieve Loose Coupling in the system.
7. Principles
Dependency Inversion Principle:- A software design principle that provide
us guidelines to loosely couple the system.
1. High level modules should not depend on low level modules or
subsystems. Both should depend on Abstractions.
2. Abstraction should not depend on details but vice versa.
Inversion Of Control (IOC) :- It is the mechanism that is used to implement
the Dependency Inversion principle.
Abstraction is introduced in the system to make the high level modules to
depend on abstraction rather than the sub systems themselves.
9. IOC Mechanism implemented
By adding an interface( INotify )
High level module LogMonitor
using abstraction( INotify )
Still, Concrete class is initialized
To achieve further level of
Decoupling, DI is used.
10. Dependency Injection injects the concrete implementation into a class ( by moving
it out of dependent class) that is using abstraction.
Major ways of implementing DI
Constructor Injection - pass the object of concrete class into the constructor of
dependent class.
Method Injection - pass the object of the concrete class into the method the
dependent class which is actually invoking the action.
Property Injection - pass the object of the concrete class via a setter property
that was exposed by the dependent class.
Dependency Injection Continued..
11. Constructor Injection
Object of concrete class
(INotify Type) will be passed into
Constructor parameter
Object of concrete class passed to
Constructor of LogMonitor
12. Method Injection
Object of concrete class
(INotify Type) will be passed
into Method parameter
Object of concrete class passed as
Method parameter
13. Other Key Concepts Of DI
Dependency injection injects the dependencies of a class at runtime.
DI Containers: To automatically inject dependencies we use a Dependency Injection(DI)
container. We can also inject the dependencies manually but using a DI container provides
the following benefits
Automatic Dependency Resolution When dependendencies are managed by the
container there are less chances of error. Suppose if our application has a lot of
dependencies then injecting those dependencies is also difficult to manage if we are
injecting them without a DI container
Decouples the client from the dependency – It saves the effort and work spent by client
to inject the dependency.
16. Pros and Cons
PROS
Reduced Dependencies.
Increased Testability and Maintainability – Inject mock implementations for testing.
More Reusable and Extensible Code – External pluggable and configurable components.
Run Time configuration for Dependency Injection
CONS
Increased code complexity.
Can complicate debugging while development.
`