2LeggedSpider

Building a 3-Tier ASP.NET Application without Visual Studio.NET

Posted in ASP.NET by Sumit Thomas on May 20, 2004

[tweetmeme style=”compact”]This is an article which I wrote for a website, long back when I was a novice to ASP.NET. I hope it will be useful to anyone who is new to .NET and don’t have the luxury of Visual Studio.NET to learn or develop .NET applications.

Prerequisites:

  • .NET Framework 1.0 or later
  • Willingness to work in DOS environment 🙂

.NET Classes used :

  • System.Data.SqlClient.SqlConnection
  • System.Data.SqlClient.SqlDataAdapter
  • System.Data.DataSet

Developing n-Tier applications! This was a something, which never crossed my mind when I was developing Web applications in ASP. Its not because it was impossible, it was and still is possible by using the good old COM. The reason was that the application gets hosted with a third party who hardly permits us to register components on their server. Well, we had to compromise on something. But when you think of all the problems with COM, it was in someway a good compromise to make.

The .NET framework made a paradigm shift to the way I develop applications. When I developed my first simple 3-tier application in ASP.NET, I knew that this is the technology for me.

In this article, I will take you through the simple process of developing a 3-tier application using the worlds best editor, Notepad and the C# compiler available with the .NET Framework.

For those who have never heard of n-Tier applications, let me give you a small introduction.

A n-Tier refers to an application that has at least 3 logical layers or tiers, namely:

1) Data Access Layer or Data Tier
2) Business Logic Layer or Business Tier
3) Presentation Layer or Tier

Often the Business and Data Tier are coupled as single tier in some applications. The Data tier is responsible for connecting to a data source. The business tier takes care of retrieving, adding, modifying or deleting the data from the Data tier. It passes the data from the Data tier to the presentation tier. The presentation tier takes care of presenting the data.

Each layer or tier interacts only with its adjacent neighbour. For instance, Presentation Layer interacts with the Business Layer and not the Data Layer. Thus there is a high level of abstraction between layers. This makes it possible to change or update one layer without touching the other. The advantage of using a n-Tier architecture in applications is enormous and out of scope of this article.

Now that we have an idea of what a n-Tier application is, lets develop a small application.

Data Tier

1) Open our editor, Notepad and type in the following code:

using System;
using System.Data;
using System.Data.SqlClient;

namespace Savage.DAL
{
public class DataAccessLayer
{
private string connectionString="server=Savage\\NetSDK;DataBase=NorthWind;Integrated Security=true;";

public DataSet ReturnDataSet(string Sql)
{
SqlConnection conn=new SqlConnection(connectionString);
SqlDataAdapter adp=new SqlDataAdapter(Sql,conn);
DataSet ds=new DataSet();
adp.Fill(ds);
conn.Close();
return ds;
}
}
}

We are creating a class called DataAccessLayer that contains a function called ReturnDataSet that returns a DataSet based on the SQL string passed to it. Since we are connecting to the Northwind database in our SQL Server, we use the System.Data.SqlClient namespace. You have to change the connectionString value.

2) Save the file as dal.cs
3) Now we have to compile the file using our C# complier CSC.exe
4) Suppose we are storing the file in C:\Tier, create a folder called bin. This is where we will be storing the complied DLLs.
5) Under command prompt type the following
C:\Tier> csc /target:library /out:Savage.DAL.dll /r:System.Data.dll dal.cs

Don’t get scared from the above line. It’s very simple. We are telling the C# compiler csc that we need to compile the file dal.cs as a DLL file (specified by /target:library parameter) and it references the System.Data.dll within it (/r:System.Data.dll). The name of the dll will be Savage.DAL.dll (specified by the /out:Savage.DAL.dll parameter)

Business Tier

1) For the business tier again open the Notepad and type the following code:

using System.Data;
using Savage.DAL;

namespace Savage.BLL
{
 public class BusinessLogicLayer
 {
  public DataSet GetCustomers()
  {
   DataAccessLayer dal=new DataAccessLayer();
   return dal.ReturnDataSet("select * from Customers");
  }
 }
}

Save this file as bll.cs

2) Again we will use the CSC complier. Type the following to compile it.
C:\Tier> csc /target:library /out:Savage.BLL.dll /r:System.Data.dll,Savage.DAL.dll bll.cs

Here note that we are also referencing the Savage.DAL.dll we created for Data tier.

Presentation Tier

For the presentation tier we will be using the codebehind method.

1) Open Notepad and type the following





Simple 3 tier application





We have included a DataGrid named myGrid in our file. Note the Page directive in the first line. We indicate that the code for this file will be available in pl.cs file, which we will be creating in a short while.

2) Save this file as default.aspx
3) Now we have to create the pl.cs file.
4) Open Notepad and type the following code

using System;
using Savage.BLL;

namespace Savage.PL
{
public class PresentationLayer:System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid myGrid;
private void Page_Load(object sender, System.EventArgs e)
{
myGrid.DataSource=new BusinessLogicLayer().GetCustomers();
myGrid.DataBind();
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}

We have included the Savage.BLL namespace in our code. The overriden method OnInit and InitializeComponent have to be included in our code for it to work. In the Page_Load method we are supplying a datasource to our DataGrid and binding it to it.

3) We have to compile the pl.cs file now.
4) Type the following at the command prompt to compile
C:\Tier> csc /target:library /out:Savage.PL.dll /r:Savage.BLL.dll pl.cs

Now that we have the compiled dll’s with us, copy them to the bin folder. We have to create a virtual directory for our application in the IIS. Lets name the virtual directory as 3tier. Now open the browser and type http://Savage/3tier/default.aspx where Savage is my system name, 3tier is the name of our virtual directory and default.aspx is the file to execute. You will see the records of the customer table from the Northwind database in our DataGrid.

Though this is a very simple example, it gives us an idea of what a 3-Tier application is all about. We can extend the idea to build bigger more complex applications. We also note that we can build simple ASP.NET applications without Visual Studio.

, ,
Advertisements
Tagged with: , ,

7 Responses

Subscribe to comments with RSS.

  1. Kasper said, on August 6, 2008 at 11:26 pm

    Thanx…it was a helpful post.

  2. Jane Goody said, on April 22, 2009 at 6:35 am

    If you want to read a reader’s feedback 🙂 , I rate this post for 4/5. Decent info, but I just have to go to that damn yahoo to find the missed parts. Thanks, anyway!

  3. sanjay kushwaha said, on November 9, 2009 at 9:02 am

    thanks,
    i got the concept of n-tier in .net technology

  4. Rajnish said, on February 15, 2010 at 1:41 pm

    Thanks for awesome information.

  5. Movie links said, on August 7, 2010 at 7:47 pm

    great info..keep it up!

  6. PROCESS SERVICES said, on June 23, 2012 at 6:48 am

    Simply desire to say your article is as amazing. The clarity for your publish is simply excellent and
    that i can think you’re an expert on this subject. Fine along with your permission let me to take hold of your RSS feed to keep updated with approaching post. Thank you 1,000,000 and please keep up the gratifying work.|

  7. Lawerence said, on October 18, 2012 at 9:08 am

    Highly energetic post, I loved that a lot.
    Will there be a part 2?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: