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 Sumit.DAL { public class DataAccessLayer {
private string connectionString="server=SUMIT\\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:Sumit.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 Sumit.DAL.dll (specified by the /out:Sumit.DAL.dll parameter)
Business Tier
1) For the business tier again open the Notepad and type the following code:
using System; using System.Data; using Sumit.DAL;
namespace Sumit.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:Sumit.BLL.dll /r:System.Data.dll,Sumit.DAL.dll bll.cs
Here note that we are also referencing the Sumit.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
<%@ Page language="c#" Codebehind="pl.cs" AutoEventWireup="false" Inherits="Sumit.PL.PresentationLayer"%>
<html>
<head>
<title>Simple 3 tier application</title>
</head>
<body>
<asp:DataGrid id="myGrid" runat="server" />
</body>
</html>
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 Sumit.BLL;
namespace Sumit.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 Sumit.BLL namespace in our code. The overriden method OnInit and InitializeComponent have to 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:Sumit.PL.dll /r:Sumit.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://sumit/3tier/default.aspx where sumit 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 ASP.NET applications without Visual Studio too.
Cheers
|
| Author: Chetla Hari Kumar Reddy 16 Jun 2004 | Member Level: Bronze Points : 0 |
Hi Sumeet,
Your article is very useful to those who are not able to understand what exactly the n-tire applications. It is very clear and motivating keep it up.
|
| Author: Pascal Hoffmann 16 Jun 2004 | Member Level: Bronze Points : 0 |
Hi Sumit,
that's exactly the kind of article I have been looking for. -Clear and simple- no confusing additional code, no tool dependent explanations
Thanks
Pascal Hoffmann Basel, Switzerland
|
| Author: critic 16 Jun 2004 | Member Level: Bronze Points : 0 |
Everythign looks great, except...
1. There is no indentation in the sample code.
2. You have shown
.NET Classes used : System.Data
But I thought System.Data is a namespace, not a class !
You must provide the list of .NET classes (with fully qualified names) instead of the namespace.
|
| Author: Sumit Thomas 17 Jun 2004 | Member Level: Silver Points : 0 |
Dear Critic,
Thanks for the feedback.
Probably I didn't have a second look at the article after I posted it. I am sure I typed the class used in the sample properly, which ain't getting updated properly even now.
Kudos to your team for such a great website.
Cheers, Sumit
|
| Author: critic 17 Jun 2004 | Member Level: Bronze Points : 0 |
It looks simply great now !!!
The last class name was not visible due to a bug in the site.... it appears fine if we give a blank line after the last class name (bad guys at dotnetspider... )
|
| Author: Sri Reddy 17 Jun 2004 | Member Level: Bronze Points : 0 |
First thanks to Author, great example.
I got two compile errors:
I. When compling bll.cs, got CS2015 error. Was expecting source code file instead of binary dll file. Changed
csc /target:library /out:Sumit.BLL.dll /r:System.Data.dll, Sumit.DAL.dll bll.cs
TO
csc /target:library /out:Sumit.BLL.dll /r:System.Data.dll, DAL.CS bll.cs
II. Extra spce in comman line CHANGED csc /target:library /out:Sumit.PL.dll /r: Sumit.BLL.dll pl.cs ^
TO csc /target:library /out:Sumit.PL.dll /r:Sumit.BLL.dll pl.cs
That seems to have fixed the errors.
Thanks -Sri
|
| Author: Sumit Thomas 18 Jun 2004 | Member Level: Silver Points : 0 |
Hi Sri,
Thanks for pointing out the 'space' in the command line argument. I have corrected it.
csc /target:library /out:Sumit.BLL.dll /r:System.Data.dll,Sumit.DAL.dll bll.cs should work if you had compiled dal.cs as written in the article.
Anyway thanks again.
Cheers, Sumit
|
| Author: Pranav 15 Jul 2004 | Member Level: Bronze Points : 0 |
Sumit Your article is really nice. I am a beginner to ASP.NET & C# so really did not know how to compile the "code behind class files" without using VS.NET ...but ur article has helped. thanks. And keep posting such fundamental articles.... really helpful to those who are learning .NET.
|
| Author: Francisco 04 Aug 2004 | Member Level: Bronze Points : 0 |
Hi Sumit, I followed your code very easily, even I use ASP.NET VB. I'm doing one similar example, but in VB, and although the code runs very well when it is in the page (.aspx) i continuosly get errors when it is in a namespace-class apart, even though the vbc compiler gives me no error. This is my code, I hope you can help me. Thanks in advance, Francisco.
-------------------
' VB Document Imports System Imports System.Data Imports System.Web Imports System.Web.UI Imports System.Web.Caching Imports System.Data.SqlClient Imports System.Data.DataSet
NameSpace WebLabels Public Class WebLabelsCache : Inherits System.Web.UI.Page
Public Function GetPageLabels_fromDB(NumPage as integer, conString AS String) AS DataTable Dim objConn As SqlConnection Dim dadCmd AS SqlDataAdapter Dim dstTemp AS DataSet Dim strSql As String Dim dtblTemp AS DataTable dstTemp = New DataSet() strSQL = "EXECUTE spGetPageLabels " & NumPage
objConn = New SqlConnection( conString ) dadCmd = New SqlDataAdapter(strSQL, objConn ) dadCmd.Fill(dstTemp,"temp") dtblTemp = dstTemp.Tables("temp") Return dtblTemp End Function End Class End Namespace
|
| Author: abhishek 20 Oct 2004 | Member Level: Bronze Points : 0 |
I m abhishek,I have a problem related with compiling using command prompt , If i have a microsoft.web.ui.controls.dll which i want refer to. So I was to use VS.net 7.0 then i simply add reference if I'm using notepad then how do i add references
please get to this problem .
regards abhishek daveabhi81@rediffmail.com
|
| Author: Francisco 20 Oct 2004 | Member Level: Bronze Points : 0 |
Ok, I use VB, pur using C should be something similar. I did this ".bat" file to compile my VB files.
@echo on C:\WINNT\Microsoft.NET\Framework\v1.1.4322\vbc /t:library /r:system.dll,system.web.dll,microsoft.visualbasic.dll,system.data.dll,system.xml.dll %1.vb move %1.dll bin
Cheers
|
| Author: abhishek 23 Oct 2004 | Member Level: Bronze Points : 0 |
hi, I got lots of help from ur Idea, So I went a bit high up and In my .vb file I wrote two classes inside one namespace, compiled it from command prompt(cmd). know I import that dll to my next .vb class but doing this i get an error as err:bc30466 namespace or type "xyz.dll" not found Please help me in that for ur further clarification I m trying to develop controls so In Ist file I put all the control classes and compile it and then i use it in the 2nd .vb class by making object but the error persist
pls help its urgent
regards abhishek (daveabhi81@rediffmail.com)
|