Download My_Article_Example.zip - 214.0 KB
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.
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.
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.
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.
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.
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.

Figure1
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.
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.

Figure 2
and forget to rename your form to "frmsearch"
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)

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)
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.
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.
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. | |||||
|
|||||
|
|||||