Documente Academic
Documente Profesional
Documente Cultură
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.
Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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.
Technical Support
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:
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:
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.
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
5.
6.
Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
11
Introduction
12
Fundamentals of N-Tier
Copyright 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.