Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,

Slides:



Advertisements
Similar presentations
Object Oriented Design Principles Arnon Rotem-Gal-Oz Product Line Architect.
Advertisements

Design Principles & Patterns
St Louis Day of.NET 2011 Refactoring to a SOLID Foundation Steve Bohlen Senior Software Engineer SpringSource/VMware Blog:
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
ASP.NET MVC Best Practices Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
CZ Biz. Auto. System & Test-Driven Development Teoman Soygul (Sept 24, 2012).
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
OODP Prudent OO Design. OODP-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism.
Design Design and Software Architecture. The design phase The analysis phase describes what the system should be doing The design phase describes how.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SOLID Principles in Software Design
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Using Mock Objects with Test Driven Development Justin Kohlhepp
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
SOLID Design Principles
SOLID Principles in Software Design
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
SOLID PHP & Code Smell Wrap-Up
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Mantas Radzevičius ifm-2/2
Course information Old exam Resit Report Result and walkthrough
Dependency Inversion Principle
Copyright © Jim Fawcett Spring 2017
Software Architecture & Difference from Design
Mark Seemann - Dependency Injection in .NET
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Copyright © by Curt Hill
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
Objects First with Java A Practical Introduction using BlueJ
TechEd /17/2018 6:14 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
Tech·Ed North America /18/ :26 AM
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Design Tips.
The SOLID Principles.
A (partial) blueprint for dealing with change
Designing For Testability
European conference.
Object Oriented Design & Analysis
Dependency Inversion principle
Some principles for object oriented design
Chapter 10 – Component-Level Design
Presentation transcript:

Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th, 2010

Who the hell am I? ► Simone Chiaretta ► Microsoft MVP ASP.NET ► ASP Insider ► Blogger – ► Italian ALT.NET UG Founder ► OpenSource developer ► Climber ► All Around Nice Guy Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"

What are we going to talk about? ► What is “Good Design”? ► Testability requirements? ► What is Design for Testability? ► What is Dependency Injection? ► What is Inversion of Control? ► How to do IoC via DI using Ninject? ► How to do IoC via DI using Unity? ► References

What is Good Design?

What is Good Design ► High Cohesion ► Low Coupling ► Good Encapsulation

What is Good Design

Solid: Single Responsibility Principle (SRP) A class should have one, and only one, reason to change.

Solid: Single Responsibility Principle (SRP) “If a class has more then one responsibility, then the responsibilities become coupled. Changes to one responsibility may impair or inhibit the class’ ability to meet the others. This kind of coupling leads to fragile designs that break in unexpected ways when changed.” -Robert C. Martin

Solid: Single Responsibility Principle (SRP)

sOlid: Open Closed Principle (OCP) You should be able to extend a classes behavior, without modifying it.

sOlid: Open Closed Principle (OCP) “Modules that conform to the open-closed principle have two primary attributes. 1. They are “Open For Extension”. This means that the behavior of the module can be extended. That we can make the module behave in new and different ways as the requirements of the application change, or to meet the needs of new applications. 2. They are “Closed for Modification”. The source code of such a module is inviolate. No one is allowed to make source code changes to it.” - Robert C. Martin

sOlid: Open Closed Principle (OCP)

soLid: Liskov Substitution Principle (LSP) Derived classes must be substitutable for their base classes.

soLid: Liskov Substitution Principle (LSP) “If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.” - Barbara Liskov

soLid: Liskov Substitution Principle (LSP)

solId: Interface Segregation Principle (ISP) Clients should not be forced to depend upon interfaces they don’t use

solId: Interface Segregation Principle (ISP) “This principle deals with the disadvantages of ‘fat’ interfaces. Classes that have ‘fat’ interfaces are classes whose interfaces are not cohesive. In other words, the interfaces of the class can be broken up into groups of member functions. Each group serves a different set of clients. Thus some clients use one group of member functions, and other clients use the other groups.” - Robert Martin

solId: Interface Segregation Principle (ISP)

soliD: Dependency Inversion Principle (DIP) Depend on abstractions, not on concretions.

soliD: Dependency Inversion Principle (DIP) “What is it that makes a design rigid, fragile and immobile? It is the interdependence of the modules within that design. A design is rigid if it cannot be easily changed. Such rigidity is due to the fact that a single change to heavily interdependent software begins a cascade of changes in dependent modules.” - Robert Martin

soliD: Dependency Inversion Principle (DIP)

Before and After Before After

How to test for “good design”? ► You can’t ► Actually you can Clear?

Testability Requirements

Testability Actors ► System Under Test ► Depended On Component ► Mock/Fake/Stub

Testability Concepts ► Test just one feature ► Indipendency from environment ► Indipendency from dependencies ► Fast

Design for Testability

Design for Testability = Good Design ► Good design is difficult to measure ► Easily testable = Good Design

What is Dependency Injection

Bad Code Demo: Hard-Coded Dependencies 1-2

The problem of strong coupling ► Rigid – Must change the Climber code to change the Tools he uses ► Fragile – Changes to the Tools can affect the Climbers ► Not Testable – Cannot replace the Tools with a stub/fake when I want to test the Climber in isolation

Better Code Demo: Hard-Coded Dependencies with Interface 3

Still problems ► We have lower coupling but still Climber has to be changed to change tools

Slightly Better Code Demo: Hard-Coded Dependencies with Service Locator 4

Still problems ► Still Climber depends on the Locator ► Just moving the problem inside another module

Introducing Dependency Injection Demo: Dependency Injection by Hand 5

Good, isn’t it? ► Climber is always the same, and doesn’t know how to “get” the tools ► The Climber is given the tools he has to use

Dependency Injection Are we done? NOT YET!

Introducing Dependency Injection Demo: Dependency Injection by Hand (more complex) 6

Needs Improvements ► Host must know how to assemble dependencies ► We loose encapsulation

What is Inversion of Control

Inversion of Control Demo: Inversion of Control 7

What we achieved ► Still have DIP ► Got encapsulation back ► Dependencies are handled by external component

How to configure ► XML ► Attributes ► Fluent API ► all of them

Many IoCC …

Ninject

The Kernel ► Factory Method on Steroids ► Hold the configuration ► Returns objects IKernel kernel = new StandardKernel( new ClimbingModule()); var climber = kernel.Get ();

Modules ► Modules hold specific configurations ► Configuration through Fluent API Bind ().ToSelf(); Bind ().To ();

Inversion of Control Demo: Inversion of Control (complex) 8

Different kinds of Injection ► Constructor Injection ► Property Injection ► Method Injection ► Through Factory Method

Attributes ► Are used to help discriminate injection patterns [Inject] public IClimbingTools tools {get; set;} [Inject] public void GetReady(IClimbingTools tools)

Inversion of Control Demo: Attributes Injection Patterns 9

Behaviours ► Singleton (Default) ► Transient ► Per Thread ► Per Request ► BYO Bind ().ToSelf().InTransientScope(); Bind ().ToSelf().InSingletonScope();

Inversion of Control Demo: Activation Behaviours 10

But there is more... ► Constructor Parameters ► Contextual Binding ► Named Binding Bind ().To ().WithConstructorArgument("brand", "Black Diamond"); Bind ().To ().WhenInjectedInto(typeof(SportClimber)); Bind ().To ().Named("Simone"); climber = kernel.Get ("Simone");

Inversion of Control Demo: Advanced Features 11

Finally Some Testing ► No need to use IoC any more (and you should not) MockTools tools = new MockTools(); Climber climber = new Climber(tools); climber.Climb(); Assert.IsTrue(tools.Placed);

Finally some Testing Demo: Testing 12

P&P Unity

Unity ► Microsoft IoC container ► Configured via code ► Configured through XML myContainer.RegisterType ();

Unity Demo: Unity 13

Bonus section: Func

Func ► By Daniel Cazzulino (of Moq fame) ► The fastest IoC available ► Doesn’t use reflection ► Always write factory method container.Register ( c => new QuickDraws()); container.Register( c => new Climber( c.Resolve ()));

Bonus section: Func Demo: Func 14

IoC inside other hosts

IoC in other hosts ► IoC shines when activation is already delegated to factories –ASP.NET MVC –WCF ► Requires changes in the default “object factory” –ControllerFactory –ServiceHostFactory

IoC in other hosts Demo: ASP.NET MVC 15

Conclusions

Call for Actions ► Think about a project you worked on ► Think about any maintainabily/change issue you had: –Most likely they would have been solved with DI/IoC ► Think how DI/IoC could have helped

Main takeaways ► DI/IoC helps building service oriented applications ► DI/IoC helps managing dependencies ► DI/IoC helps bulding highly cohese, loose coupled code while maintaling encapsulation

References ► Uncle Bob’s Principle Of Object Oriented Development: od od ► OO Design Principles: ► Complete SOLID slides and demo (Derick Bailey): ► Ninject: - v2 - v2.2 beta ► p&p Unity: - v2 (part of EntLib5) ► Funq:

Contacts – Simone Chiaretta ► MSN: ► Blog: –English: –Italian: ► Twitter: 72

Questions? Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"

Rating If you liked this talk, please consider rating it: way-to-good-coding 74 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"