Dependency Inversion, Dependency Injection and Inversion of Control. Dependency Inversion Principle by Robert Martin of Agile Fame

Similar documents
SOLID: Principles of OOD

Object-Oriented Design I

Object-Oriented Design I - SOLID

SOLID DESIGN PATTERNS FOR MERE MORTALS

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

CSC207H: Software Design SOLID. CSC207 Winter 2018

Object-Oriented Design

Advanced WCF 4.0 .NET. Web Services. Contents for.net Professionals. Learn new and stay updated. Design Patterns, OOPS Principles, WCF, WPF, MVC &LINQ

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Lessons Learned. Johnny Bigert, Ph.D., Skype/Microsoft October 26, 2011

Software Development Project. Kazi Masudul Alam

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Build Testable Client and Service Applications

Agile Architecture. The Why, the What and the How

Tecniche di Progettazione: Design Patterns

Open Closed Principle (OCP)

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Single Responsibility Principle (SRP)

An Introduction to Patterns

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Patterns Continued and Concluded. July 26, 2017

Introduction to Testing and Maintainable code

Program to an Interface (a.k.a. - P2I) Not an Implementation

Dependency Injection with ObjectPoolManager

Guice. Java DI Framework

Dependency Injection & Design Principles Recap Reid Holmes

Program to an Interface, Not an Implementation

Basic design patterns

Software Engineering I (02161)

Software Engineering

DESIGN PATTERNS FOR MERE MORTALS

Resolving Cyclic Dependencies Design for Testability

Philosophy of Unit Testing

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

Embrace Factories Factories. By Rob Gonda

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Design patterns. Jef De Smedt Beta VZW

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Dependency Injection (ESaaS 11.6)! 2013 Armando Fox & David Patterson, all rights reserved

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Dependency Injection & Design Principles Recap Reid Holmes

Services in Joomla 4. Allon Moritz J and Beyond 13. May 2018

Chris Donnan & Solomon Duskis

Princípy tvorby softvéru Dizajnové princípy

Index. BigBadGuiTools (BBGT), 186 Business logic and data layer, 32 Business requirements

Design patterns using Spring and Guice

Application Architectures, Design Patterns

1 Software Architecture

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Design Patterns. Dependency Injection. Oliver Haase

An Introduction to Patterns

Essential Skills for the Agile Developer. Agile. copyright Net Objectives, Inc.

International Association of Scientific Innovation and Research (IASIR) (An Association Unifying the Sciences, Engineering, and Applied Research)

Improve Your SystemVerilog OOP Skills

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

Design Patterns. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

CS 520/620 Advanced Software Engineering Fall September 27, 2016

Object Oriented Software Design - I

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

DI Why? Getting a Grip on Dependency Injection. Jeremy Clark

Spring Interview Questions

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Patterns for Injection of Mock Objects in a Modeling Environment

CS 351 Design of Large Programs Singleton Pattern

Clean Code Why Clean Code matters

COURSE 2 DESIGN PATTERNS

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Enterprise Java Development using JPA, Hibernate and Spring. Srini Penchikala Detroit JUG Developer Day Conference November 14, 2009

Design Pattern- Creational pattern 2015

A Primer on Design Patterns

Dr. Xiaolin Hu. Review of last class

Agile Principles, Patterns, and Practices in C#

The Principled Developer. Gerardo

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

OO Class Design Principles

SPRING MOCK TEST SPRING MOCK TEST I

Index. Dmitri Nesteruk 2018 D. Nesteruk, Design Patterns in Modern C++,

MEPubReader: J2ME epub Book Reader CSCI 188/ 297. Rizwan Habib. December 13, 2010

Dependency Injection with Guice

Agile Software Development

Fast Track to Spring 3 and Spring MVC / Web Flow

Design Patterns Reid Holmes

Java Technologies. Lecture III. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

Unit 1 3 Dependency Injection & Inversion of Control

The de constructed. Magento module

UML and Design Patterns Prof. Dr. Eric Dubuis, V. June Engineering and Information Technology. On Package Design

Croquet. William R. Speirs, Ph.D. Founder & CEO of Metrink

Architecting ios Project. Massimo Oliviero

Simple Injector Documentation

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 13: Design Patterns

Comparing Spring & Guice

SDC Design patterns GoF

Single Responsibility Principle

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Transcription:

Dependency Inversion, Dependency Injection and Inversion of Control Dependency Inversion Principle by Robert Martin of Agile Fame

Dependency Inversion Principle History Postulated by Robert C. Martin Described the paper Object Oriented Design Quality Metrics: an analysis of dependencies, an article in the C++ Report in May 1996 entitled The Dependency Inversion Principle In group that began Agile Principles which came out of Extreme Programming Founder Of SOLID: Single responsibility principle, Open/closed principle, Liskov-substitution principle, Interface segregation principle, Dependency inversion principle

IoC Vocabulary

Why Dependency Inversion Needed

Dependency Not Inverted from Previous Slide Example: Low Level Classes Defining Interface

Solution: Dependency Inversion Where High Level Class Defines the Interface for Low Level Classes

From Bob Martin s 1996 Paper on DIP 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 Simple problem presented in paper is a copy program that reads from a keyboard and writes to a printer Become problem when must also write to disk so Copy program now has switch between two interfaces in printer and disk

DI needed to solve problem where high level Copy must switch between 2 different low level interfaces

DIP solves Copy problem with interfaces for Reader and Writer that low level classes must implement

Inversion of Control (IoC) IoC in an implementation of Dependency Inversion Principle Some types of IoC (many definitions out there, however) Interface Inversion: control of interface between 2 components or systems Flow Inversion: control of the flow between an application s layers Creation Inversion: control of dependency creation and binding of classes

How does IoC relate to DIP? Low level module or layer should not control high level module or layer IoC is a way to implement Dependency Inversion Principle

DIP and IoC General Implementation Types

Interface Inversion (the Provider Model)

Flow Inversion

Flow Inversion: App not in control, flow now inverted so that user is in control of application

Creation Inversion Normally objects are created in the class that s using it. MyClass myobject = new MyClass(); If the creation is done in a high level class to instantiate the lower level class, they are still dependent upon each other even if MyClass is an interface In Creation Inversion, create the object outside the class they are used in Creation Inversion uses the GoF Factory Pattern to eliminate switch statements that usually necessitate cutting-and-pasting of same code everywhere

Factory Pattern for Creation Inversion Define interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Moves creation of an low-level object to a factory class outside the highlevel class: MyClass myobject = MyFactory.CreateNewObject(); Not the only way for Creation Inversion since any time moving object outside the class using it, it s creation inversion

Many Creation Inversion Types

Summary So Far

Dependency Injection (DI) Overview What is Dependency Injection? Constructor Injection (Most Common) Setter Injection (Less Common) Interface Injection (Least Common) DI Caution

What is Dependency Injection? Dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. DI is moving creation and binding of dependency outside of class that depends on it Simple example: packing lunch versus having lunch provided by Service.

Decoupling Dependency doesn t remove Dependency since MyClass creates interface

Class Outside MyClass acts as Injector to remove Dependency (Dependency Injection)

Constructor Injection via Outside Class Simple: Pass dependency to dependent class via constructor

Setter Injection: Create property setter in dependent class as injector method but order matters

Interface Injection: Dependent class implements interface and injector uses interface to set dependency, not common

IoC Container

IoC Container Visualization

IoC Container (From Previous Example)

ASP.NET 5 IoC or DI Container The core features DI container abstracted to the IServiceProvider interface The DI container supports just four modes of operation: Instance: A specific instance is given all the time. You are responsible for its initial creation. Transient: A new instance is created every time. Singleton: A single instance is created and acts like a singleton. Scoped: A single instance is created inside the current scope. It is equivalent to Singleton in the current scope.

IoC, DI Container Demo We don't need to know anything about the service,other than that it implements the IProjectService interface, an interface custom to our demo project. IProjectService has one method, GetOrganization(). This method retrieves an organization and its corresponding list of projects. Demo

Summary Covered Dependency Inversion Principle Covered Inversion of Control Covered Dependency Injection Gave a demo using IoC