Forums » .NET » ASP.NET »

Example for three tier architechture


Posted Date: 15 Sep 2011      Posted By:: kalaingar     Member Level: Bronze  Member Rank: 0     Points: 1   Responses: 3

example for three tier architecture tell me with screen shot as soon as possible

Responses

#632450    Author: BHANU PRAKASH BYSANI      Member Level: Gold      Member Rank: 0     Date: 15/Sep/2011   Rating: 2 out of 52 out of 5     Points: 0

Hi,


3-Tier architecture generally contains UI or Presentation Layer, Business Access Layer (BAL) or Business Logic Layer and Data Access Layer (DAL).

Presentation Layer (UI)
Presentation layer cotains pages like .aspx or windows form where data is presented to the user or input is taken from the user.

Business Access Layer (BAL) or Business Logic Layer
BAL contains business logic, validations or calculations related with the data, if needed. I will call it Business Access Layer in my demo.

Data Access Layer (DAL)
DAL contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc). For this demo application, I have taken a very simple example. I am assuming that I have to play with record of persons (FirstName, LastName, Age) and I will refer only these data through out this article.

For complete information follow the below link

http://www.dotnetfunda.com/articles/article71.aspx

Best Regards,

Bhanu Prakash



 
#632461    Author: Asheej T K        Member Level: Gold      Member Rank: 2     Date: 15/Sep/2011   Rating: 2 out of 52 out of 5     Points: 4

Hi,

I am not sure how can I give screen print of Data Access later and business layer...

Check the code below, I tried my level best to explain it properly,

Web.config entry for the connection string,


<connectionStrings>
<add name="sqlConnection" providerName="System.Data.SqlClient" connectionString="Data Source=DBServerName;Initial Catalog=DBName;Trusted_Connection=False;User ID=UserID;Password=password;"/>
</connectionStrings>



If you don't have app_code folder you may create this folder first the add a new class file to your project wiht the name DB.cs. While adding the class file it may prompt you to create the app_code folder that time you may click Yes.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;


/// <summary>
/// Summary description for DB
/// </summary>
namespace DNSTestApp.DBClass
{
public class DB
{
public DB()
{
//
// TODO: Add constructor logic here
//
}



public DataSet GetUserList()
{
DataSet objDataSet = new DataSet();
SqlDataAdapter objDataAdatpter;
string strSQLconstring = System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnection"].ToString();

SqlConnection objConnection = new SqlConnection(strSQLconstring);
SqlCommand _Command = new SqlCommand("sp_GetUserList", objConnection);
try
{
if (objConnection.State == ConnectionState.Closed)
{
objConnection.Open();
}


_Command.CommandType = CommandType.StoredProcedure;
_Command.Prepare();
objDataAdatpter = new SqlDataAdapter(_Command);
objDataAdatpter.Fill(objDataSet);

}
catch (Exception ex)
{
String sqlError = "SQL Query failed: " + ex.Message;
}
finally
{
objDataAdatpter = null;
_Command = null;
objConnection.Close();
}

return objDataSet;
}
}
}



Below is a funtion you will be creating in your code behind page to call the function inside the DB.cs class file.


private void PopulateUser()
{
DNSTestApp.DBClass.DB objUserDet = new DNSTestApp.DBClass.DB();
DataSet dsUserDet = objUserDet.GetUserList();
dgUserList.DataSource = dsUserDet;
dgUserList.DataBind();
}



I haven't give the code for your presentation layer(UI) which you may design based on your requirement.


Let me know if you need any more explanation for this.


Regards,
Asheej T K


 
#632487    Author: Pawan Awasthi        Member Level: Gold      Member Rank: 3     Date: 15/Sep/2011   Rating: 2 out of 52 out of 5     Points: 4

Hai Kalaingar,

3-tier architecture is the triad of layers where the layers are as follows:


1. Application Layer/UI Layer/Client Layer/User Layer

2. Business Logic Layer/ Validation Layer

3. Data Access Layer


These layers consist of in a single application solution and they communicate through the objects.

See below the structure of the 3-tier architecture:

1. Application Layer/UI Layer/Client Layer/User Layer


This layer mainly consists of the client code or the front end of the application.
Ex. there is a Login.aspx page which contains the code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Application3TierArchitecture.ApplicationLayer.Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 384px;
}
#form1
{
width: 478px;
}
.style3
{
width: 288px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1"
style="border: thin solid #0000FF; font-family: Verdana; font-size: small; color: #008000; background-color: #C0C0C0;" width="300">
<tr>
<td colspan="2">
<asp:Label ID="lblUserLogin" runat="server"
style="font-weight: 700; text-decoration: underline" Text="USER LOGIN FORM"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lblUserId" runat="server" Text="User Id"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtUserName" runat="server" Font-Bold="True"
Font-Names="Verdana" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"
Font-Bold="True" Font-Names="Verdana" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" class="style3">
<asp:Button ID="btnLogin" runat="server" Text="Login"
onclick="btnLogin_Click" />
</td>
<td class="style2">
<asp:Button ID="btnCancel" runat="server" Text="Cancel"
UseSubmitBehavior="False" />
</td>
</tr>
</table>
</form>
</body>
</html>


Code for the codebehind files Login.aspx.cs


using System;
using Application3TierArchitecture.BusinessLogicLayer;

namespace Application3TierArchitecture.ApplicationLayer
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnLogin_Click(object sender, EventArgs e)
{
UserBL objUserBL = new UserBL();
objUserBL.UserName = txtUserName.Text;
objUserBL.Password = txtPassword.Text;

int i = objUserBL.AuthenticateUser(objUserBL);
if (i == 1)
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Login", "<script>aler('valid user.')</script>");
else
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Login", "<script>aler('User name or password is not correct.')</script>");
}
}
}


2. Business Layer

This will be a new class called UserBL.cs as

using System;
using Application3TierArchitecture.BusinessLogicLayer;

namespace Application3TierArchitecture.ApplicationLayer
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnLogin_Click(object sender, EventArgs e)
{
UserBL objUserBL = new UserBL();
objUserBL.UserName = txtUserName.Text;
objUserBL.Password = txtPassword.Text;

int i = objUserBL.AuthenticateUser(objUserBL);
if (i == 1)
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Login", "<script>aler('valid user.')</script>");
else
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Login", "<script>aler('User name or password is not correct.')</script>");
}
}
}


3. Data Access Layer

This layer mainly contains all the code related to data access communication with the database.
A new class names UserDAL.cs will be created as:


using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Application3TierArchitecture.BusinessLogicLayer;

namespace Application3TierArchitecture.DataAccessLayer
{
public class UserDAL
{
internal int ValidateUserDAL(UserBL objUserBL)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlDataAdapter da = new SqlDataAdapter ("SELECT COUNT(*) FROM USER WHERE USERID="+ objUserBL.UserName + "AND PASSWORD =" + objUserBL.Password, con);
DataSet ds = new DataSet();
da.Fill(ds, "USER");
if (ds.Tables[0].Rows.Count > 0)
return 1;
else return 0;
}
}
}


Hope it will get the idea about the implementation of the 3 tier architecture in the project.
Let me know if you have any issue.

Regards,
Pawan Awasthi(DNS MVM)
+91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
pawansoftit@gmail.com





 
Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.





  • Return to Discussion Forum
  • Start new thread


  • Subscribe to Subscribers
    Top Contributors
    Today
      Last 7 Daysmore...

      Awards & Gifts

      Online Members

      More...
       
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India