23
Dependency Inversion Principle Depend on abstractions, not on concretions. Shahriar Hyder Kaz Software Ltd. 29 th September, 2011

Dependency Inversion Principle

Embed Size (px)

DESCRIPTION

Dependency Inversion - Design Principle

Citation preview

Page 1: Dependency Inversion Principle

Dependency Inversion Principle

Depend on abstractions, not on concretions.

Shahriar HyderKaz Software Ltd.29th September, 2011

Page 2: Dependency Inversion Principle

Dependency Inversion Principle

In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling where conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are inverted (i.e. reversed) for the purpose of rendering high-level modules independent of the low-level module implementation details. The principle states:

Page 3: Dependency Inversion Principle

Dependency Inversion Principle

High level modules should not depend on low level modules. Both should depend on abstractions.

Abstractions should not depend on details. Details should depend on abstractions.

Page 4: Dependency Inversion Principle

Depend on abstractions, not on concretions

Page 5: Dependency Inversion Principle

Inversion of control

In software engineering, Inversion of Control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.

Page 6: Dependency Inversion Principle

Inversion of control

Also known as the Hollywood Principle Don’t call us, we’ll call you!

Objects rely on their environment to provide dependencies rather than actively obtaining them.

Inversion of Control can make the difference between a library and a framework.

Page 7: Dependency Inversion Principle

Dependency Injection

Dependency injection (DI) is a design pattern in object-oriented computer programming whose purpose is to reduce the coupling between software components. It is similar to the factory method pattern. Frequently an object uses (depends on) work produced by another part of the system. With DI, the object does not need to know in advance about how the other part of the system works. Instead, the programmer provides (injects) the relevant system component in advance along with a contract that it will behave in a certain way.

Page 8: Dependency Inversion Principle

Dependency Injection

Dependency injection is = IoC + Dependency Inversion: Dependencies are provided externally so we

enforce the dependency inversion principle The container sets the dependencies (not us)

so we speak of inversion of control Aspect-oriented programming is one

technique to implement IoC. Dependency Injection - one example of

IoC design principle.

Page 9: Dependency Inversion Principle

The Service Locator Pattern

The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task.

Page 10: Dependency Inversion Principle

The Service Locator Pattern

For some reasons many people think that there is no relationship between Service Locator and DI. The Service Locator is yet another implementation of the Dependency Inversion Principle. And yes Service Locator is competitor for the Dependency Injection Pattern! But they still can co-exist.

Page 11: Dependency Inversion Principle

Plugin

 Use your brains!

Page 12: Dependency Inversion Principle

DIP Violations

Use of “new”

Page 13: Dependency Inversion Principle

DIP Violations

Use of “static”

public class SecurityService{ public static User GetCurrentUser() { // do something }}

Page 14: Dependency Inversion Principle

DIP Violations

Use of singletons using “static”

public class ProductCache{ private static readonly _instance = new ProductCache(); public static ProductCache Instance { get { return _instance; } }}

Page 15: Dependency Inversion Principle

What is a DI (IoC) Container? Creates objects that are ready for you to use Knows how to create objects and their

dependencies Knows how to initialize objects when they are

created (if necessary) Provide lifecycle support A container is nothing more than a fancy

dictionary of interfaces and their implementing types

Inversion Of Control containers act like “super factories”

Page 16: Dependency Inversion Principle

DI Containers

Spring Framework Unity (Microsoft) Windsor (Castle Project) Ninject StructureMap

Page 17: Dependency Inversion Principle

Types of Dependency Injection

Constructor (Most popular) Setter Method

Page 18: Dependency Inversion Principle

Type 1 or interface injection / Method Injection

Injecting a ICustomerRepository as well as an integer dependency.

Page 19: Dependency Inversion Principle

Type 2 or Setter Injection

Injecting a ICustomerRepository through the setter.

Page 20: Dependency Inversion Principle

Type 3 or Constructor Injection

Injecting a ICustomerRepository and a ICustomerDTOMapper through the constructor.

Note: This is the most popular type of injection.

Page 21: Dependency Inversion Principle

Dependency Injection Pros & Cons

Pros Loosely Coupled Increases Testability (A LOT!) Separates components cleanly Allows for use of Inversion of Control Container

Cons Increases code complexity Some Jr. Developers find it difficult to understand at First Can Complicate Debugging at First Complicates following Code Flow Architecture is less straight-forward Overkill for smaller or throwaway applications Easy to abuse Be careful to avoid “XML Hell” in configuration files

Page 22: Dependency Inversion Principle

*ILITIES

Agility Testability Reversibility Changeability Flexibility Maintainability

Page 23: Dependency Inversion Principle

Resource

Probably the best place to read is Martin Fowler - Inversion of Control Containers and the Dependency Injection pattern