0% found this document useful (0 votes)
55 views

Video How To: Creating An N-Tier Data Application

This document summarizes the steps to create a multi-tier data application using Visual Basic or C#, separated into presentation, middle, and data tiers. It demonstrates creating: 1) Data access and entity class library projects to represent the data and middle tiers 2) A Windows Communication Foundation service project to call into the data access tier 3) A Windows Forms application as the presentation tier to display data retrieved via the WCF service.

Uploaded by

mcguiver
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Video How To: Creating An N-Tier Data Application

This document summarizes the steps to create a multi-tier data application using Visual Basic or C#, separated into presentation, middle, and data tiers. It demonstrates creating: 1) Data access and entity class library projects to represent the data and middle tiers 2) A Windows Communication Foundation service project to call into the data access tier 3) A Windows Forms application as the presentation tier to display data retrieved via the WCF service.

Uploaded by

mcguiver
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Video How to: Creating an N-Tier Data Application

Transcript
I'm Harry Miller. I'm going to show you the basics of creating a multi-tier, or N-tier, data
application, using the Northwind sample database.
About N-Tier Data Applications
N-tier data applications are separated into multiple logical layers, or tiers, including a
presentation tier, a middle tier, and a data tier:
The presentation tier is the user interface.
The middle tier typically includes a data access layer, a business logic layer, and
shared components such as authentication and alidation.
The data tier is a relational database.
!ne way to separate the arious tiers of an n-tier application is to create a pro"ect for
each tier. I#ll create two class library pro"ects: one for the data-access tier, and one for the
data tier. $ater, I#ll add the presentation tier.
I'll demonstrate the three basic tas%s inoled:
&. 'reate the data-access and data pro"ects with a typed dataset split between them.
(. 'reate a )indows 'ommunication *oundation serice to call into the data access tier.
+. 'reate a )indows *orms application as the presentation tier.
Creating the Data Projects
I'll create a new pro"ect ,I#m using -isual .asic, but I#ll also show the '/ code0, select the
Windows folder, and clic% the Class Library template. I'll name the pro"ect
DataEntityTier, and the solution NTierWalkthrough, and clic% OK.
This is the data tier.
I'll add a new Class Library pro"ect to the NTierWalkthrough solution, name it
DataAccessTier, and clic% OK.
This is the data access tier, which contains the information that is re1uired to connect to
the database.
Creating the Typed Dataset
Now I'll create a typed dataset. I'll select DataAccessTier in Solution Eplorer, and
then open the Data menu and clic% Show Data Sources. In the Data Sources window,
I'll clic% Add New Data Source to start the Data Source Con!iguration Wi"ard.
I'll select Database and then clic% Net. I'll select the Northwind sample database. I'll
clic% Net to sae the connection string to the application configuration file.
Now I'll e2pand the Tables node and select the chec% bo2es for the Custo#ers and
Orders tables, and then clic% $inish.
Typed datasets hae a dataset class and a Table3dapter class in a single pro"ect. I want to
separate those classes into the two pro"ects I already created, to %eep the functions in the
correct tiers4typically, Table3dapters belong in the data access tier, and the dataset class
goes in the data tier. )hen you separate a dataset, the Table3dapter classes stay in the
pro"ect where you created the dataset, and the dataset class is moed to a pro"ect that
you specify.
l created the typed dataset in the DataAccessTier pro"ect, because I want the
Table3dapters to stay in that tier.
Separating the Typed Dataset
Now I'll separate the classes by setting the DataSet %ro&ect property to the name of the
pro"ect where I want to store the dataset class.
I'll double-clic% Northwind5ata6et.2sd in Solution Eplorer to open the dataset in the
5ataset 5esigner, and clic% an empty area on the designer to clear the focus.
In the %roperties window, I'll select the DataSet %ro&ect property, and clic%
DataEntityTier in the list.
Then on the 'uild menu, I'll clic% 'uild Solution.
The dataset and Table3dapters are separated into the two class library pro"ects. The
pro"ect that originally contained the whole dataset ,DataAccessTier0 now contains only
the Table3dapters. The pro"ect designated in the DataSet %ro&ect property
,DataEntityTier0 contains the typed dataset.
Creating a WCF Service
.ecause I want to demonstrate how to access the data access tier by using a )'* serice,
I'll create a new )'* serice application.
I'll add a new )'* pro"ect to the NTierWalkthrough solution4a WC$ Ser(ice Library.
I'll name the pro"ect DataSer(ice and clic% OK.
The data serice has to get the Northwind Custo#ers and Orders tables, so I'll create
two methods in the DataAccessTier pro"ect that return the tables: )etCusto#ers and
)etOrders.
In Solution Eplorer, I'll double-clic% Northwind5ataset.2sd to open the dataset in the
5ataset 5esigner. Then I can right-clic% 'ustomersTable3dapter and clic% Add *uery to
open the TableAdapter *uery Con!iguration Wi"ard.
I'll leae the default alue of +se S*L state#ents and clic% Net. I'll leae the default
alue of SELECT which returns rows, and clic% Net. I'll also leae the default 1uery
and clic% Net.
*or the method name, I'll type )etCusto#ers in the ,eturn a DataTable section, and
clic% $inish.
Now I need to return the Orders table, so I'll right-clic% !rdersTable3dapter, and then
clic% Add *uery. $i%e last time through wi7ard, I'll leae the default alues.
I#ll name the method )etOrders and clic% $inish.
Now I'll open the 'uild menu and clic% 'uild Solution.
Adding Reerences
The data serice re1uires information from the dataset and Table3dapters, so I need to
add references to the DataEntityTier and DataAccessTier pro"ects.
In Solution Eplorer, I'll right-clic% DataSer(ice and clic% Add ,e!erence. !n the
%ro&ects tab, I'll select the DataAccessTier and DataEntityTier pro"ects and clic% OK.
'/ pro"ects also need a reference to the 6ystem.5ata.5ata6et82tensions assembly. -isual
.asic pro"ects don't need this reference.
Adding Code to the Projects
Now that the data access tier contains the methods to return data, I can create methods
in the data serice to call those methods.
In the DataSer(ice pro"ect, I'll double-clic% I6erice&.b and add the following code
under the comment Add your ser(ice operations here.
[Visual Basic]
<OperationContract()> _
Function GetCustomers() As
DataEntityier!"ort#$in%Data&et!CustomersDataa'le
<OperationContract()> _
Function GetOr%ers() As DataEntityier!"ort#$in%Data&et!Or%ersDataa'le
In '/, the code loo%s li%e this.
[C(]
[OperationContract]
DataEntityier!"ort#$in%Data&et!CustomersDataa'le GetCustomers())
[OperationContract]
DataEntityier!"ort#$in%Data&et!Or%ersDataa'le GetOr%ers())
Now I'll double-clic% 6erice&.b and add the following code to the Ser(ice- class, and
then build the solution.
[Visual Basic]
*u'lic Function GetCustomers() As _
DataEntityier!"ort#$in%Data&et!CustomersDataa'le +mplements _
+&er,ice-!GetCustomers
Dim Customersa'leA%apter- As "e$ _
DataAccessier!"ort#$in%Data&eta'leA%apters!Customersa'leA%apter
.eturn Customersa'leA%apter-!GetCustomers()
En% Function
*u'lic Function GetOr%ers() As _
DataEntityier!"ort#$in%Data&et!Or%ersDataa'le +mplements _
+&er,ice-!GetOr%ers
Dim Or%ersa'leA%apter- As "e$ _
DataAccessier!"ort#$in%Data&eta'leA%apters!Or%ersa'leA%apter
.eturn Or%ersa'leA%apter-!GetOr%ers()
En% Function
In '/, the code loo%s li%e this.
[C(]
pu'lic DataEntityier!"ort#$in%Data&et!CustomersDataa'le GetCustomers()
/
DataAccessier!"ort#$in%Data&eta'leA%apters!Customersa'leA%apter
Customersa'leA%apter- 0 ne$ DataAccessier!
"ort#$in%Data&eta'leA%apters!Customersa'leA%apter())
return Customersa'leA%apter-!GetCustomers())
1
pu'lic DataEntityier!"ort#$in%Data&et!Or%ersDataa'le GetOr%ers()
/
DataAccessier!"ort#$in%Data&eta'leA%apters!Or%ersa'leA%apter
Or%ersa'leA%apter- 0 ne$ DataAccessier!
"ort#$in%Data&eta'leA%apters!Or%ersa'leA%apter())
return Or%ersa'leA%apter-!GetOr%ers())
1
Creating the Presentation Tier
Now the solution contains a data serice that has methods that call into the data access
tier, but I need a way to present the results to users. This is the presentation tier of the n-
tier application.
*or this demonstration I'll add a )indows *orms pro"ect to the solution, name it
%resentationTier, and clic% OK.
.ecause the presentation tier is the actual client application that is used to present and
interact with the data, I need to set the %resentationTier pro"ect to be the 6tartup
pro"ect. 6o I'll right-clic% it and then clic% Set as Start+p %ro&ect.
The client application %resentationTier re1uires a serice reference to the data serice to
access the methods in the serice. It also needs a reference to the dataset to enable type
sharing by the )'* serice.
To add the reference to the dataset I'll right-clic% the %resentationTier pro"ect and then
clic% Add ,e!erence. !n the %ro&ects tab, I'll clic% DataEntityTier and clic% OK.
To add a serice reference, I'll right-clic% %resentationTier and clic% Add Ser(ice
,e!erence. In the Add Ser(ice ,e!erence dialog bo2, I clic% Disco(er, then select
Ser(ice- and clic% OK.
The Data Sources window is automatically populated with the data that is returned by
the serice. I want to display the data on my )indows *orm, so I'll add two data-bound
5ata9rid-iews to the form.
In the Data Sources window, e2pand Northwind5ata6et and locate the Custo#ers node
and drag it onto *orm&. 3gain in the Data Sources window, I'll e2pand the Custo#ers
node and locate the related Orders node that's nested in the Custo#ers node, and drag
it onto *orm&.
Adding Code to Sho! the Data
)hen the form opens I want the data to show up immediately, so I'll create a
$or#-.Load eent handler by double-clic%ing an empty area of the form and add the
code.
[Visual Basic]
Dim Data&,c As "e$ &er,ice.e2erence-!&er,ice-Client
"ort#$in%Data&et!Customers!3er4e(Data&,c!GetCustomers)
"ort#$in%Data&et!Or%ers!3er4e(Data&,c!GetOr%ers)
In '/, the code loo%s li%e this.
[C(]
&er,ice.e2erence-!&er,ice-Client Data&,c 0
ne$ &er,ice.e2erence-!&er,ice-Client())
nort#$in%Data&et!Customers!3er4e(Data&,c!GetCustomers()))
nort#$in%Data&et!Or%ers!3er4e(Data&,c!GetOr%ers()))
The default ma2imum message si7e is not large enough to hold all the data. I'll change the
alue on the client, and this will automatically update the serice reference. I'll double-
clic% the app.config file in the %resentationTier pro"ect, find the attribute
#a,ecei(ed/essageSi"e, and "ust add a couple of 7eros on the end of the default
alue to increase it.
Running the Progra"
Now I can test the application by pressing *:. The data from the Custo#ers and Orders
tables is retrieed from the data serice and displayed on the form.
For #ore $nor"ation
;ou can get more information about accessing data in the -isual 6tudio Help. ;ou can find
other resources such as technical articles, samples, blogs, and ideos at the -isual 6tudio
5eeloper 'enter. <ust go to msdn.microsoft.com=studio.

You might also like