Sunteți pe pagina 1din 12

Fundamentals of N-Tier

By: Paul D. Sheriff

With Examples in VB.NET and C#

3rd Edition May 2006


Published By: PDSA, Inc.

Copyright 2006, PDSA, Inc.


All Rights Reserved Worldwide

Introduction

Written By:

Paul D. Sheriff

Technical Editors:

Paul D. Sheriff
Craig Shoemaker

Every effort has been made to supply complete and accurate information.
However, PDSA, Inc. assumes no responsibility for its use, nor for any
infringement of the intellectual property rights of third parties which would result
from such use.

Copyright 2006 PDSA, Inc.


All rights reserved worldwide. No part of this publication may be stored in a
retrieval system, transmitted, or reproduced in any way, including but not limited
to photocopy, photograph, magnetic or other record, without the prior
agreement and written permission of the publisher.

Printed in the United States of America

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

E-Book Payment Notice - PLEASE READ

E-Book Payment Notice - PLEASE READ


This e-book is sold on a per reader basis. This means that you have the right to
read this book as many times as you wish on any computer that you wish to
read it on. However, you may not share it with anyone else.
I have kept the cost of this e-book low in order to keep it within the budget of
most readers. By purchasing this e-book, and encouraging others to purchase
this e-book, I will be able to keep writing more of these.

Thank you for your support.

Sincerely,
Paul D. Sheriff

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Introduction

Table of Contents
E-Book Payment Notice - PLEASE READ............................................................ 3
Table of Contents.................................................................................................. 4
Introduction ........................................................................................................... 8
Goals of this Book ..................................................................................... 8
Benefits of Reading this Book ................................................................... 8
Prerequisites ............................................................................................. 9
Assumptions.............................................................................................. 9
Technical Support ................................................................................................. 9
Installation ........................................................................................................... 10
If you have Problems Installing the Sample ........................................................ 10
About Paul D. Sheriff........................................................................................... 11
Chapter 1 ....................................................................................................................... 1-1
Overview of N-Tier Architecture..................................................................................... 1-1
What is N-Tier? .................................................................................................. 1-1
Services.................................................................................................. 1-2
A Tiered Approach to Development ................................................................... 1-4
Implementation of N-Tier.................................................................................... 1-4
Why N-Tier is a good choice .................................................................. 1-5
Why use Data Access Classes .............................................................. 1-5
Advantages of Separating Services ....................................................... 1-6
Disadvantages of Separating Services .................................................. 1-7
Reusable Components....................................................................................... 1-7
How to Achieve N-Tier ....................................................................................... 1-8
Map a Database Object to a Class......................................................... 1-9
The Data Layer Component ................................................................. 1-10
The Data Classes................................................................................. 1-10
The Business Rules Component.......................................................... 1-11
Goals of Data Classes.......................................................................... 1-12
Goals of Business Classes................................................................... 1-12
Benefits of Data Classes ...................................................................... 1-12
Summary.......................................................................................................... 1-14
Chapter 2 ....................................................................................................................... 2-1
Creating N-Tier Services................................................................................................ 2-1
Sample 1 Two-Tier Sample.............................................................................. 2-2
List Box Click Event and FormShow() .................................................... 2-3
Sample 2 Refactored Form Methods .............................................................. 2-5

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Table of Contents
List Box Click Event and FormShow() .................................................... 2-7
Sample 3 Product Data Class ......................................................................... 2-9
List Box Click Event and FormShow() .................................................. 2-10
The Products Class (Version 1) ........................................................... 2-11
Sample 4 AppConfig Class ........................................................................... 2-13
App.Config File..................................................................................... 2-13
Products Class (Version 2)................................................................... 2-14
Sample 5 DataLayer Class ........................................................................... 2-16
Products Class (Version 3)................................................................... 2-18
Creating Reuseable Components .................................................................... 2-19
ConfigCommon .................................................................................... 2-22
DataCommon ....................................................................................... 2-22
NTierData ............................................................................................. 2-22
NTierSample1a .................................................................................... 2-22
Summary.......................................................................................................... 2-23
Chapter 3 ....................................................................................................................... 3-1
The DataLayer Component............................................................................................ 3-1
Methods of the DataLayer Class........................................................................ 3-2
Overview ................................................................................................ 3-2
Using the ADO.NET Interfaces .......................................................................... 3-3
DataProvider Class ............................................................................................ 3-6
CreateConnection Method ..................................................................... 3-6
CreateCommand Method ....................................................................... 3-6
CreateParameter Method....................................................................... 3-7
CreateDataAdapter Method ................................................................... 3-7
DataLayer.CreateConnection Method................................................................ 3-8
DataLayer.CreateCommand Method ................................................................. 3-9
CreateCommand(SQL) .......................................................................... 3-9
CreateCommand(SQL, ConnectString, OpenConnection)................... 3-10
CreateCommand(SQL, ConnectString)................................................ 3-11
DataLayer.CreateParameter Method ............................................................... 3-12
CreateParameter(ParameterName) ..................................................... 3-12
CreateParameter(ParameterName, DataType) ................................... 3-13
CreateParameter(ParameterName, DataType, Value) ........................ 3-14
DataLayer.CreateDataAdapter Method............................................................ 3-15
GetDataSet Method ......................................................................................... 3-16
Usage ................................................................................................... 3-17
GetDataTable Method...................................................................................... 3-18
Usage ................................................................................................... 3-19

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Introduction
GetDataReader ................................................................................................ 3-20
Usage ................................................................................................... 3-22
ExecuteScalar .................................................................................................. 3-24
Usage ................................................................................................... 3-26
ExecuteSQL Method ........................................................................................ 3-27
ExecuteSQL(cmd, DisposeOfCommand)............................................. 3-27
Usage ................................................................................................... 3-29
ExecuteSQL(cmd) ................................................................................ 3-31
Usage ................................................................................................... 3-32
Usage with Parameters ........................................................................ 3-34
ExecuteSQL(SQL, ConnectString)....................................................... 3-37
Usage ................................................................................................... 3-38
Summary.......................................................................................................... 3-40
Chapter 4 ....................................................................................................................... 4-1
Data Classes.................................................................................................................. 4-1
Standard Properties of the Data Classes........................................................... 4-2
Methods of the Data Classes ............................................................................. 4-3
Examples of Usage ................................................................................ 4-4
Match Columns to Properties............................................................................. 4-6
Connection String Property .................................................................... 4-9
Schema Structure .............................................................................................. 4-9
Usage of the Schema Structure ........................................................... 4-10
Data Retrieval Methods.................................................................................... 4-11
Returning All Rows............................................................................... 4-11
Returning One Row by Primary Key .................................................... 4-12
Load Method ........................................................................................ 4-13
Data Modification Methods............................................................................... 4-15
Insert Method ....................................................................................... 4-15
FillInParameters Method ...................................................................... 4-16
Update Method..................................................................................... 4-17
Delete Method ...................................................................................... 4-18
Validate Method ............................................................................................... 4-19
BusinessRuleException Class ......................................................................... 4-21
Multiple Tables ................................................................................................. 4-23
Multi-Table Joins .................................................................................. 4-23
Multi-Table Updates ............................................................................. 4-23
Summary.......................................................................................................... 4-25
Chapter 5 ....................................................................................................................... 5-1
Business Classes........................................................................................................... 5-1

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Table of Contents
Creating a Business Class ................................................................................. 5-1
Validating Data................................................................................................... 5-2
Summary............................................................................................................ 5-6
Chapter 6 ....................................................................................................................... 6-1
Alternate N-Tier Implementation .................................................................................... 6-1
ProductsState Class........................................................................................... 6-1
ProductsDC Class.............................................................................................. 6-4
Products Class ................................................................................................... 6-7
Summary.......................................................................................................... 6-10
Chapter 7 ....................................................................................................................... 7-1
N-Tier and Web Services............................................................................................... 7-1
Build the Web Service........................................................................................ 7-1
Web Service....................................................................................................... 7-4
Consuming the Web Service.............................................................................. 7-7
Loading the Products List Box................................................................ 7-8
Showing the Form Data.......................................................................... 7-9
Inserting Data ....................................................................................... 7-11
Summary.......................................................................................................... 7-14

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Introduction

Introduction
This book is designed for anyone who wants to learn how to create N-Tier
applications using the .NET Framework. N-Tier applications should be used for
all business applications. With the advent of .NET, creating N-Tier applications
is much easier since the amount of code you need to write in each data class is
greatly reduced.
The purpose of this book is to show readers how to create N-Tier applications
to build real-world business applications. This book emphasizes good
programming standards and practices.

Goals of this Book


By the end of reading this book you will have learned several very useful
techniques that will allow you to build upon the base set of classes in this book
in your own N-Tier applications. In this book you will learn the following:

Why you want to use N-Tier development techniques

How to create a class that wraps up ADO.NET using a provider


pattern

How to create data access classes

How to build business rule classes

How to consume business rule classes from a front end application

How to create a Web Service and Smart Client Application

Benefits of Reading this Book


There are two major benefits you will derive from reading this book. First, you
will learn to create N-Tier business applications. Secondly you will learn many
good Object Oriented Programming (OOP) practices. It is impossible to create
N-Tier applications without learning about inheritance, polymorphism,
overloading, and overriding methods. These are all valid and great OOP
techniques.
This book will save you hours of time because it already includes the following:

Data Layer Class


Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Technical Support

Sample Data Access Classes

Sample Business Rule Classes

Sample Web Service

Complete Sample Applications

If you started from scratch it would probably take you over 40 hours of time to
develop all the classes that are included in this book!

Prerequisites
This book is designed for programmers who are already experienced with
VB.NET, C#, ADO.NET, relational databases, and basic Object Oriented
Programming. You should have already created at least one Windows Form
application in .NET. You should also be familiar with relational database
concepts. You must be familiar with Windows 2000 or later, and have access to
Windows 2000 or Windows XP to effectively use this book.

Assumptions
You will need several tools at your disposal so you can try out the many
exercises contained in this book. Below is a list of the tools that you should
have on your computer:

Microsoft SQL Server 2000 or later

Windows 2000 Professional or higher, Windows 2003, or Windows


XP.

Microsoft .NET 2.0 CLR Framework SDK

Microsoft Visual Studio .NET 2005

Technical Support
Because of the complexity of .NET software development, PDSA, Inc. does not
provide free technical support related to the use of the material in this eBook.
Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

Introduction
Technical support is available for problems with installation of the samples that
are a part of this eBook but only through email, please do not call.
Tip:

It is strongly recommended that you read this book


thoroughly before emailing us for technical support.

Installation
This eBook comes with an MSI file that you downloaded from the web site. This
MSI file will install all of the samples for this e-book.

If you have Problems Installing the Sample


Below is a list of suggestions to ensure your installation of the sample files for
the book is successful. Please read these carefully and follow each one for best
results.
1.

You must be running a supported ASP.NET operating system. The only


ones you can use are Windows 2000 Professional with IIS installed,
Windows 2000 Server or Advanced Server, Windows XP Professional or
Windows Server 2003.

2.

You must be running the final version of .NET 2.0 not a Beta version.
a. Start Visual Studio .NET.
b. Choose Help | About Microsoft Development Environment.
c. You should have the following Version numbers or later:
i. Microsoft Development Environment 2005 Version:
8.0.50727.42.
ii. Microsoft .NET Framework Version 2.0.50727.

3.
10

You must have administrator rights for installation.


Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

About Paul D. Sheriff


4.

IIS must be installed and running.

5.

Ensure that you can create an ASP.NET application using VS.NET.

6.

Shut down VS.NET prior to doing the installation.

About Paul D. Sheriff


Paul is the CEO and President of PDSA, Inc., a Microsoft Partner, in Southern
California. PDSA, Inc. has provided customers with high quality custom
software, training, and architecture services since 1991.
Paul has been using .NET since Beta 1 and has written over 40 articles and 12
days of courseware on .NET. He has created many videos on .NET
programming and performed over 40 Webcasts for Microsoft on .NET
technology. Paul has lectured at various conferences on .NET, including the
launch of VS.NET at VSLive and Microsoft Tech Ed 2003. Paul is the Microsoft
Regional Director for Southern California and helps run the local Developer
Days event for Microsoft as well as speaking at various local Microsoft events.
Paul has over twenty years experience architecting information systems and his
expertise is in much demand from companies such as Microsoft, TransAmerica,
Rockwell, American Express, J.D. Powers & Associates and many others.
Lately Paul has been helping these companies architect .NET applications and
helping them lay out application frameworks such as the one presented in the
eBook Architecting ASP.NET Applications. This eBook may also be
purchased at www.pdsa.com/ebooks.
Over the years Paul has written over 150 articles for many different
publications. Paul is the author of the QUE book Paul Sheriff teaches Visual
Basic 6.0 and has co-authored the Addison-Wesley book ASP.NET Developer's
Jumpstart with Ken Getz. He has authored over 7 eBooks as well on .NET and
SharePoint technologies. Paul speaks at many different conferences around the
world including Microsoft Tech-Ed, Advisor Live, VSLive, MSDN Events and
Microsoft Developer Days.
PDSA, Inc. is available for consulting work and on-site training in Microsoft
.NET, SharePoint, C#, Visual Basic, SQL Server and Architecture. Contact
PDSA, Inc. toll-free at (888) 899-PDSA (7372), or (714) 734-9792. Fax (714)
734-9793. E-mail: Psheriff@pdsa.com. VISIT THE PDSA WEB SITE at:
http://www.pdsa.com. See the Appendix for more information on PDSA, Inc.

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

11

Introduction

THIS PAGE INTENTIONALLY LEFT BLANK

12

Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.

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