Sunteți pe pagina 1din 8

Agenda

Do Application Design Patterns Make •What are Design Patterns?


Sense in ASP.NET? •Basic Design Patterns for ASP.NET
•Basic Pattern Support within ASP.NET
Alex Homer
•Controller Patterns for ASP.NET
alex@stonebroom.com
•Advanced Patterns for ASP.NET
You may like to write these down now... •Conclusion and Summary
•http://www.daveandal.net/articles/
•http://www.daveandal.net/download/

Design Patterns are Scary...! You Use Patterns Every Day...


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

Using xr As XmlReader = XmlReader.Create("file.xml")


... read some XML ...
End Using Lifetime

What are Design Patterns? Why Do We Need Design Patterns?


•Informal Design Patterns •Engineering disciplines require patterns
–Code constructs, best practice, well-structured, –Structural Engineering
common sense, the accepted approach, evolved –Electronic Engineering
over time –... even Social Engineering!
•Formal Design Patterns •Creating software is also Engineering
–Documented as "Context", "Problem", "Solution", –Structural design and architecture
and a UML diagram –Componentization and interconnections
–Have specific aims –User interface design and implementation
–Solve specific issues –Deployment, testing, and management
Formal Design Patterns The Gang of Four (GOF)
"Fundamental to any science or engineering discipline
is a common vocabulary for expressing its concepts,
and a language for relating them together." • Erich Gamma, Richard Helm,
Ralph Johnson, and John Vlissides
"... a body of literature to help software developers
• "Design Patterns: Elements of
resolve recurring problems encountered throughout
Reusable Object-Oriented
all of software development."
Software" (1995)
"... a shared language for communicating insight and
experience about these problems and their
n The Hillside Group
solutions." http://hillside.net/patterns/DPBook/GOF.html
from http://www.scmpatterns.com/

Microsoft .NET Design Patterns .NET Design Pattern Frameworks


•"data & object factory"
• "Enterprise Solution Patterns Using –Design Pattern Framework 2.0
Microsoft .NET" – http://www.dofactory.com/Framework/Framework.aspx
• From the patterns and practices • Ingenious MVC for .NET 2.0 Web and WinForms
(p&p) Group – http://sourceforge.net/projects/ingeniousmvc
• Printed book (ISBN: 0735618399) •Microsoft "patterns & practices" Group
– CAB and ObjectBuilder
n PDF Download
– Enterprise Library
• http://www.microsoft.com/downloads/details.aspx?familyid=3C8
1C38E-ABFC-484F-A076-CF99B3485754 – Software Factories (Smart Client, Mobile, Web Service)
– http://msdn.microsoft.com/practices/

Pattern Types and Families Common Patterns in ASP.NET


• Model-View-Controller (MVC)
Presentation
• Model-View-Presenter (MVP) Logic
• Use Case Controller
• Command
Host or
• Publish-Subscribe / Observer Behavioral
• Plug-in / Module / Intercepting Filter
• Service Agent / Proxy / Broker
Structural
• Provider / Adapter
• Factory / Builder / Injection
Creational
• Singleton
These are just a few of the common ones ...
• Repository Persistence
PatternShare.org lists more than 250 !
Agenda MVC & MVP Presentation Patterns
•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
• Both improve reusability of business logic
• MVC has dependency between model and view
• MVP improves testability but adds complexity

Provider / Adapter Patterns Service Agent / Proxy / Broker

n Used by ASP.NET itself, and other frameworks n Removes dependencies between client and
such as Enterprise Library service through intermediate brokers
n Allows behavioral changes without prior n Range of different implementations, with or
knowledge of requirements without service agent logic component

Repository Persistence Pattern Singleton Creation Pattern


n Provides for creation of a class for which
only a single instance can exist
n Useful for exposing read-only data
n Useful for exposing static methods that do
not rely on instance data
n Include private default constructor to
n Virtualizes storage of an entity in a persistent prevent client instantiation
medium, such as a database or as XML n Provide a static GetInstance method that
n Hides storage implementation from the returns the current instance
application code
Agenda Pattern Support within ASP.NET
•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

Pattern Support within ASP.NET Pattern Support within ASP.NET

Pattern Support within ASP.NET Pattern Support within ASP.NET


How To... Basic Patterns in ASP.NET Example
• 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

Agenda Use Case Controller


•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

n Coordinates and sequences interaction between


the system and the users to carry out a process

Page Controller & Front Controller Plug-in/Module/Intercepting Filter


n Allows the features or behavior of an application
to change when it loads separate components
that implement extra functionality
n Plug-ins and Modules may be custom assemblies
(modules), or general-purpose software
components such as ActiveX controls or Java
applets
n Intercepting Filters reside in an application
pipeline, such as the ASP.NET HTTP pipeline
• Select content to display in a page using different n Commonly, the components extend the features
partial views, or...
of the host application
• Select which page (view) to display
Implementation in ASP.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
• HTTP Module performs redirection at front
appropriate Server.Transfer
• Page Controller selects view elements

Page & Front Controller Example 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

Factory / Builder / Injection Command Design Pattern


n Separates the construction of a complex object
from its representation
n Allows use of the same construction process to
create different representations
n In the Factory pattern, the subclasses of the
object generator determine the object type
n In the Builder and Injection patterns, the client
passes instructions or hints to the object
generator to specify the required object type.
• Separates command invoker and receiver
Observer and Publish/Subscribe Command-Observer Implementation

n Separates creator and receiver(s) of events • Client creates and subscribes objects

Command-Observer Implementation Command-Observer Example

• Introduces a dependency between Observer and


Subject, but removes the requirement for events

Observer and Publish/Subscribe Publish-Subscribe Example

n Separates creator and receiver(s) of events


Conclusions - 1 Conclusions - 2
• MVP and the various Controller patterns are useful, but • Provider is useful for accessing various types of data
firing update events from Model usually is not. sources.
• Page Controller and Front Controller allow for custom • Adapter is useful for extending ASP.NET controls.
use case behavior by showing different views or • Factory, Builder, Injection, Observer, and Command are
activating different presenters. Front Controller can make probably less useful, more complex, and can be difficult
use of the Intercepting Filter pattern. to implement.
• Repository is useful for virtualization of source data. • Event-driven patterns like Publish-Subscribe are generally
• Singleton is useful for reducing the need for multiple useful only if all subscribers are instantiated within the
instances. page lifetime.
• Service Agent and Proxy are ideal for interacting with • The composite Command-Observer event-free approach
remote services. is useful for removing dependencies between classes.

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: alex@stonebroom.com
• Slides & code: http://www.daveandal.net/download/
• Article: http://www.daveandal.net/articles/

S-ar putea să vă placă și