3. Describe the process of interact UI to BAL n DAL?

Answer Posted / samik

This is a chunk of code from the user interface. I am only
including the functions that are used to call the middle
tier or the business logic layer.

I am keeping a reference to business logic layer as BOCustomer.
Collapse

//This function get the details from the user via GUI

//tier and calls the Add method of business logic layer.

private void cmdAdd_Click(object sender, System.EventArgs e)
{
try
{
cus = new BOCustomer();
cus.cusID=txtID.Text.ToString();
cus.LName = txtLName.Text.ToString();
cus.FName = txtFName.Text.ToString();
cus.Tel= txtTel.Text.ToString();
cus.Address = txtAddress.Text.ToString();
cus.Add();
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}

//This function gets the ID from the user and finds the

//customer details and return the details in the form of

//a dataset via busniss object layer. Then it loops through

//the content of the dataset and fills the controls.


private void cmdFind_Click(object sender, System.EventArgs e)
{
try
{
String cusID = txtID.Text.ToString();

BOCustomer thisCus = new BOCustomer();

DataSet ds = thisCus.Find(cusID);

DataRow row;
row = ds.Tables[0].Rows[0];

//via looping

foreach(DataRow rows in ds.Tables[0].Rows )
{
txtFName.Text = rows["CUS_F_NAME"].ToString();
txtLName.Text = rows["CUS_L_NAME"].ToString();
txtAddress.Text = rows["CUS_ADDRESS"].ToString();
txtTel.Text = rows["CUS_TEL"].ToString();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}

}

//this function used to update the customer details.

private void cmdUpdate_Click(object sender,
System.EventArgs e)
{
try
{
cus = new BOCustomer();
cus.cusID=txtID.Text.ToString();
cus.LName = txtLName.Text.ToString();
cus.FName = txtFName.Text.ToString();
cus.Tel= txtTel.Text.ToString();
cus.Address = txtAddress.Text.ToString();

cus.Update();
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}

Business Logic layer

Here, I am including all the code for this tier. Basically
it has properties that are needed to define the customer
object. But as I mentioned it is just a dummy customer and
many other properties can be added if required. It also has
all the methods including Add, update, find that are
required to maintain customer details.

This is the middle tier and acts between the GUI and the
Data access layer. It keeps a reference to the data access
tier as cusData = new DACustomer(). It also has a reference
to System.Data namespace as sometimes it returns details in
the form of DataSet to the GUI tier.
Collapse

using System;
using System.Data;

namespace _3tierarchitecture
{
/// <SUMMARY>

/// Summary description for BOCustomer.

/// </SUMMARY>


public class BOCustomer
{
//Customer properties

private String fName;
private String lName;
private String cusId;
private String address;
private String tel;

private DACustomer cusData;

public BOCustomer()
{
//An instance of the Data access layer!

cusData = new DACustomer();
}



/// <SUMMARY>

/// Property FirstName (String)

/// </SUMMARY>

public String FName
{

get
{
return this.fName;
}
set
{
try
{
this.fName = value;

if (this.fName == "")
{
throw new Exception(
"Please provide first
name ...");
}
}
catch(Exception e)
{
throw new
Exception(e.Message.ToString());
}
}
}

/// <SUMMARY>

/// Property LastName (String)

/// </SUMMARY>

public String LName
{
get
{
return this.lName;
}
set
{
//could be more checkings here eg
revmove ' chars

//change to proper case

//blah blah

this.lName = value;
if (this.LName == "")
{
throw new Exception("Please
provide name ...");
}

}
}

/// <SUMMARY>

/// Property Customer ID (String)

/// </SUMMARY>

public String cusID
{
get
{
return this.cusId;
}
set
{
this.cusId = value;
if (this.cusID == "")
{
throw new Exception("Please
provide ID ...");
}

}
}

/// <SUMMARY>

/// Property Address (String)

/// </SUMMARY>

public String Address
{
get
{
return this.address;
}
set
{
this.address = value;

if (this.Address == "")
{
throw new Exception("Please
provide address ...");
}
}
}

/// <SUMMARY>

/// Property Telephone (String)

/// </SUMMARY>

public String Tel
{
get
{
return this.tel;
}
set
{
this.tel = value;
if (this.Tel == "")
{
throw new Exception("Please
provide Tel ...");
}

}
}

/// <SUMMARY>

/// Function Add new customer. Calls

/// the function in Data layer.

/// </SUMMARY>

public void Add()
{
cusData.Add(this);
}


/// <SUMMARY>

/// Function Update customer details.

/// Calls the function in Data layer.

/// </SUMMARY>

public void Update()
{
cusData.Update(this);
}

/// <SUMMARY>

/// Function Find customer. Calls the

/// function in Data layer.

/// It returns the details of the customer using

/// customer ID via a Dataset to GUI tier.

/// </SUMMARY>

public DataSet Find(String str)
{
if (str == "")
throw new Exception("Please provide ID
to search");

DataSet data = null;

data = cusData.Find(str);

return data;
}
}
}

Data Access Layer

The data tier has details to manipulate an MS Access
database. However, all these details are transparent and do
not affect the business logic layer. This module has a
reference to the business logic layer as BOCustomer cus. To
make the usage easier and be supported by any other database
I have created some constants including the database name
and field name that need to be changed as per the customer
table details. This is an usable module for any database
after making these changes.
Collapse

using System;
using System.Data.OleDb;
using System.Data;

namespace _3tierarchitecture

{

/// <SUMMARY>

/// Summary description for DACustomer.

/// </SUMMARY>

public class DACustomer
{
private OleDbConnection cnn;
//change connection string as per the

//folder you unzip the files

private const string CnnStr =
"Provider=Microsoft.Jet.OLEDB.4.0;Data " +
"Source= D:\\Rahman_Backup\\Programming\\" +
"Csharp\\3tierarchitecture\\customer.mdb;";

//local variables
private String strTable="";
private String strFields="";
private String strValues="";
private String insertStr="";

//this needs to be changed based on customer
//table fields' Name of the database!
private const String thisTable = "tblCustomer";
private const String cus_ID = "CUS_ID";
private const String cus_LName = "CUS_L_NAME";
private const String cus_FName = "CUS_F_NAME";
private const String cus_Tel = "CUS_TEL";
private const String cus_Address = "CUS_ADDRESS";


public DACustomer()
{
}

public DACustomer(BOCustomer cus)
{
// A reference of the business object class
}

//standard dataset function that adds a new customer

public void Add(BOCustomer cus)
{

String str = BuildAddString(cus);

OpenCnn();

//Open command option - cnn parameter is imporant
OleDbCommand cmd = new OleDbCommand(str,cnn);


//execute connection
cmd.ExecuteNonQuery();

// close connection
CloseCnn();

}

//standard dataset function that updates
//details of a customer based on ID
public void Update(BOCustomer cus)
{
OpenCnn();

String selectStr = "UPDATE " + thisTable +
" set " + cus_LName + " = '" + cus.LName + "'" +
", " + cus_FName + " = '" + cus.FName + "'" +
", " + cus_Address + " = '" + cus.Address +
"'" +
", " + cus_Tel + " = '" + cus.Tel + "'" +
" where cus_ID = '" + cus.cusID + "'";

OleDbCommand cmd = new OleDbCommand(selectStr,cnn);

cmd.ExecuteNonQuery();

CloseCnn();
}

//standard dataset function that finds and
//return the detail of a customer in a dataset
public DataSet Find(String argStr)
{
DataSet ds=null;

try
{
OpenCnn();

String selectStr = "select * from " +
thisTable +
" where cus_ID = '" + argStr +
"'";
OleDbDataAdapter da =
new OleDbDataAdapter(selectStr,cnn);
ds = new DataSet();
da.Fill(ds,thisTable);

CloseCnn();


}
catch(Exception e)
{
String Str = e.Message;
}

return ds;
}

private void OpenCnn()
{
// initialise connection
String cnnStr = CnnStr;
cnn = new OleDbConnection(cnnStr);
// open connection
cnn.Open();
}

private void CloseCnn()
{
// 5- step five
cnn.Close();
}

// just a supporting function that builds
// and return the insert string for dataset.
private String BuildAddString(BOCustomer cus)
{
// these are the constants as
// set in the top of this module.
strTable="Insert into " + thisTable;
strFields=" (" + cus_ID +
"," + cus_LName +
"," + cus_FName +
"," + cus_Address +
"," + cus_Tel + ")";

//these are the attributes of the
//customer business object.
strValues= " Values ( '" + cus.cusID +
"' , '" + cus.LName +
"' , '" + cus.FName +
"' , '" + cus.Address +
"' , '" + cus.Tel + "' )";

insertStr = strTable + strFields + strValues;

return insertStr;

}
}
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the difference between a sub and a function in c#.

625


Explain the use of Mutex in C#?

651


Why do we Need of static class in c#?

721


Why main method is static in c#?

567


What is interface c#?

541






What is the purpose of reserved word using in c#?

576


What does exclamation mark mean in access query?

648


What is the use of dll file in c#?

557


Why attributes are used in c#?

555


What are the drawbacks of extending an interface as opposed to extending a class?

546


What is the difference between serialization and deserialization in c#?

529


What is a nested type. Give an example?

721


What are the advantages of using delegates in c#?

547


Is typeof c#?

616


What are the fundamental differences between value types and reference types?

619