Build Testable Client and Service Applications

Similar documents
Prism Composite Application Guidance

Composite Application Guidance for WPF and Silverlight (AKA Prism 2 )

AR.04 Composite Application Guidance for WPF (aka Prism ) Brian Noyes IDesign Inc (

Thinking and Processing in Parallel Wrap Your Head Around WF 4.0 Concurrency. Brian Noyes IDesign Inc (

Building Extensible XAML Client Apps

Build Loosely Coupled Silverlight Business Applications

Build Custom Data Bound Objects and Collections

APP301: Implement a Data Access Layer with Enterprise Library

Building Loosely Coupled XAML Client Apps with Prism

VWC02 Build N-Tier Silverlight Data Applications Easily with WCF RIA Services

Drive Application Behavior with Application and User Configuration Settings Brian Noyes IDesign Inc (

Blissful Separation of Concerns with Model-View-ViewModel (MVVM)

Drive Application Behavior with Application and User Configuration Settings. Brian Noyes IDesign Inc (

VS10 WCF of Many Flavors When do I use which?

Building Extensible XAML Client Apps

WCF RIA Services. About Brian 8/10/2011. Brian Noyes Chief Architect IDesign Inc. (

Smart Client Offline Data Caching and Synchronization

VSC01 Securing WPF Client Applications

LVL08 Black Belt Silverlight Business Data Validation

NE.15 Data Binding In Windows Presentation Foundation

NET237: Deploying Smart Client Apps with ClickOnce

Learn to Behave Extend Your XAML with Behaviors

Windows Presentation Foundation In Windows Forms And Vice Versa

Data Binding with Windows Forms 2.0

Microsoft TechEd US, Europe, Malaysia, Visual Studio Connections, DevTeach, INETA Speakers Bureau, MSDN Webcasts

VS08 This One Goes to Going Parallel with PFX, PLINQ, TPL and Async Keywords

Data Binding in ASP.NET 2.0

Tackle Complex Data Binding in WinForms 2.0

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

Build Process Driven Applications with WF

Introduction to.net Deployment. Brian Noyes IDesign, Inc. (

Prism Composite Application Guidance

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

Introduction to Testing and Maintainable code

Top 7 Lessons From My First Big Silverlight Project

CSC207H: Software Design SOLID. CSC207 Winter 2018

Software Engineering

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

Single Responsibility Principle (SRP)

Test Your XAML-based Windows Store Apps with Visual Studio 2013 Benjamin Day

Smart Client Offline Data Caching and Synchronization

Clean Code Why Clean Code matters

Implementing MVVM in Real World ArcGIS Server Silverlight Applications. Brandon Copeland LJA Engineering, Inc.

Resolving Cyclic Dependencies Design for Testability

Agile Architecture. The Why, the What and the How

Philosophy of Unit Testing

Extending ASP.NET. Brian Noyes Principal Software Architect IDesign, Inc. ( (

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

17.11 Bean Rules persistent

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

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

Single Responsibility Principle

Bruno Bossola SOLID Design Principles

Embrace Factories Factories. By Rob Gonda

Build Better WPF & Silverlight applications using Prism v2

Test, Code, Design: Inverting the Waterfall

Chris Donnan & Solomon Duskis

Cross-Platform Mobile Platforms and Xamarin. Presented by Mir Majeed

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Test Driven Development (TDD), and Working with Legacy Code Using C# Workshop ( 4 days)

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

OO Class Design Principles

Influence of Design Patterns Application on Quality of IT Solutions

Open Closed Principle (OCP)

Building a mobile enterprise application with Xamarin.Forms, Docker, MVVM and.net Core. Gill

Summary of the course lectures

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

Object-Oriented Design

1 Software Architecture

Building Effective ASP.NET MVC 5.x Web Applications using Visual Studio 2013

Design Principles: Part 2

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

Principles of Object-Oriented Design

Object-Oriented Design I

GitHub code samples. Appendix B

Application Architectures, Design Patterns

Overview and Technical Design Insurance Agent Portal, Pomegranate

Pro XAML with C# From Design to Deployment on WPF, Windows Store, and Windows Phone. Buddy James. Lori Lalonde

17 Roberts St #2 Brookline, MA

COPYRIGHTED MATERIAL. Introducing the Project: The SmartCA Application. The Problem

Improve Your SystemVerilog OOP Skills

Design patterns. Jef De Smedt Beta VZW

Software Development Project. Kazi Masudul Alam

Design Principles: Part 2

Software Engineering I (02161)

Clean Architecture Patterns, Practices, and #DevSum17

Object-Oriented Design

Produced by. Agile Software Development. Eamonn de Leastar

Want Better Software? TEST it! (and thenwrite it) Tame defects before they appear You Rise/Bugs Fall

Don Smith, Program Manager Microsoft patterns & practices

COURSE 2 DESIGN PATTERNS

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

wałdis iljuczonok (aka technical fellow)

Agile Software Development

Architecting ios Project. Massimo Oliviero

1: ASP.NET AND JQUERY

Clean Architecture Patterns, Practices, and #sddconf

Apex TG India Pvt. Ltd.

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

EFFECTIVE C# (COVERS C# 6.0), (INCLUDES CONTENT UPDATE PROGRAM): 50 SPECIFIC WAYS TO IMPROVE YOUR C# (3RD EDITION) (EFFECTIVE SOFTWARE DEV

Transcription:

Build Testable Client and Service Applications Brian Noyes IDesign Inc (www.idesign.net) brian.noyes@idesign.net About Brian Chief Architect IDesign Inc. (www.idesign.net) Microsoft Regional Director MVP Microsoft MVP Connected Systems Publishing Developing Applications with Windows Workflow Foundation, LiveLessons training DVD, June 2007 Smart Client Deployment with ClickOnce, Addison Wesley, January 2007 Data Binding in Windows Forms 2.0, Addison Wesley, January 2006 MSDN Magazine, MSDN Online, CoDe Magazine, The Server Side.NET, asp.netpro, Visual Studio Magazine Speaking E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net Microsoft TechEd US, Europe, Malaysia, Visual Studio Connections, DevTeach, INETA Speakers Bureau, MSDN Webcasts 1

Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks Design for Testability Should this really be the way you think about it? A good design with high quality is the goal, not that you can test it Some changes to design and process are a necessity if you want to be testable Affects design at the component level, rarely at the system level May influence technology selections 2

TDD Should you use TDD? Probably Will it be hard to get used to? Probably Do I need to become a card-carrying zealot? No What does TDD buy you? Drives a clean design Ensures your app is testable Puts the tests in place as part of the process Mocks, Stubs, Fakes, oh my Terminology: Mock a test double that has expectations on how it is called Stub a test double that provides data to a test without breaking the test if its usage is different than the test specifies Fake has a real implementation, but simplified to just what the test needs Dummy object that provides a reference to unit under test, but is not called in general and does nothing Generalization: Mocks is often used to refer to all of these. Cleaner term is Test Double 3

Test Double Client Business Layer Component Data Layer Component Production Unit Test Business Layer Component Data Layer Double Test Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks 4

S.O.L.I.D. Principles Single Responsibility Principle (SRP) Open-Close Principle (OCP) Liskov Substitution Principle (LSP) Interface Segregation Principle (ISP) Dependency Inversion Principle (DIP) Documented by Robert (Uncle Bob) Martin of ObjectMentor http://butunclebob.com/articles.unclebob.principlesofood Single Responsibility Principle A class should have only one reason for change Ex: Order rich business object Contains data Implements business rules Does data persistence Should be at least 3 classes Simple, focused classes and methods are also much easier to test 5

Open-Close Principle You should be able to extend a class behavior without changing it Clients should depend on an abstraction of the actual object they are using Ex: Rendering engine IShape.Render() Open-Close Principle Extensions Basis for many modern best practices: Encapsulated member variables Aggressively avoid globals (static variables/props) Beware explicit casting Violating these practices is the enemy of testability 6

Liskov Substitution Principle Derived classes must be substitutable for their base classes Can also apply to component-based extensibility and plug-in architectures based on interfaces Drives Design by Contract mentality Ex: Rectangle/Square hierarchy What should happen if someone calls SetHeight on a Square vs a Rectangle? Dependency Inversion Principle Depend on abstractions, not concrete implementations Allow multiple concrete implementations to be substituted Basis for interface-based programming ESSENTIAL for testability Leads to dependency injection patterns 7

Interface Segregation Principle Make fine-grained interfaces that are client specific Separate contract per-usage scenario SRP extended to interface design Prevents needing to change a client if some functionality they don t use does not change Avoid potential for breakage Looser coupling facilitates testing Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks 8

Inversion of Control / Dependency Injection Closely related patterns IoC: Pattern for indirect construction of objects Delegate responsibility for construction of objects to a Container Dependency Injection Dependencies of an object are injected from the outside instead of constructing internally Most Containers do both Container constructs objects and injects their dependencies Dependencies are often located using Service Locator pattern.net DI Containers Unity MEF? Castle Windsor StructureMap Spring.NET NInject CAB WorkItem 9

DI Container Functions Object construction Injection of dependencies Child object relations Service Locator Lifetime management How long does the object live and what instance is given to the caller when they ask DI and Testability DI allows you to design a more loosely coupled application Generally need to adhere to S.O.L.I.D. Principles for it to work out Good pattern even if you don t unit test Extensibility, auto-wiring, centralized lifetime management Allows easy substitution of test doubles 10

DI and WCF WCF normally constructs service instances itself based on your selected instancing model Per Call, Per Session, Singleton To leverage a DI container in WCF, you have to intercept the construction process and let the DI container do it Service behavior Instance Provider Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks 11

Model View Controller (MVC) Model is the business logic and data View is a single page or chunk of screen Controller decides when to present each view / coordinates view-view interactions Model View Presenter 12

Presentation Model Also known as Model-View-ViewModel in the WPF / Silverlight world (MVVM) ViewModel offers up state to the view in the way the view wants to see it ViewModel Offers up state to the view through simple properties Encapsulates interaction logic to support the view Initial load of data Command handlers Should be loosely coupled to the view Relationship with the view 1:1 most common 1 view : multiple view models (example: Add/Edit ) Multiple views : 1 ViewModel (example: listing/details views) Properties should raise change events Implement INotifyPropertyChanged or DependencyProperties 13

Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks Repository Pattern Fowler: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. Interface separation between persistence strategy and application usage of data Allows you to change implementation Allows you to test with double implementation Simple collection oriented API (All, Where, Update,Delete,etc.) Can include other transmogrify methods (SimpleCustomerWhere) May have multiple entity types on a single repository interface Usually supporting types for a single parent concept (i.e. Customer/Address) 14

Repositories Using the repository pattern can help a lot in testing Isolate the specifics of persistence from the consuming objects Persistence is the most test-resistant part of your code Become a point for dependency injection for testing Allow you to switch to a different implementation strategy later with no impact on application i.e. LINQ to SQL -> Entity Framework Agenda Design for Testability S.O.L.I.D. Principles IoC / Dependency Injection Separated Presentation Repository Pattern Testability Tricks 15

Testability Tricks Avoid inline construction of dependencies Define dependencies as members if possible Define dependency injection constructors Even if you don t use a container Use for tests to inject test doubles Use overridable factory methods for dynamically created dependencies Can override in a test version of the class to inject test doubles Avoid static members / singletons Create Simulators and Emulators for your services Leverage a real isolation framework Rhino Mocks, MOQ, TypeMock Isolator, etc. G -word Guide http://misko.hevery.com/code-reviewersguide/ Smells: Constructor does real work Digging in to collaborators (dependencies) Law of Demeter Brittle Global State and Singletons Class does too much 16

Resources S.O.L.I.D. principles: http://butunclebob.com/articles.unclebob.principlesofood Repository http://codebetter.com/blogs/karlseguin/archive/2008/12/22/new-repository-lt-t-gt-domagic.aspx http://blog.wekeroad.com/mvc-storefront/asp-net-mvc-mvc-storefront-part-2/ Unity http://www.codeplex.com/unity Separated Presentation http://martinfowler.com/eaadev/separatedpresentation.html Using DI: http://www.codethinked.com/post/2009/03/23/to-inject-or-not-to-inject.aspx E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net 17