Improving feeding practices: Current patterns, common constraints ...
Common asp.net design patterns aspconf2012
-
Upload
steven-smith -
Category
Technology
-
view
111 -
download
2
description
Transcript of Common asp.net design patterns aspconf2012
![Page 1: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/1.jpg)
Common ASP.NET Design Patterns
Steve Smith@ardalis | ardalis.comTelerik
http://flic.kr/p/4MEeVn
![Page 2: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/2.jpg)
http://about.me/stevenasmith
![Page 3: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/3.jpg)
Design Patterns
http://flic.kr/p/8XgqKu
![Page 4: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/4.jpg)
Stages of Learning
Stage Zero - Ignorance
Stage One - Awakening
Stage Two - Overzealous
Stage Three – Mastery
http://flic.kr/p/6StgW5
![Page 5: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/5.jpg)
Language
http://flic.kr/p/6UpMt9
![Page 6: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/6.jpg)
http://geekandpoke.typepad.com/geekandpoke/2011/04/design-patterns-are-still-useful.html
![Page 7: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/7.jpg)
Actual Usagehttp://twtpoll.com/r/t7jzrx
![Page 8: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/8.jpg)
Common Design Patterns
0.Singleton1.Strategy2.Repository3.Proxy4.Command5.Factory
(Of course there are many others!)
![Page 9: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/9.jpg)
(Singleton)
![Page 10: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/10.jpg)
Singleton: Intent
Ensure a class has only one instance
Make the class itself responsible for keeping track of its sole instance
“There can be only one”
![Page 11: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/11.jpg)
Singleton Structure (not thread-safe)
![Page 12: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/12.jpg)
How Singleton Is Used
Call methods on Instance directly
Assign variables to Instance
Pass as Parameter
![Page 13: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/13.jpg)
Singleton Structure (thread-safe and fast)
Source: http://csharpindepth.com/Articles/General/Singleton.aspx
![Page 14: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/14.jpg)
Singleton Consequences The default implementation not thread-safe
Should not be used in multi-threaded environments Should not be used in web server scenarios (e.g. ASP.NET)
Singletons introduce tight coupling among collaborating classes
Singletons are notoriously difficult to test Commonly regarded as an anti-pattern
Violate the Single Responsibility Principle Class is responsible for managing its instances as well as
whatever it does
Using an IOC Container it is straightforward to avoid the coupling and testability issues
Shameless Plug:Telerik JustMock can be used to mock and test
Singletons
But why would you intentionally write code you can only test with
certain premium tools?
![Page 15: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/15.jpg)
Strategy
http://flic.kr/p/6spkwo
![Page 16: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/16.jpg)
Strategy: Intent Encapsulate a family of related algorithms
Let the algorithm vary and evolve independently from the class(es) that use it
Allow a class to maintain a single purpose Single Responsibility Principle (SRP) Also enables Open/Closed Principle (OCP) Learn more about SRP and OCP here: http://bit.ly/hfT16I
![Page 17: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/17.jpg)
Strategy : Structure
![Page 18: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/18.jpg)
Strategy : Common Usage
Dependency Inversion and Dependency Injection
Decouple class dependencies and responsibilities
Create interfaces for each responsibility
Inject the implementation of the interface via the constructor
Move the implementation to a new class that implements the interface
![Page 19: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/19.jpg)
Hidden Dependencies Classes should declare their dependencies via their
constructor
Avoid hidden dependencies Anything the class needs but which is not passed into
constructor
Avoid making non-stateless static calls Avoid directly instantiating classes (except those
with no dependencies, like strings)
Instead, move these calls to an interface, and call a local instance of the interface
![Page 20: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/20.jpg)
“New is Glue”
Be conscious of the consequences of using “new”
If you need loose coupling, replace “new” with Strategy Pattern
http://flic.kr/p/aN4Zv
“new” creates tight coupling between classes
![Page 21: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/21.jpg)
Repository A Data Access Pattern
Separates persistence responsibility from business classes
Enables Single Responsibility Principle Separation of Concerns Testability
Frequently Separate Interfaces for Each Entity in Application E.g. CustomerRepository, OrderRepository, etc. Or using Generics: IRepository<TEntity>
![Page 22: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/22.jpg)
Data Access Evolution
No separation of concerns:
Data access logic baked directly into UI ASP.NET Data Source Controls Classic ASP scripts
Data access logic in UI layer via codebehind ASP.NET Page_Load event ASP.NET Button_Click event
User Interface
Database
Compile Time
Runtime
![Page 23: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/23.jpg)
Data Access : Helper Classes
Calls to data made through a utility
Example: Data Access Application Block (SqlHelper)
Logic may still live in UI layer
Or a Business Logic Layer may make calls to a Data Access Layer which might then call the helper
User Interface
Database
Compile Time
Runtime
Helper Class
![Page 24: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/24.jpg)
What’s Missing? Abstraction!
No way to abstract away data access
Tight coupling
Leads to Big Ball of Mud system
Solution: Depend on interfaces, not
concrete implementations What should we call such
interfaces? Repositories!
User Interface
Database
Compile Time
Runtime
CoreIFooRepository
InfrastructureSqlFooRepository
![Page 25: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/25.jpg)
Repository
![Page 26: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/26.jpg)
Repository - Example
![Page 27: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/27.jpg)
Repository - Example
![Page 28: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/28.jpg)
Where do Repositories Live?
Place interfaces in Core Core includes Model classes and most business logic Core has no dependencies (ideally)
Place implementation in Infrastructure Infrastructure references Core
UI layer references both Core and Infrastructure Responsible for creating object graph, either manually or
via an IOC Container
![Page 29: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/29.jpg)
Proxy
![Page 30: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/30.jpg)
Proxy A class that controls access to another
Implemented via subclass or via delegation using a common interface
Frequent use cases: Remote Proxy for web services / network calls Lazy loading implementations Security / access control
![Page 31: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/31.jpg)
Proxy - Structure
![Page 32: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/32.jpg)
Adding Caching to a Repository
Caching is frequently added to query methods in repositories
Caching logic is a separate concern and should live in a separate class from the main data access logic
Proxy pattern provides a straightforward means to control access to the “real” data, versus the cache
![Page 33: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/33.jpg)
Proxy – CachedRepository Implementation
![Page 34: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/34.jpg)
Command
http://flic.kr/p/5Yb5i
![Page 35: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/35.jpg)
Command Represent an action as an
object
Decouple performing the action from the client that is issuing the command
Common scenarios: Delayed execution Logging activity Enabling Undo / Revoke
Transaction functionality
![Page 36: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/36.jpg)
Command
![Page 37: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/37.jpg)
Command : Usage Great for representing state transitions (the arrows
below)
Pending
PublishedArchived
![Page 38: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/38.jpg)
Factory
http://flic.kr/p/vGpC2
![Page 39: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/39.jpg)
Factory: Intent Separate object creation from the decision of which
object to create
Defer creation of objects Only create them if and when needed
Add new classes and functionality without breaking client code Only factory needs to know about new types available
Store possible objects to create outside of program Configuration Persistent Storage Plug-in assemblies
![Page 40: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/40.jpg)
Practice PDD Pain
Driven
Development
If it’s causing pain, fix it.
![Page 41: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/41.jpg)
CODE WALKTHROUGHDesign Patterns in Action
![Page 42: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/42.jpg)
REAL WORLDA Tale from the
![Page 43: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/43.jpg)
References Design Patterns, http://amzn.to/95q9ux Design Patterns Explained, http://amzn.to/cr8Vxb Design Patterns in C#, http://amzn.to/bqJgdU Head First Design Patterns, http://amzn.to/aA4RS6
Pluralsight Resources N-Tier Application Design in C# http://bit.ly/Msrwig Design Patterns Library http://bit.ly/vyPEgK SOLID Principles of OO Design http://bit.ly/OWF4la
My Blog http://ardalis.com
![Page 44: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/44.jpg)
Summary Design Patterns can be used during refactoring
or initial design to improve code quality
Become comfortable with a number of design patterns, but focus on the most common ones
Use design patterns to address pain in your design, such as tight coupling or excessive repetition
Design patterns can be combined in powerful ways, such as Strategy-Proxy-Repository
![Page 45: Common asp.net design patterns aspconf2012](https://reader036.fdocuments.net/reader036/viewer/2022062511/54c733694a7959d67c8b4616/html5/thumbnails/45.jpg)
Thanks!
• Find me at http://ardalis.com
• We’re hiring developers and trainers at Telerik!
• http://telerik.com/company/careers.aspx