daveandal.net do application design patterns make sense in asp.net? alex homer [email protected]...

45
DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer [email protected] You may like to write these down now... http://www.daveandal.net/article s/

Upload: benedict-cooper

Post on 16-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Do Application Design Patterns Make Sense in ASP.NET?

Alex [email protected]

You may like to write these down now...• http://www.daveandal.net/articles/• http://www.daveandal.net/download/

Page 2: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Agenda

What are Design Patterns? Basic Design Patterns for ASP.NET Basic Pattern Support within ASP.NET Controller Patterns for ASP.NET Advanced Patterns for ASP.NET Conclusion and Summary

Page 3: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Design Patterns are Scary...!

Page 4: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

You Use Patterns Every Day...

try { something }catch (SqlException qx){ handle SQL error }catch (SecurityException sx){ handle security violation }catch (Exception ex){ handle all other exceptions }finally{ clean up resources }

Using xr As XmlReader = XmlReader.Create("file.xml") ... read some XML ...End Using

StructuredExceptionHandling

Factory

Lifetime

Page 5: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

What are Design Patterns?

Informal Design Patterns• Code constructs, best practice, well-

structured, common sense, the accepted approach, evolved over time

Formal Design Patterns• Documented as "Context", "Problem",

"Solution", and a UML diagram

• Have specific aims

• Solve specific issues

Page 6: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Why Do We Need Design Patterns?

Engineering disciplines require patterns • Structural Engineering

• Electronic Engineering

• ... even Social Engineering!

Creating software is also Engineering• Structural design and architecture

• Componentization and interconnections

• User interface design and implementation

• Deployment, testing, and management

Page 7: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Formal Design Patterns"Fundamental to any science or engineering

discipline is a common vocabulary for expressing its concepts, and a language for relating them together."

"... a body of literature to help software developers resolve recurring problems encountered throughout all of software development."

"... a shared language for communicating insight and experience about these problems and their solutions."

from http://www.scmpatterns.com/

Page 8: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

The Gang of Four (GOF)

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

"Design Patterns: Elements of Reusable Object-Oriented Software" (1995)

The Hillside Group http://hillside.net/patterns/DPBook/GOF.html

Page 9: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Microsoft .NET Design Patterns

"Enterprise Solution Patterns Using Microsoft .NET"

From the patterns and practices (p&p) Group

Printed book (ISBN: 0735618399) PDF Download

• http://www.microsoft.com/downloads/details.aspx?familyid=3C81C38E-ABFC-484F-A076-CF99B3485754

Page 10: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

.NET Design Pattern Frameworks

"data & object factory"• Design Pattern Framework 2.0• http://www.dofactory.com/Framework/

Framework.aspx

Ingenious MVC for .NET 2.0 Web and WinForms• http://sourceforge.net/projects/ingeniousmvc

Microsoft "patterns & practices" Group• CAB and ObjectBuilder

• Enterprise Library

• Software Factories (Smart Client, Mobile, Web Service)

• http://msdn.microsoft.com/practices/

Page 11: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Types and Families

These are just a few of the common ones ...PatternShare.org lists more than 250 !

Page 12: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Common Patterns in ASP.NET Model-View-Controller (MVC) Model-View-Presenter (MVP) Use Case Controller Command Publish-Subscribe / Observer Plug-in / Module / Intercepting Filter Service Agent / Proxy / Broker Provider / Adapter Factory / Builder / Injection Singleton Repository

Presentation Logic

Host or Behavioral

Creational

Structural

Persistence

Page 13: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Agenda

What are Design Patterns? Basic Design Patterns for ASP.NET Basic Pattern Support within ASP.NET Controller Patterns for ASP.NET Advanced Patterns for ASP.NET Conclusion and Summary

Page 14: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

MVC & MVP Presentation Patterns

Both improve reusability of business logic MVC has dependency between model and view MVP improves testability but adds complexity

Page 15: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Provider / Adapter Patterns

Used by ASP.NET itself, and other frameworks such as Enterprise Library

Allows behavioral changes without prior knowledge of requirements

Page 16: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Service Agent / Proxy / Broker

Removes dependencies between client and service through intermediate brokers

Range of different implementations, with or without service agent logic component

Page 17: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Repository Persistence Pattern

Virtualizes storage of an entity in a persistent medium, such as a database or as XML

Hides storage implementation from the application code

Page 18: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Singleton Creation Pattern Provides for creation of a class for

which only a single instance can exist Useful for exposing read-only data Useful for exposing static methods

that do not rely on instance data Include private default constructor to

prevent client instantiation Provide a static GetInstance method

that returns the current instance

Page 19: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Agenda

What are Design Patterns? Basic Design Patterns for ASP.NET Basic Pattern Support within ASP.NET Controller Patterns for ASP.NET Advanced Patterns for ASP.NET Conclusion and Summary

Page 20: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Support within ASP.NET

Page 21: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Support within ASP.NET

Page 22: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Support within ASP.NET

Page 23: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Support within ASP.NET

Page 24: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Pattern Support within ASP.NET

Page 25: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

How To... MVP: use ASP.NET code-behind model or

extend code file with custom classes

Provider: use built-in providers or create custom providers from a base class or interface

Repository: typed DataSet, third-party tools (such as CodeSmith), or custom code

Adapter: use built-in (such as CSS control adapters) or create custom adapters

Service Agent and Proxy: Web References in Visual Studio, maybe wrap in custom class

Page 26: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Basic Patterns in ASP.NET Example

Page 27: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Agenda

What are Design Patterns? Basic Design Patterns for ASP.NET Basic Pattern Support within ASP.NET Controller Patterns for ASP.NET Advanced Patterns for ASP.NET Conclusion and Summary

Page 28: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Use Case Controller

Coordinates and sequences interaction between the system and the users to carry out a process

Page 29: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Page Controller & Front Controller

Select content to display in a page using different partial views, or...

Select which page (view) to display

Page 30: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Plug-in/Module/Intercepting Filter

Allows the features or behavior of an application to change when it loads separate components that implement extra functionality

Plug-ins and Modules may be custom assemblies (modules), or general-purpose software components such as ActiveX controls or Java applets

Intercepting Filters reside in an application pipeline, such as the ASP.NET HTTP pipeline

Commonly, the components extend the features of the host application

Page 31: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Implementation in ASP.NET

HTTP Module performs redirection at front Page Controller selects view elements

Page 32: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

How To... Use Case: custom branching code in

presenter class - case statements using Server.Transfer, or displaying partial Views as User Controls

Page Controller: custom base class for Page that handles Init or Load event to perform common tasks then calls a method in the actual page to create page-specific content

Front Controller: an HTTP Module that adds a custom handler to a pipeline event that performs the appropriate Server.Transfer

Page 33: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Page & Front Controller Example

Page 34: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Agenda

What are Design Patterns? Basic Design Patterns for ASP.NET Basic Pattern Support within ASP.NET Controller Patterns for ASP.NET Advanced Patterns for ASP.NET Conclusion and Summary

Page 35: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Factory / Builder / Injection

Separates the construction of a complex object from its representation

Allows use of the same construction process to create different representations

In the Factory pattern, the subclasses of the object generator determine the object type

In the Builder and Injection patterns, the client passes instructions or hints to the object generator to specify the required object type.

Page 36: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Command Design Pattern

Separates command invoker and receiver

Page 37: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Observer and Publish/Subscribe

Separates creator and receiver(s) of events

Page 38: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Command-Observer Implementation

Client creates and subscribes objects

Page 39: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Command-Observer Implementation

Introduces a dependency between Observer and Subject, but removes the requirement for events

Page 40: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Command-Observer Example

Page 41: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Observer and Publish/Subscribe

Separates creator and receiver(s) of events

Page 42: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Publish-Subscribe Example

Page 43: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Conclusions - 1

MVP and the various Controller patterns are useful, but firing update events from Model usually is not.

Page Controller and Front Controller allow for custom use case behavior by showing different views or activating different presenters. Front Controller can make use of the Intercepting Filter pattern.

Repository is useful for virtualization of source data.

Singleton is useful for reducing the need for multiple instances.

Service Agent and Proxy are ideal for interacting with remote services.

Page 44: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

Conclusions - 2

Provider is useful for accessing various types of data sources.

Adapter is useful for extending ASP.NET controls.

Factory, Builder, Injection, Observer, and Command are probably less useful, more complex, and can be difficult to implement.

Event-driven patterns like Publish-Subscribe are generally useful only if all subscribers are instantiated within the page lifetime.

The composite Command-Observer event-free approach is useful for removing dependencies between classes.

Page 45: DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer alex@stonebroom.com You may like to write these down now

DaveAndAl.net

References Information about Design Patterns:

• http://msdn.microsoft.com/library/en-us/dnpag/html/intpatt.asp

• http://www.patternshare.org/• http://msdn.microsoft.com/architecture/• http://msdn.microsoft.com/practices/• http://www.dofactory.com/Patterns/Patterns.aspx• http://hillside.net/

Contact: [email protected] Slides & code: http://www.daveandal.net/download/ Article: http://www.daveandal.net/articles/