Here is the simplest 3 tier example I can provide you with. I am using string return types & only returning strings but you will be returning DataSets or DataReaders & retrieving data from DB.
Create a new ASP.Net Web Application - I called mine "Test3Tier" namespace Test3Tier.
Add a new ClassLibrary project to the Solution. I called mine "BusinessLayer" - namespace BusinessLayer & I called the Class "BusLayer".
Add another new ClassLibrary project to the Solution. I called mine "DAL" - namespace DAL. I called the Class "DataLayer".
Add a reference in the BusinessLayer project to the DAL project. Add a using directive for it to the code (using DAL;)
Add a reference in the Front End project to the Business Layer project. Add a using directive in the codebehind (using BusinessLayer;)
The code in your DataLayer.cs will be something like the following (depending on your namespaces)
//start of code
using System;
namespace DAL
{
/// <summary>
/// Summary description for DataLayer.
/// </summary>
public class DataLayer
{
public DataLayer()
{
//
// TODO: Add constructor logic here
//
}
public string GetData()
{
//This is where you would do your database work.
return "From DataLayer"; //Instead of returning a string it would be a DataSet or DataReader
}
}
}
//end of code
The code in your BusLayer.cs will be something like the following (depending on your namespaces)
//start of code
using System;
using DAL;
namespace BusinessLayer
{
/// <summary>
/// Summary description for BusLayer.
/// </summary>
public class BusLayer
{
public BusLayer()
{
//
// TODO: Add constructor logic here
//
}
public string GetData()
{
DAL.DataLayer newDataLayerObj = new DataLayer();
return newDataLayerObj.GetData() + "<br>" + " From BusLayer";
}
}
}
//end of code
Add a button on the webpage.aspx (In your front end project) called Button1.
add code to the click event for the button so it looks something like this:
BusinessLayer.BusLayer newBusLayerObj = new BusLayer();
Response.Write("From WebPage" + "<br>" + newBusLayerObj.GetData());
When you compile & run the webpage will create an instance of BusLayer & run GetData, which will create an instance of DataLayer & run GetData which returns a string to the GetData in BusLayer.
The GetData method in BusLayer then takes this return value from DataLayer & concatenates it with a string & returns it to the WebPage.
The webpage then displays this using Responce.Write.
Main Topics
Browse All Topics





by: stumpy1Posted on 2007-05-24 at 08:32:10ID: 19150065
Ok - why do you want/need to reference the connection from the DAL in employeeLookUp.Web? That defeats using 3 tier.
The way your application should work is that your Presentation Layer (aspx codebehind page) should create an instance/object of your Business Layer Class.
You then call the relevent method in your instance of that class (Object.Method()) which will create an instance/object of your Data Layer Class.
You then call the relevent method in your instance of that class (Object.Method()) which will take care of running your SP & returns whatever you need - DataReader, DataSet .... to your Business Layer.
Your Business layer will do whatever it need to on the Data & return the data to the presentation layer where you display it.