Download - Dependency injection and inversion
Dependency InjectionWhat, Why and How
Ravish Chhabra
Design Requirements 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
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.
Tight Coupling
CustomerRepository class directly depends upon Database class.
Loose Coupling
CustomerRepository class depends upon IDatabase interface
Dependency Injection 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.
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.
Tightly Coupled System
Forcefully you have to write to Event Logs only.
IOC Mechanism implementedBy adding an interface( INotify )
High level module LogMonitorusing abstraction( INotify )
Still, Concrete class is initializedTo achieve further level of Decoupling, DI is used.
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..
Constructor Injection
Object of concrete class (INotify Type) will be passed into Constructor parameter
Object of concrete class passed to Constructor of LogMonitor
Method Injection
Object of concrete class (INotify Type) will be passed into Method parameter
Object of concrete class passed as Method parameter
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.
StructureMap UnityContainer Autofac Ninject Castle Windsor Spring .Net
Popular DI Containers For .NET
DI Containers Example-Unity Container
Dependency configured using Containerand is resolved at run time
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.
`
Thank You