SlideShare a Scribd company logo
1 of 12
Download to read offline
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
2 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
E-Book Payment Notice - PLEASE READ
Fundamentals of N-Tier 3
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Introduction
4 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Table of Contents
Fundamentals of N-Tier 5
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Introduction
6 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Table of Contents
Fundamentals of N-Tier 7
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Introduction
8 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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
Technical Support
Fundamentals of N-Tier 9
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
• 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.
Introduction
10 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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. You must have administrator rights for installation.
About Paul D. Sheriff
Fundamentals of N-Tier 11
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
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.
Introduction
12 Fundamentals of N-Tier
Copyright © 2006 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
THIS PAGE INTENTIONALLY LEFT BLANK

More Related Content

Similar to Funamentals of N-Tier

Pro Bp Coffee Shop Business Plan
Pro Bp Coffee Shop Business PlanPro Bp Coffee Shop Business Plan
Pro Bp Coffee Shop Business Planguest645d32
 
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationHP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationProtect724gopi
 
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationHP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationProtect724gopi
 
SAP BusinessObjects Installation Guide
SAP BusinessObjects Installation GuideSAP BusinessObjects Installation Guide
SAP BusinessObjects Installation GuideProtect724gopi
 
HP EnterpriseView v1.5 SAP BusinessObjects Installation Guide
HP EnterpriseView v1.5 SAP BusinessObjects Installation GuideHP EnterpriseView v1.5 SAP BusinessObjects Installation Guide
HP EnterpriseView v1.5 SAP BusinessObjects Installation GuideProtect724gopi
 
Search Engine Optimization Guide For Bloggers
Search Engine Optimization Guide For BloggersSearch Engine Optimization Guide For Bloggers
Search Engine Optimization Guide For BloggersElizabeth439Boggan
 
Solution.manual elementary.surveying-13edition
Solution.manual elementary.surveying-13editionSolution.manual elementary.surveying-13edition
Solution.manual elementary.surveying-13editionAwais Ahmad
 
Installing and conf guide for hp sm connector
Installing and conf guide for hp sm connectorInstalling and conf guide for hp sm connector
Installing and conf guide for hp sm connectorTheEnferRimbaud
 
Informatica installation guide
Informatica installation guideInformatica installation guide
Informatica installation guidecbosepandian
 
Saptableref[1]
Saptableref[1]Saptableref[1]
Saptableref[1]mpeepms
 
Pro Bp Coffee Shop Business Plan doc
Pro Bp Coffee Shop Business Plan docPro Bp Coffee Shop Business Plan doc
Pro Bp Coffee Shop Business Plan docguest645d32
 
Terminos condicionesgoldbex en
Terminos condicionesgoldbex enTerminos condicionesgoldbex en
Terminos condicionesgoldbex enalberto mariani
 
Dotcomology Science of Online Marketing
Dotcomology Science of Online Marketing Dotcomology Science of Online Marketing
Dotcomology Science of Online Marketing bob SPIRO
 
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206Dennis Reurings
 

Similar to Funamentals of N-Tier (20)

Pro Bp Coffee Shop Business Plan
Pro Bp Coffee Shop Business PlanPro Bp Coffee Shop Business Plan
Pro Bp Coffee Shop Business Plan
 
Mail chimp for-designers
Mail chimp for-designersMail chimp for-designers
Mail chimp for-designers
 
Party merge
Party mergeParty merge
Party merge
 
Sap sd tutorial
Sap sd tutorialSap sd tutorial
Sap sd tutorial
 
Sap sd tutorial
Sap sd tutorialSap sd tutorial
Sap sd tutorial
 
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationHP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
 
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects InstallationHP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
HP ArcSight EnterpriseView v1.6 SAP BusinessObjects Installation
 
SAP BusinessObjects Installation Guide
SAP BusinessObjects Installation GuideSAP BusinessObjects Installation Guide
SAP BusinessObjects Installation Guide
 
HP EnterpriseView v1.5 SAP BusinessObjects Installation Guide
HP EnterpriseView v1.5 SAP BusinessObjects Installation GuideHP EnterpriseView v1.5 SAP BusinessObjects Installation Guide
HP EnterpriseView v1.5 SAP BusinessObjects Installation Guide
 
Crisp dm
Crisp dmCrisp dm
Crisp dm
 
Search Engine Optimization Guide For Bloggers
Search Engine Optimization Guide For BloggersSearch Engine Optimization Guide For Bloggers
Search Engine Optimization Guide For Bloggers
 
Solution.manual elementary.surveying-13edition
Solution.manual elementary.surveying-13editionSolution.manual elementary.surveying-13edition
Solution.manual elementary.surveying-13edition
 
Installing and conf guide for hp sm connector
Installing and conf guide for hp sm connectorInstalling and conf guide for hp sm connector
Installing and conf guide for hp sm connector
 
BI & DW Documentation
BI & DW DocumentationBI & DW Documentation
BI & DW Documentation
 
Informatica installation guide
Informatica installation guideInformatica installation guide
Informatica installation guide
 
Saptableref[1]
Saptableref[1]Saptableref[1]
Saptableref[1]
 
Pro Bp Coffee Shop Business Plan doc
Pro Bp Coffee Shop Business Plan docPro Bp Coffee Shop Business Plan doc
Pro Bp Coffee Shop Business Plan doc
 
Terminos condicionesgoldbex en
Terminos condicionesgoldbex enTerminos condicionesgoldbex en
Terminos condicionesgoldbex en
 
Dotcomology Science of Online Marketing
Dotcomology Science of Online Marketing Dotcomology Science of Online Marketing
Dotcomology Science of Online Marketing
 
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206
Youwe sap-ecc-r3-hana-e commerce-with-magento-mb2b-100717-1601-206
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

Funamentals of N-Tier

  • 1. 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
  • 2. Introduction 2 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 3. E-Book Payment Notice - PLEASE READ Fundamentals of N-Tier 3 Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 4. Introduction 4 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 5. Table of Contents Fundamentals of N-Tier 5 Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 6. Introduction 6 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 7. Table of Contents Fundamentals of N-Tier 7 Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 8. Introduction 8 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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
  • 9. Technical Support Fundamentals of N-Tier 9 Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. • 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.
  • 10. Introduction 10 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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. You must have administrator rights for installation.
  • 11. About Paul D. Sheriff Fundamentals of N-Tier 11 Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. 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.
  • 12. Introduction 12 Fundamentals of N-Tier Copyright © 2006 by PDSA, Inc. All rights reserved. Reproduction is strictly prohibited. THIS PAGE INTENTIONALLY LEFT BLANK