Getting Started 3.5
Getting Started 3.5
Getting Started
by
Page 1 of 14
Design Pattern Framework™ 3.5
1. Index
1. Index ....................................................................................................................... 2
2. Introduction.............................................................................................................. 3
Copyright and License ......................................................................................... 3
3. Setup ....................................................................................................................... 4
Versions of Visual Studio and .NET ..................................................................... 4
Installation ........................................................................................................... 4
1: Gang of Four ........................................................................................ 5
2: Head First ............................................................................................ 5
3: Patterns in Action ................................................................................. 5
4: Visio UML Diagrams............................................................................. 6
4. Database Setup ....................................................................................................... 7
5. Learning about patterns ........................................................................................... 8
What are Design Patterns .................................................................................... 8
Exploring Design Patterns ................................................................................... 9
Step A: Gang of Four .............................................................................. 9
Step B: Head First ................................................................................. 10
UML Diagrams: ...................................................................................... 10
Applying Design Patterns................................................................................... 10
Step 1: Functionality ............................................................................... 11
Step 2: Overview Level .......................................................................... 12
Step 3: Detail Level ................................................................................ 13
Step 4: Data Flow................................................................................... 13
Step 5: GoF Design Patterns.................................................................. 14
Step 6: Modern Design Patterns ............................................................ 14
6. Summary ............................................................................................................... 14
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 2 of 14
Design Pattern Framework™ 3.5
2. Introduction
Welcome to the Design Pattern Framework 3.5, a unique package that will help you join
the ranks of expert .NET developers and architects.
This document will show you how to get started with design patterns using Design
Pattern Framework 3.5. It discusses setup and installation, followed by a section on
how to optimize your learning experience with this extensive resource. But first, we ask
that you review our simple copyright and license notice.
Copyright (C) 2006-2009 Data & Object Factory, LLC. All rights reserved.
If this is a single user license the product can only be used by a single user. If you
purchased the 16-user license then the product can be used by up to 16 developers
concurrently. Site licenses are restricted to a single facility at a given physical
address.
You are free to use the source code in your own business application development,
but remember that this product and the source code are intended for educational
purposes only.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 3 of 14
Design Pattern Framework™ 3.5
3. Setup
The Design Pattern Framework 3.5 requires Visual Studio 2008 and .NET 3.5. Please
note that Visual Studio 2008 Express Edition (the free version) is not supported. It is
also necessary that you have SQL Server Express installed.
Installation
Run the ‘Design Pattern Framework 3.5 **.msi’ installer program (where ** = CS for the
C# edition, and VB for the VB.NET edition). This will install the complete framework with
all necessary files. We strongly suggest that you follow the suggested folders in the
installation program. They are:
C# Edition:
C:\Program Files\DoFactory\Design Pattern Framework 3.5 CS\*.*
VB Edition
C:\Program Files\DoFactory\Design Pattern Framework 3.5 VB\*.*
Also installed are these Program menu items for easy access.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 4 of 14
Design Pattern Framework™ 3.5
Following installation, you will have 4 subfolders and 3 pdf e-books in these folders.
They are:
1: Gang of Four
This folder contains 69 Gang-of-Four design pattern projects, i.e. Structural, Real world,
and .NET optimized. In addition to the menu, you can also create a shortcut on your
desktop to the solution file: ‘DoFactory.GangOfFour.sln’. Double-click and you're ready
to go.
2: Head First
The folder contains 46 Head First design pattern projects. These are .NET versions of
the Java based examples from the Head First Design Patterns book. In addition to the
menu, you can also create a shortcut to the solution file ‘DoFactory.HeadFirst.sln’ on
your desktop. Double-click and you're ready to go.
3: Patterns in Action
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 5 of 14
Design Pattern Framework™ 3.5
In addition to the menu item you can create a shortcut to solution ‘Patterns In Action.sln’
on your desktop. Double-click and you're ready to go. The default startup project is the
ASPNET Web Application, but you can change this to the Windows Forms Application or
the WPF Application as well.
‘Patterns in Action’ uses the Microsoft ASP.NET 3.5 built-in web server (code named
Cassini). When running you will see 3 ASP.NET Development Servers popup. If you
want to use IIS instead, you will need to create 3 new IIS virtual directories, one for the
web application, and two for the web services. Also, adjust the WCF configuration and
project Start Options accordingly.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 6 of 14
Design Pattern Framework™ 3.5
4. Database Setup
The ‘Patterns in Action’ application is configured to run against a local SQL Express
database named Action.mdf. The database files are located under project
‘Host.WCF.ActionServer’ in the \App_Data\ folder.
You can change to SQL Server by creating a new SQL Server database (for example,
named Action) and run the action.sql script (located in a folder named \Solution
Items\SQL Server\). Then, in web.config (in project ‘Host.WCF.ActionServer’) under
<appSettings> change the ConnectionStringName to ADO.NET.SqlServer. The
application will now run against SQL Server.
Alternatively, you can use MS Access by changing the same web.config as follows: set
the DataProvider to System.Data.OleDb and set the ConnectionStringName to
ADO.NET.Access.
Note: the Membership database is configured to always use SQL Express. You can
change this to SQL Server, but not MS Access. Because MS Access is not available for
Membership, you will always need access to either SQL Express or SQL Server to run
the ‘Patterns in Action’ reference application.
With SQL Server and SQL Express, you have the additional option to change data
access from ADO.NET to LINQ-to-SQL. For details consult the ‘Patterns In Action
3.5.pdf’ document.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 7 of 14
Design Pattern Framework™ 3.5
Design patterns are advanced object-oriented techniques. To get the most of this
framework it is best that you are familiar with object-oriented programming in C# or
VB.NET.
As a first step you will need to understand what design patterns are and why they exist.
On our website we explain it as follows:
Figuratively speaking, what this means is that developers do not want to keep re-
inventing the wheel for problems that are common and re-occurring. So, they describe
the ‘wheel pattern’ that will solve all problems that require the ‘ability to move over the
ground’. Clearly, there are different types of wheels (for cars, bikes, trains, etc), but they
all have in common that the solution requires a device that is circular with an axis in the
middle (this is the pattern).
It takes time and experience before you see or ‘discover’ patterns. But once they are
proven and documented, other developers can start benefitting because they don’t have
to go through the same trial-and-error process. This greatly accelerates productivity of
.NET developers. Another benefit is that their code will become more effective, more
robust, and more elegant.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 8 of 14
Design Pattern Framework™ 3.5
You’ll find plenty of information on design patterns in books and on the Internet (wikis,
forums, etc). However, what is missing are pattern examples in the context of real-world
applications, that is, when, where, and how do you use design pattern. Most
examples are simple and are not representative of the real world. This makes it hard for
.NET developers to learn about design patterns and use them in their own projects. The
Design Pattern Framework 3.5 is designed to address this very problem. Below are two
possible steps you can take to start exploring and learning about design patterns.
The 23 Gang of Four (GoF) design patterns are considered the foundation of all
patterns. They are called Gang of Four because they were first published in a book,
titled Design Patterns, written by 4 co-authors. If you are new to design patterns, we
recommend you start with these. The pattern solution with 69 projects is located under
folder \Gang Of Four\ and is described in file ‘Gang of Four Design Patterns 3.5.pdf’.
Each of the GoF design patterns is presented in 3 forms: structural, real-world and .NET
optimized. The structural form follows the original, somewhat abstract, definition with
original class names. The real-world follows the same structure, but is applied to an
easy to understand real-world problem. Finally, the .NET optimized form solves the
same problem but exploits the latest, most effective programming techniques available
in .NET 3.5.
We suggest you start with the most commonly used patterns. In ‘Gang of Four Design
Patterns 3.5.pdf’ you will find for each of the 23 patterns their frequency-of-use. It is best
to start with the most frequently used ones (medium high and high), such as, Factory,
Singleton, Proxy, Observer, etc. In this same document we also explain when and
where each pattern is used and how Microsoft uses the pattern in their .NET Framework
libraries.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 9 of 14
Design Pattern Framework™ 3.5
Alternatively, you could start by exploring the ‘Head First’ design pattern path. These
patterns are based on a book titled Head First Design Patterns. Many developers really
enjoy this book. It explains design patterns in a light-hearted, easy-to-understand
manner. In fact, it describes the same set of 23 GoF patterns as discussed in Step A,
but it is written for Java developers and all code examples are in Java.
As part of the Design Pattern Framework you have received .NET versions of these
Head First Java examples. This will allow you to read about the patterns from the book
while referencing our .NET Head First design pattern code examples. This combination
of an easy-to-read book with .NET code samples will get you up to speed quickly with
design patterns. The .NET Head First design patterns are located under folder \Head
First\ and associated documentation is found in ‘Head First Design Patterns 35.pdf’.
UML Diagrams:
To support the study of GoF patterns (both Step A and Step B), you will find visual
representations of the pattern classes in a UML class diagram for each of these 23
patterns in the \Visio UML Diagrams\ folder.
Once you’re familiar with the concept of design patterns and have explored the more
commonly used GoF design patterns, we suggest you move on to the ‘Patterns in
Action’ reference application. This comprehensive e-commerce application
demonstrates when, where, and how design patterns are used in today’s modern 3-
tier architectures. ‘Patterns in Action’ demonstrates modern pattern architectures for an
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 10 of 14
Design Pattern Framework™ 3.5
entire application -- from top to bottom. The .NET solution is located under folder
\Patterns in Action\. The associated documentation is called ‘Pattern in Action 3.5 pdf’.
Begin by exploring the functionality of the application, that is, navigate through the
application and get a feel of what the user can do. After that, you need to explore the
the high level organization of the solution and projects including the different layers and
their relationships. Next, you should dig a little deeper and start learning about the
individual projects and the roles they play, such as, Log, DataObjects, BusinessObject,
etc. After you are somewhat familiar with the different projects, you need to understand
the data flow, that is, how does the data from the database get all the way to the UI and
back again. The debugger is of great help in tracing the data. By the time you
understand the data flow you will have a pretty good understanding of the overall 3-tier
architecture. With that, you are ready to explore the individual GoF patterns that are
scattered throughout the application, such as Proxy, Factory, Singleton, Strategy, and
Observer. Try to understand what the purpose is of these patterns and how they do it.
Finally, you can explore the numerous newer patterns, including the Enterprise patterns,
the SOA and Messaging Patterns, as well as the Model-View design patterns.
Step 1: Functionality
This is where you find out what the application actually does. ‘Patterns in Action’ is an
e-commerce application that allows users to shop for electronic products online. It is best
to start with the ASPNET Web Application. Ensure that the project is set to be the
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 11 of 14
Design Pattern Framework™ 3.5
startup project (the default setting). Start browsing, searching, and sorting products.
View product details and add these to the shopping cart. Add several products with
different quantities to the cart. Change the cart by changing quantities (and recalculate),
removing items, and setting different shipping options.
The shopping experience is in a public area. The administration area is secure and
requires the user to login. Login and then browse, add, edit, delete customers. View
customer orders and order details. Once you’re done you can logout.
Next, change the startup project to Windows Forms Application. Run the application and
login. Select different customers on the left-hand tree control and see that the orders and
order details adjust accordingly. Add a new customer, and edit, delete customers. When
you are done you can logout and exit.
Then, change the startup application to WPF Application. Login and manipulate
customers by hovering over, double clicking, and right clicking on the customer images.
Right click will allow you to add, edit, and delete customers. Double clicking on a
customer will open the order and order details window. Once completed, you can logout
and exit.
It is very important that you know that the three UI platforms consume the exact same
WCF service layer (the Cloud Façade pattern). You may find that the applications are
running a bit slow. The reason for this is the WCF Hosting configuration (via HTTP). To
keep the configuration simple, we opted to have all three applications use the same
protocol, but in a real-world production environment you will most likely configure these
differently (ideally using IIS7).
This is where you learn about the organization of the solution which consists of 21
different projects and 5 different layers (although the architecture is still referred to as 3-
tier). The layers are arranged in folders that have numbers so that they display in a
logical order, that is, from top to bottom. The presentation tier (Layer 1) contains 3
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 12 of 14
Design Pattern Framework™ 3.5
different UI platforms: ASP.NET, WinForms and WPF. The service tier is represented by
the WCF Hosting and WCF Services (Layers 2 and 3). There are two different services:
ActionService (application services) and ImageService (for presenting customer
images). The service layer typically handles data validation, authentication,
authorization, and transactions.
The next two layers are Business Layer (Layer 4) and Data Layer (Layer 5). The
business layer contains business objects with business rules. The data layer performs
database access and contains implementations of ADO.NET and LINQ-to-SQL. Both the
database and the data access methodologies are configurable in the web.config file
under project ‘Host.WCF.ActionServer’
Here you focus on the individual projects. It is best to start at the bottom and then move
up until you reach the UI layer. Start with DataObjects, then BusinessObjects, then
Action Service, then Host.WCF.ActionServer, and finally one of the presentation tier
platforms. The references folder in each project shows you the dependencies on other
projects. The ‘Patterns in Action 3.5 pdf’ guide describes each project in more detail.
By this time you should have a reasonable understanding of the data flow, that is how
does the application move the data from the database, via the different layers to the UI
layer and then back to the database. It is important that you understand the different
data container types that hold the data as it travels through the layers. These containers
include: Entities, Business Objects, Data Transfer Objects, and Business Model Objects.
Remember, that in your own projects it may not be necessary to use all container types
presented in this reference application; perhaps a simple DataTable is all you need. As
mentioned before, the debugger is a great tool to follow the data as it travels through the
different layers.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 13 of 14
Design Pattern Framework™ 3.5
Many of the Gang of Four design patterns are located in the Data Layer and Framework
projects. It is suggested that you identify these (browse the code and explore the code
comments), study the source code, and with your prior understanding (based on Step A
or B) you will be getting a good handle on when, where, and why they are used.
The newer, more recently, discovered design patterns are located throughout the
application. The Enterprise Patterns are generally located where the GoF are, that is, in
the Data Layer and Framework projects. The SOA and Message patterns are located in
the WCF service layer. The Model-View patterns are located in the presentation layer:
MVC in ASP.NET application, MVP in the WinForms application, and MVVM in the WPF
application. The MVVM is probably the last one to study as it is the most complex
patterns and requires that you are familiar with WPF commands and data binding.
6. Summary
As you can see, the Design Pattern Framework 3.5 is a unique and comprehensive
package that captures and presents design patterns in a real-world setting with a .NET
reference application and 3-tier application architecture. We are hopeful that you will be
able to apply many of these patterns and practices in your own projects and build .NET
applications that are robust, easy-to-maintain, elegant, and, above all, successful.
Copyright © 2005-2009, Data & Object Factory, LLC. All rights reserved. Page 14 of 14