Building SOLID Software with Dependency Injection Jeremy Rosenberg.

Slides:



Advertisements
Similar presentations
St Louis Day of.NET 2011 Refactoring to a SOLID Foundation Steve Bohlen Senior Software Engineer SpringSource/VMware Blog:
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
SOLID Object Oriented Design Craig Berntson
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Word Cards by Team 6 Problem: Learning new words and languages should be made accessible and enjoyable through the use of software – Enable learners to.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
CZ Biz. Auto. System & Test-Driven Development Teoman Soygul (Sept 24, 2012).
Design Patterns.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Object-Oriented Programming and the Progress ABL Tomáš Kučera Principal Solution Engineer / EMEA Power Team.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Course Overview & Topics CSc 335: Object-Oriented Programming and Design © Rick Mercer 1.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SOLID Principles in Software Design
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Using Mock Objects with Test Driven Development Justin Kohlhepp
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Introducing Allors Applications, Tools & Platform.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Five design principles
OOP Review CS 124.
G.Govi CERN/IT-DB 1 September 26, 2003 POOL Integration, Testing and Release Procedure Integration  Packages structure  External dependencies  Configuration.
SOLID Design Principles
Applying the Principles Two Examples. Example 1 New Requirement It would be nice with a simple GUI “to see something” instead of just xUnit tests...
SOLID PHP & Code Smell Wrap-Up
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Clean Code and How to Achieve Zero Defects Jason Jolley Director, Application Development Micro Strategies, Inc.
Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -
Principled N-Tier Design or, a Solution to the Solution Problem Steve | ardalis.com Telerik, Inc.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Design I. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out Portability.
Mantas Radzevičius ifm-2/2
“When quality is critical"
(on behalf of the POOL team)
Software Architecture & Difference from Design
Mark Seemann - Dependency Injection in .NET
Copyright © by Curt Hill
object oriented Principles of software design
Adaptive Code Umamaheswaran Senior Software Engineer
Object Oriented Practices
Object-Oriented Design
Object Oriented Practices
Object Oriented Practices
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
A (partial) blueprint for dealing with change
Designing For Testability
European conference.
Object Oriented Design & Analysis
Dependency Inversion principle
Liskov Substitution Principle (LSP)
Dependency Injection Mechanism
Presentation transcript:

Building SOLID Software with Dependency Injection Jeremy Rosenberg

Me

Who cares about software design?

What are our goals? Deliver yesterday Communicate telepathically No bugs

What are our goals? Deliver yesterday Communicate telepathically No bugs

What are our goals? Deliver quickly Communicate quickly Few bugs Fix bugs quickly Deliver yesterday Communicate telepathically No bugs

What are our goals? Deliver quickly Communicate quickly Few bugs Fix bugs quickly Deliver yesterday Communicate telepathically No bugs aka Efficiently deal with Change

What is a bad design?

A bad design causes these Deliver slowly or unpredictably Slow ramp-up for new team members Slow sharing between veteran team members

A bad design causes these Frequent bugs Simple bugs need complex changes Lots of regressions Lots of regression testing

What is a good design?

What is SOLID?

What it is Catchy acronym Principles for dealing with change

What it is Catchy acronym Principles for dealing with change

What it is Catchy acronym Principles for dealing with change SDOLI

What it’s not Framework Library Pattern Goal

What is Dependency Injection? Pattern Jives well with SOLID

What kinds of code? Object-oriented Statically typed* (code examples are in C#)

What does this method do?

– Coordinates a book checkout process for a member Checkout Storage

What does this method do? – Coordinates a book checkout process for a member Checkout Storage Is that all?

What does this class do?

Configuration DB Access

So what does this method really do? Checkout Storage

So what does this method really do? Checkout Storage – Configuration – DB access Implicit!

Let’s write a test case

SDOLI: Single Responsibility Principle “Do one thing and do it well” Applies to a method, class, service, system, …

How do we isolate the coordination?

SDOLI: Dependency Inversion Principle Depend on contracts, not implementations Goal: Decouple the single responsibility from implementations of its dependencies

SDOLI: Dependency Inversion Principle Depend on a contract at design-time – e.g. IBookRepository or BookRepositoryBase Receive an implementation at runtime – e.g. SqlBookRepository

Dependency Injection A class declares its dependencies through its own contract Providing dependencies is someone else’s job

Dependency Injection Constructor injection Property injection Method injection

Constructor injection

Our test case No configuration No database Only coordination

All this “just” for test cases? What else have we gained? – “Tightly bound, loosely coupled” Focused responsibility => readability Dependent on behavior, not implementation – Dependencies are explicit Limits unexpected side effects Straightforward (re)usage – Composability

You’re a hack!

Composition Root Constructs the object graph Lives near the entry point

Example: Console app

What about that repository?

Make the dependencies explicit

Updated Composition Root

With me?

SDOLI: Open/Closed Principle Open for extensibility, closed for modification Add behavior without changing existing code

Decorator Inject behavior into an application Man-in-the-middle

Example: Document storage

Composition Root

Add Logging

Add Authorization

Add Caching

Noticing a pattern?

Open…

Closed.

Composite Implements a contract Delegates to others with the same contract

Example

Build it like this

Use it like this

A more concrete example

Move the responsibility

Notify billing

Notify inventory

Notify shipping

Dispatch all notifications

Open / closed

SDOLI: Liskov Substitution Principle Functions that use pointers to base types must be able to use objects of derived types without knowing it. “Leaky abstraction principle” -Me

Is a Square a Rectangle?

SetWidth / SetHeight ?

Example: calculator

Usage

Guess what’s next

Oops

Plug the leak – Part 1

- Nine fingers left!

Plug the leak – Part 2

What to do Some ideas – Kill the abstraction – Adapt each calculator to something that supports input validation

Another example: more documents…

Read-only integration

Oops

Plug the leak

What to do? Some ideas – IReadOnlyDocRepo and IReadWriteDocRepo – IReadDocs and IWriteDocs – SupportsXYZ methods

How can DI help apply the LSP?

It can’t.

SDOLI: Interface Segregation Principle Thin interfaces are better than fat ones Client-focused

Example: more books…

Who’s using it?

Cache books?

Don’t need these

These are ok

What about these?

Client-specific interface

Adapter This: or this:

Slimmer caching

Focused dependencies

Let’s review

SOLID Principles S ingle Responsibility O pen/Closed L iskov Substitution I nterface Segregation D ependency Inversion

I don’t know about you…

DI Container Auto-wiring of dependencies Lifetime management

Ninject

Register

Ninject Register Resolve

Ninject Register Resolve Release

Other containers Castle Windsor Autofac StructureMap Unity …

What not to do

Don’t use Service Locator

Hides dependencies Consumers must use the same container Testing becomes un-fun

Don’t use Service Locator Composition root should be the only place referencing the DI Container. General rule: if it’s not just initializing the object graph, it’s not the composition root.

Don’t just new it

Useless interface definition Same tight coupling No way to swap, decorate, or otherwise compose implementations

Don’t be double minded

Two paths… Dependencies are still not always apparent Auto-wiring can be difficult

Building SOLID software

SOLID encourages good designs Dependency injection is a tool to bring closer alignment with SOLID

Join us

Resources The Principles of OOD Dependency Injection in.NET by Mark Seemann ploeh blog – DependencyInjection SOLID (object-oriented design)