Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.

Slides:



Advertisements
Similar presentations
T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
Advertisements

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
1 Layers Data from IBM-Rational and Craig Larman’s text integrated into these slides. These are great references… Slides from these sources have been modified.
St Testing, Simulation and Monitoring (actually mostly simulation) Stephen Hillier Joint Meeting, Mainz, June 2001.
Lecture 23: Software Architectures
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
4/2/03I-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Software Architecture and Design Readings: Ambler, Chap. 7 (Sections to start.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Software Design Principles
Programming in Java CSCI-2220 Object Oriented Programming.
Introducing Allors Applications, Tools & Platform.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Five design principles
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Advanced Object-oriented Design Patterns Creational Design Patterns.
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
Dependency Inversion Principle Jon McBee Principal Software Engineer Ultratech CNT.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
Object-Oriented Design Concepts University of Sunderland.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
Implementing Distribution and Persistence Aspects with AspectJ WAS CLASS.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Layers Architecture Pattern Source: Pattern-Oriented Software Architecture, Vol. 1, Buschmann, et al.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Dependency Injection with Guice Technion – Institute of Technology Author: Gal Lalouche - Technion 2016 ©
Principled N-Tier Design or, a Solution to the Solution Problem Steve | ardalis.com Telerik, Inc.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
Process 4 Hours.
Interface Segregation / Dependency Inversion
Dependency Inversion Principle
Architecture Patterns Design Patterns
Operating System Structures
Template Method Pattern Iterator Pattern
Designing software applications
How to be a Good Developer
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Software Design and Architecture
Copyright © by Curt Hill
Chapter 9 - Programming to Interfaces
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Object Oriented Concepts
Component-Level Design
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Questions First On class? On project? On material we’ve discussed?
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Principles of Object-Oriented Design
Information Hidding Dr. Veton Kepuska.
European conference.
Dependency Inversion principle
Presentation transcript:

Dependency Inversion By Steve Faurie

Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin

Dependency Inversion Basic Principles High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions

High Level vs Low Level A high level module defines what your application does It contains the policy decisions and business models that define the entire purpose of the application A high level module should be separated from and not dependent upon the implementation details implemented in lower level modules. Low level modules contain implementation details Ex. Calls to a particular type of database

Layering in object oriented programs Well designed object oriented architectures should have clearly defined layers. Each layer should: Provide some coherent set of services Provide those services through a well defined interface

Naïve Layering: In the code: Policy Layer calls new MechanismLayer Mechanism Layer calls new Utility Layer Problems? Changes to the mechanism layer could require changes to the policy layer. Policy Layer transitively dependent upon all layers below it

Ownership Inversion In Code: Policy layer does not directly declare Mechanism Layer. No code like: MechanismLayer ml = new MechanismLayer; Policy layer will have something like: IPolicyService myService; myService can be an instance of anything that implements IPolicyService Mechanism Layer will implement the IPolicyService interface and must conform to its expectations Implementations dependent upon policy now!

How do I “new” up stuff? Factory Pattern Returns concrete versions of the interfaces your policy layer uses. Dependency Injection Complicated way to inject concrete implementations of interfaces into objects. Ask for ISomeInterface, inject the concrete type you have set up the injector to return.

Factory Example

Abstractions No variable should have a reference to a concrete class Reference to interface or abstract class means variable can be anything that implements that interface No class should derive from a concrete class Don’t extend concrete classes No method should override an implemented method of any of its base classes. If you extend an abstract class don’t overwrite the functionality built into it.

A Simple Example From Robert C. Martin The button is polling for input Upon input it calls the lamp turnOn or turnOff method The button class depends on the lamp class, but shouldn’t a button be able to turn other things on or off too?

The Button Lamp Example With inverted dependencies The button no longer references a concrete class. The button can control anything that implements the ButtonServer interface Does lamp depend on button? No, lamp can be controlled by anything that can control the button server interface

Why bother? Consider having your policy layer dependent upon a specific type of database What if your IT department doesn’t want to support the type of database you are currently using? What if the current database you are using is no longer adequate for your needs? Your entire policy layer that was based around a connection to a specific type of database is now worthless and will need to be rewritten.

Solution: IMyDataRepository Your policy layer will not reference specific database objects, rather it will have a variable of type IMyDataRepository

Implementations of IMyDataRepository