Skip to main content
Email Password   helpLost your password?

Download My_Article_Example.zip - 214.0 KB

Introduction

Many People googled for an Article or a Tutorial that shows how to create N-Tier Applications step by Step in VB.NET and for Windows Application.But never found one. In this Article i will show you Step by Step on how to do it.i made an example from one of the Small Projects in did in N-Tier. i have included ways to reaquest Data pass Data between Tiers. This is a Good Practise and its Simple to mantain your code if its in tiers. Imagine having your Business Layer, Data Layer in the Presentation Layer(your VB form,ASP page). Your Code will be a Spageti code that is time consuming when it has to be maintined. Before we start coding, lets Explain what is N-Tier and why is it used.

Background

N-Tier is a Client-Server Architecture in which User interface (Presentation Layer), Business Rules(Business Logic Layer), Data Access(Data Layer) are separated in Layers, maintained and Developed Indipendently. The Applicatin will be broken into Tiers. Presentation Layer will be your Form or ASP.NEt page, Busines Layer will be the Classes that will Check if the Business Rules have not been Violeted, and the Data Access layer will Accept the Validated Data from the Business Logic layer and run the SQl commands on the Actual Database.

When reading this Article, i assume that you know how -

Getting Started

Make sure that your table is in Place and and it has a Primary key. because if you are using an Adapter, you cannot not update a table that has no Primary key, you will only select.

Step1(Adding PL and BLL)

In the First Step we are going create a Solution. you can call it anything meaningfull, in my case i called it "Pro_Client". This will be will be your Presentation Layer, that is where the user will interact with the Actual data that we are going to Consume later in the Article.

Step 2(Adding PL and BLL)

In the Solution Explorer, select your Solution "Prop_Client" and right Click, in the menu select "Add" and "New Project." An Addnew Project Dialogbox will appear and you must select "Class Library" because we are going to use this Project as our BLL(Business Logic Layer) and name it "Prop_BLL" or something meaningfull that will show you that its a BLL. rename the "Class1" into "clsPropertyBLL", or something else meaningfull. and Double click on the renamed class or view the code. you will Notice that the "Class1" is still there. Change it to something meaning fulll, if you are working with Employees use "Employees" in my case "Properties". Then you are done with you BLL, we will visit it Later again.

Step 3(Adding DAL to a Solution)

In this Step, we are going to Add our DAL in the same Solution. Select your Solution "Prop_Client" and right Click, in the menu select "Add" and "New Project." An Addnew Project Dialogbox will appear and you must select "Class Library" and name it "PropDAL", or something meaningfull to show you that this is your DAL. After you have done that, Delete "Class1" from your Dal project we will not use it. The next thing is to Add a Component, for a Beginner you will not use a Class directly, i want you to drag and drop some adapters from the Toolbox. Now to add a Component into our DAL , you must right Click on the DAL project in my case "PropDAL" and select "Add" and "Add Component" and give it a meaningfull name, in my case i have named it "Property_DAL". Double click the Component and you will see that the name of the Component is the same as the Class name. use the name for the class you used in your BLL, in my case "Properties". That means the code in your component will look like this.

Public Class Properties
Inherits System.ComponentModel.Component         
End Class

Now Our Solution will look like the one in Figure1 the Structure.

Screenshot - Structure_Vs2003.jpg

Figure1

Step 4(Adding DAL Componets)

In this Step we are going to Add an Adapter. On your DAl Project, Double click on the Component "Property_DAL" in my case and select your toolbox section of "DATA". In the Toolbox select an SQLdataAdapter. and the Adapter dialog box will appear, click on "Next", Select the Connection. note that if its the First time you do this, you will have to Create a Connection like this Example. Click next when the Connection is established, in the "Query type" , for now lets select "Use SQl Statement" , and Click next. and you will get a Space where you can type in your SQl statement or Use a Query Builder to let Visual Studio Build a Query for you. Lets type our SQl state like this.

select * from Clients  where Client_name = @Clientname

This is a parameterized query. sometimes we dont know what Display until runtime and we might decide to display something else at runtime. Then in this case we use a parameterized query. The above sql query will be your commandtext in the adapter. when you are finished, you will click next,then the Report about your adapter will appear. If you see a yellow Exclamation mark on the Update,you should know that,it might be that you did not set a Primary Key in your table. If no Errors in your Adapter, click on Finish and your adapter will be created for you and note that the Connection object will appear on your Component. Right click and rename the connection object to "cnProperties" and you adapter to "daProperties". If you are using SQl, on the Properties of you connection, click on Connection string and enter the password after the user ID and your Connectionstring will look like this.

user id=sa; Password=topman;data source=Myserver;persist security info=False;initial catalog=ValRollClients

Next step is to Right click on the Adapter "daProperties" and Generate a dataset. Name your dataset as "dsProperties". and check the Option "Add this Dataset to the Designer" to visualy see it on top of the Component after that click ok.

Now everything had been setup. Our Data source if fine then lets go back to Presentation Layer.

Step 5(Presentation Layer)

Now in this Step we have to do small Adjustments. first add a datagrid into your Form and name it dgproperties and add four buttons like this.

Screenshot - Appl.jpg

Figure 2

and forget to rename your form to "frmsearch"

Step 6(Function in DAL)

Lets go back again to our DAL Project. we are going to give our data to BLL from DAL and BLL will give the data to the Frontend(PL)

Screenshot - N-Tier.jpg

Figure3

now lets create a Function that will return a Dataset and takes a parameter. now Double Click on your Component and enter the Following code to inside a Class.

Public Function Getdat(ByVal strname As String) As dsProperties
Dim dsdata As dsProperties
Try 
dsdata = New dsProperties
'We are Accepting an input parameter 
daproperties.SelectCommand.Parameters("Clients").Value = strname
'filling the dataset 
daproperties.Fill(dsdata)
Catch Throw 
'In this Layer we dont Display Error or Exceptions. BLl will take care of that 
'we are throwing the Exception to be handled by the Calling Function End Try 
'Here we are returning a dataset filled with requested data 
Return dsdata
End Function 

Remember we have to save changes too, so we are going to create another Sub procedure that will Save the changes into the Database. So for saving,create this sub procedure in your Component Class. After the First one like this.

Public Sub SaveData(ByVal dsdata As dsProperties)
Try
daproperties.Update(dsdata)
Catch
Throw
'In this Layer we dont Display Error or Exceptions. BLl will take care of that 
'we are throwing the Exception to be handled by the Calling Function
End Try
End Sub
 

Now everything Has been Added to our DAL, so we are going to call this Function and Sub Procedure in the BLL (Business Ligic Layer)

Step 7(Calling DAL Functions in BLL)

Now we are going to call DAL Function when Data is Requested from Business Logic layer from the Presentation Layer . Go to the BLL Project and Double click on the "clsPropertyBLL" to view the code. First thing we need to create is a Function that will request the Data from the DAL and a Save Function that will send data to the BLL and some Business Rules Check Before we save to the actual database. Before we start coding. Add the a reference to the DAL on the BLL. Right click on the BLL and select Add Reference, in the Dialogbox select the Project tab and click on your DaL Project and click ok.

In the Class Properties of the BLL , Add the Following code.

Public Function GetData(ByVal strname As String) As Prop_DAL.dsProperties
Dim dsdata As Prop_DAL.dsProperties
'Creating an Object of a Class Properties in the Dal layer
Dim objgetdal As Prop_DAL.Properties
Try
dsdata = New Prop_DAL.dsProperties
objgetdal = New Prop_DAL.Properties
'Calling the Function of DAL and Passing Arguments to the Function
dsdata = objgetdal.Getdata(strname)
If dsdata.Tables("Clients").Rows.Count = 0 Then
MsgBox("No record with that Name was Found")
Else
'return a dataset with Data
Return dsdata
End If
Catch ex As SqlClient.SqlException
MsgBox(ex.Message)
End Try
End Function
 

The next Sub Should save and a Sub to Check if the Business Rules have been Violated.

Public Function SaveData(ByVal dsdata As Prop_DAL.dsProperties) As Prop_DAL.dsProperties
Dim objDal As Prop_DAL.Properties
Try
objDal = New Prop_DAL.Properties
Me.CheckRules(dsdata)

'saving the Data

objDal.SaveData(dsdata)

Catch ex As Exception
End Try
End Function

in the Above Function we will check the Rules using a Sub that will come Below. if the Rules are not Violated, then we can save the data, by calling a Dal Function through a Dal object "objDAL". The Following code is the for Checking the Business Rules.

Private Sub CheckRules(ByVal dsdata As Prop_DAL.dsProperties)
Dim strMsg As String
Dim row As Prop_DAL.dsProperties.ClientsRow
'Iterating through the Dataset and Checking the Rules
For Each row In dsdata.Clients.Rows
If row.RowState = DataRowState.Added Or _
row.RowState = DataRowState.Modified Then
'Start Checking the Rules now
If row.Client_Name.Trim = "" Then
MsgBox("Name Cannot be Empty")
End If
If row.Client_Address = "" Then
MsgBox("Address Cannot be Empty")
End If
If row.Client_ID.ToString.Length > 13 Then
MsgBox("ID number must less than 13")
End If
End If
Next
If strMsg <> "" Then
'throw a new ApplicationException
'with our Custom Error message in it
Throw New ApplicationException(strMsg)
End If
End Sub

Now Everything is done, your BLL can now Communicate with your DAL and your PL will Communicate with your DAL through your BLL. the Next is to call your BLL Functions from your PL . Add a reference to your BLL Project in the PL Project as i did when i added a reference to DAl in the BLL Project.

Step 8(Call BLL functions from a PL)

This is the Last Step of our Article. In this Step we are going to Call the BLL Functions. Firstly bouble click on your Form and declare a module level dataset like this

Public Class Form1

Inherits System.Windows.Forms.Form

Dim mdsProperty As Prop_DAL.dsProperty

 

 

Note that this should not be inside your Form load, but outside, because its a Module level declaration that we want. On your Search button insert the Following code.

Dim objBll As Prop_BLL.Properties Dim strsearch As String = CStr(txtsearch.Text)
btnClear.Enabled = True
Try
 mdsProperty = New Prop_DAL.dsProperty
objBll = New Prop_BLL.Properties
 mdsProperty = objBll.GetData(strsearch)
'Binding the Datagrid
dgProperties.DataMember = "Clients"
dgProperties.DataSource = mdsProperty
 
Catch ex As SqlClient.SqlException
MessageBox.Show(ex.Message)
End Try

And in the Button Clear , you will Enter the Following code to Clear the Grid.

mdsProperty.Clear()
btnClear.Enabled = False

And the last Button, will be the Exit Button, you can Enter your own code to Exit the Application or Form. So your N-Tier Application should be done now with 8 Steps.

Conclusion

Designing your Application in an N-Tier Application helps you to trace Bugs and helps you to improve your application at ease, than trying to trace a small bug in one mixed Application having all Subjects in one Place. i would like to learn more from anyone who has Suggestion and more knowledge on N-tier Applications. My Next Article will be on Web services. I want make the Subject Broad by Biulding an Example of a Windows N-tier Application that uses Web services. To those who know me,you know that this is not my First Article. Thanks for every e-mail i got People Asking Questions about Article and how to implement it in their Situations. IF Thank me for this Article Vote for it and mail me at vuyiswam@tshwane.gov.za

Thanks

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionquestion on n-tier application framework design Pin
BensonWong
15:32 6 Nov '07  
AnswerRe: question on n-tier application framework design Pin
Vuyiswamb
21:23 6 Nov '07  
Generalperformance is not a reason for n-tier Pin
wk633
19:06 5 Nov '07  
GeneralRe: performance is not a reason for n-tier Pin
Vuyiswamb
20:44 5 Nov '07  
GeneralRe: performance is not a reason for n-tier Pin
wk633
5:26 6 Nov '07  
GeneralRe: performance is not a reason for n-tier Pin
Vuyiswamb
20:42 6 Nov '07  
GeneralHi Pin
Cape Town Developer
0:42 2 Nov '07  
GeneralRe: Hi Pin
Vuyiswamb
1:51 2 Nov '07  
GeneralNice work Pin
titandeveloper
13:28 31 Oct '07  
GeneralRe: Nice work Pin
Vuyiswa
4:32 14 Feb '08  
Generalsteps 9 to 9467 are missing Pin
Thanks for all the fish
8:41 31 Oct '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
8:43 31 Oct '07  
GeneralRe: steps 9 to 9467 are missing Pin
Ray Cassick
10:57 31 Oct '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
20:47 31 Oct '07  
GeneralRe: steps 9 to 9467 are missing Pin
Thanks for all the fish
8:54 2 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
21:41 4 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
20:16 5 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
wk633
5:28 6 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
20:21 6 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
Thanks for all the fish
6:42 7 Nov '07  
GeneralRe: steps 9 to 9467 are missing Pin
Vuyiswamb
19:49 7 Nov '07  


Last Updated 14 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009