Thursday, January 29, 2009

Registration form in three tire architecture.

This 3 tier architecture or layers are categorised such that they communicate to each other to perform tasks.
These 3 layers are said to be as
1) Presentation Layer (User Interface form).
2) Business Layer (Layer where business plans can be validated or Media between user to application core data base.
3) Data Layer (Layer which is very specific to access database and does not interact with user directly but send/receive information from User through Business Layer.





We can find 3 blocks in this diagram that states that there is inter block communication to handle requests and response from user.

Example:-
I would like to create a windows form named “3Tier –Demo”.
Presentation Layer
So this comes under first layer (from user prospective) or User Interface.




Business Layer
I would like to add new project saying “class library” to this solution saying “BusinessLayer”.
Rename the default created “Class1.cs” to “Form.cs”.




Data Layer
Similar fashion as above I would like to add one more class library as new project for this solution saying “DataLayer”.
Rename the default created “Class1.cs” to “Form.cs”.




So now there are three layers available to our application (3Tier-Demo).



Step 1
First I would to start coding from Data layer, i.e; to start writing sql querues that perform sql transactions with the database like select, Insert, Update, Delete etc…

using System;
using System.Collections.Generic;
using System.Text;

namespace DataLayer
{
public class Form
{
//Connection string set to this which is read and set from app.config file or any other
//hardcoded variable
public string ConnectionString;
public void InsertData(string argName, int argAge, string argOccupation, string argPlace)
{
//Write code to open orcale/sql connection, insert query to be performed, etc...
}
public void ReadData(string argName,out int argAge,out string argOccupation,out string argPlace)
{
argAge = 0;
argOccupation = String.Empty;
argPlace = String.Empty;
//Write code to open orcale/sql connection, select query to read user
//details based upon given username (argName)
//Assign readed age,location,place to the given out parameters. }

}
}


Now build data layer, add this as reference to business layer.
Since business layer has to interact with data layer to handle user requests or response.




Step 2
Now I would like to start coding in business layer (Form.cs).

using System;
using System.Collections.Generic;
using System.Text;
using DataLayer;
namespace BusinessLayer
{
public class Form
{
//Connection string set to this which is read and set from app.config file or any other
//hardcoded variable
public string ConnectionString;
public void InsertData(string argName, int argAge, string argOccupation, string argPlace)
{
DataLayer.Form objForm = new DataLayer.Form();
objForm.ConnectionString = ConnectionString;
objForm.InsertData(argName, argAge, argOccupation, argPlace);
}
public void ReadData(string argName, out int argAge, out string argOccupation, out string argPlace)
{
argAge = 0;
argOccupation = String.Empty;
argPlace = String.Empty;
DataLayer.Form objForm = new DataLayer.Form();
objForm.ConnectionString = ConnectionString;
objForm.ReadData(argName,out argAge, out argOccupation, out argPlace);
}
}
}


Similarly, add this business layer as reference to presentation layer (3Tier-Demo).



Step 3
I would like to start coding in Presentation Layer or 3Tier-Demo, in the windows form accessing the business layers form class to get or send data from or to database through data layer.









Now after building the application, it is fine.
Things left and to be written or updated
1) Write sql queries for specific actions like Insert or select or update to specific table.
2) Mention connection string for appropriate database.
3) Call these “Insertdata” and “Getdata” functions in Presentation layer where ever needed or required.
There are some instants where we introduce one more layer saying
"Common Layer" that holds the data transfered from/to between these 3 layers



No comments: