Interpretation of ASP.net timetracker Starter Kit (2)--reconstructing article

Source: Internet
Author: User
Tags empty net time tostring oracle database
After looking at asp.net time Tracker Starter Kit's code, asp.net that this program is a good example of learning object-oriented programming. The entire program is functionally a record of the hours that a person participates in a project's work. The analysis concludes that there are mainly personnel, projects, hours recorded in these categories. The entire process is based on these categories. In the business logic layer, the Ttuser (user Class), Project (Project Class) and Timeentry (Hour record Class) respectively include the actions of adding, deleting and modifying the corresponding table, and the various operations on these main objects used in the whole program. The whole program is doing well in the division and function of the class, but I don't think it's ideal in the three-tier architecture division.
Business Logic layer contains a lot of data access layer things, such as: Database connection information, used stored procedures, and so on. And this information I think should be better on the data access layer. As a data access layer, you should provide a way to access the database for the data logic layer, rather than providing only one component (Daab) that simplifies data access. Assuming that Oracle data is used, the changed code needs to be changed a lot, and it is not conducive to future changes.
My idea is to completely separate data-related operations from the data logic layer and provide access to the data access layer. In the design of the data access layer, the method of "writing a base class for the data Access layer" is used to access Daab through the base class. So if you want to transform into an Oracle database, just modify the contents of the Daab. Let's take a look at my improved code to see if there are any problems with the ideas.
Data Access layer base class:
Using System;
Using System.Data;
Data access component (provided by Microsoft)
Using Mystarterkit.ddab;
Namespace MyStarterKit.TimeTracker.DAL
{
<summary>
Summary description of the dalbase.
Data Access Layer Accumulation
</summary>
public class Dalbase
{
Database connection string
private string connstr = system.configuration.configurationsettings.appsettings["ConnectionString"];
Public Dalbase ()
{
}
<summary>
Returns a single value
</summary>
<param name= "CommandText" ></param>
<param name= "ParameterValues" ></param>
<returns></returns>
Protected Object ExecuteScalar (string commandtext, params object[] parametervalues)
{
Return Sqlhelper.executescalar (ConnStr, CommandText, parametervalues);
}
<summary>
Perform no return value operation
</summary>
<param name= "CommandText" ></param>
<param name= "ParameterValues" ></param>
protected void ExecuteNonQuery (string commandtext, params object[] parametervalues)
{
Sqlhelper.executenonquery (ConnStr, CommandText, parametervalues);
}
<summary>
return dataset
</summary>
<param name= "CommandText" ></param>
<param name= "ParameterValues" ></param>
<returns></returns>
Protected DataSet ExecuteDataset (string commandtext, params object[] parametervalues)
{
Return SqlHelper.ExecuteDataset (ConnStr, CommandText, parametervalues);
}
}
}

Data Access Layer code: (For example, project class data access layer code)
Using System;
Using System.Data;
Namespace MyStarterKit.TimeTracker.DAL
{
<summary>
A summary description of Project.
Project information class (data access layer code)
</summary>
public class Project:dalbase
{
Public Project ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Get a list of all items
</summary>
<returns></returns>
Public DataSet getallprojects ()
{
Return base. ExecuteDataset ("tt_listallprojects");
}
<summary>
Get a list of items
</summary>
<param name= "ParameterValues" ></param>
<returns></returns>
Public DataSet getprojects (params object[] parametervalues)
{
Return base. ExecuteDataset ("Tt_listprojects", parametervalues);
}
<summary>
Delete items
</summary>
<param name= "ParameterValues" ></param>
public void Remove (params object[] parametervalues)
{
Base. ExecuteNonQuery ("Tt_deleteproject", parametervalues);
}
<summary>
Update Project
</summary>
<param name= "ParameterValues" ></param>
public void Update (params object[] parametervalues)
{
Base. ExecuteNonQuery ("Tt_updateproject", parametervalues);
}
<summary>
New Items
</summary>
<param name= "ParameterValues" ></param>
<returns> id</returns> of new projects
public int Insert (params object[] parametervalues)
{
return Convert.ToInt32 (base. ExecuteScalar ("Tt_addproject", parametervalues));
}
}
}
Business Logic Layer Code: (Take the project class business logic layer code as an example)
<summary>
Obtain a list of items that a specified user can view, based on user and user roles
</summary>
<param name= "UserID" ></param>
<param name= "Role" ></param>
<returns></returns>
public static projectscollection getprojects (int userID, string role)
{
String firstName = String. Empty;
String lastName = String. Empty;
To create a data access Layer class
DAL. Project Project = new DAL. Project ();
Calling the data Access layer method
DataSet ds = Project. Getprojects (UserID, Convert.ToInt32 (role));
Projectscollection projects = new Projectscollection ();
foreach (DataRow R in DS. Tables[0]. Rows)
{
Project PRJ = new Project ();
Prj. ProjectID = Convert.ToInt32 (r["ProjectID"));
Prj. Name = r["ProjectName"]. ToString ();
Prj. Description = r["Description"]. ToString ();
Prj. Manageruserid = Convert.ToInt32 (r["Manageruserid"));
Prj. Managerusername = Ttuser.getdisplayname (convert.tostring (r["UserName")), ref firstName, ref lastName);
Prj. Estcompletiondate = Convert.todatetime (r["estcompletiondate"));
Prj. Estduration = Convert.todecimal (r["estduration"));
Projects. ADD (PRJ);
}
return projects;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.