Designing the Interface and More for an Online E-Mail System in ASP.NET 2.0 - Designing the Data Tier
(Page 2 of 5 )
In fact, alert readers may have sensed that this sample system is composed of a typical three-tier structure: the web pages correspond to the representation tier, the interfaces defined above are associated with the logic tier, and the SQL Server database is related to the data tier. The data access tier (the logic tier) of the whole system mainly lies in two classes: Folder and Mail depicted above, along with an auxiliary class, named WebMailProfile (in fact a struct) to hold the system profile information. And with further digging, you can easily find that the WebMailProfile class is closely connected with two methods (GetWebMailProfile and WebMailProfile) defined in the Mail class. The sketch in Figure 5 gives us a more vivid depiction on the relationships between the modules we'll use:
Figure 5-the sketch shows the relationships between the modules

Here, for brevity, we choose to omit all other methods for there are so many of them (see the downloadable source code for detail). However, I want to single out a representative method, named SaveAsMail of the Mail class, to give the typical implementation of the logic tier above. The following lists the complete source code of the SaveAsMail method:
public int SaveAsMail(string sTitle,string sBody,string sFrom,string sTo,
string sCC,bool bHtmlFormat,int nContain,bool bAttachmentFlag)
{
///open the link to the database
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
SqlCommand myCommand = new SqlCommand("Pr_SaveAsMail",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
///give the parameters of the stored procedure
SqlParameter pTitle = new SqlParameter("@Title",SqlDbType.VarChar,200);
pTitle.Value = sTitle;
myCommand.Parameters.Add(pTitle);
SqlParameter pBody = new SqlParameter("@Body",SqlDbType.Text,2147483647);
pBody.Value = sBody;
myCommand.Parameters.Add(pBody);
SqlParameter pFrom = new SqlParameter("@FromAddress",SqlDbType.Text,2147483647);
pFrom.Value = sFrom;
myCommand.Parameters.Add(pFrom);
SqlParameter pTo = new SqlParameter("@ToAddress",SqlDbType.Text,2147483647);
pTo.Value = sTo;
myCommand.Parameters.Add(pTo);
SqlParameter pCC = new SqlParameter("@CCAddress",SqlDbType.Text,2147483647);
pCC.Value = sCC;
myCommand.Parameters.Add(pCC);
SqlParameter pHtmlFormat = new SqlParameter("@HtmlFormat",SqlDbType.Bit,1);
pHtmlFormat.Value = bHtmlFormat.ToString();
myCommand.Parameters.Add(pHtmlFormat);
SqlParameter pContain = new SqlParameter("@Contain",SqlDbType.Int,4);
pContain.Value = nContain;
myCommand.Parameters.Add(pContain);
SqlParameter pAttachmentFlag = new SqlParameter("@AttachmentFlag",SqlDbType.Bit,1);
pAttachmentFlag.Value = bAttachmentFlag.ToString();
myCommand.Parameters.Add(pAttachmentFlag);
SqlParameter pMailID = new SqlParameter("@MailID",SqlDbType.Int,4);
pMailID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(pMailID);
///define the returned data
int nResult = -1;
try
{
///open the link
myConnection.Open();
///execute the SQL clause
nResult = myCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
///throw exception
throw new Exception(ex.Message,ex);
}
finally
{ ///close the link
myConnection.Close();
}
///return nResult
return(int)myCommand.Parameters[8].Value;
}
In this method, the e-mail that has just been sent out is to be saved in the mailbox. First of all, we create and open a link to the SQL Server database (the linking string is defined in the web.config file). Second, we get ready to invoke the Pr_SaveAsMail stored procedure defined in the previous Database Design section by populating the parameters of the ASP.NET built-in objects SqlCommand and SqlParameter. Third, we execute the SQL clause to perform the real task within a safer try block. And finally, we return some according the method definition. It's a seemingly long, but indeed a typical database operation in the logic tier, isn't it?
So much for the lower development! Starting with the next section, we'll examine the presentation tier design.
Next: Check Your Mailbox List >>
More ASP.NET Articles
More By Xianzhong Zhu