当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET开发电子商务网站学习经验

ASP.NET
二级域名Cookie问题的解决方法
如何为asp.net网站项目添加子项目
asp.net用url重写URLReWriter实现任意二级域名
asp.net 序列化and反序列化演示
asp.net Timer的使用方法
AjaxControlToolKit DropDownExtender(下拉扩展控件)使用方法
AjaxControlToolKit CalendarExtender(日历扩展控件)的使用方法
让GridView只显示特定用户的数据的方法
让GridView只更新某些特定的数据的方法
ajaxControlToolkit中CascadingDropDown的用法说明
axp.net ScriptManager的简单用法
把程序集安装到全局程序集缓存中的步骤
引用全局程序集缓存内的程序集的方法
asp.net COOKIES需要注意的一点
asp.net UrlReWriter使用经验小结
页面导出为Excel的时间格式的问题
asp.net cookie清除的代码
Asp.net XMLHTTP封装类(GET,Post发送和接收数据)
ASP.NET XmlHttp跨域访问实现代码
Asp.NET 随机码生成基类(随机字母,随机数字,随机字母+数字)

ASP.NET开发电子商务网站学习经验


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-04   浏览: 270 ::
收藏到网摘: n/a

前些阵子照着《Pro ASP.NET 2.0 E-Commerce in C#2005》书编辑了一个商务系统网站,想总结一下学习到的所学的知识。

该网站具有一般商务网站的特征

这里先讲讲他的框架

数据访问层

用的的存储过程操作数据库的存储,有一个Shop.DataAccess类库专门(注意我这里将原文的命名空间改为shop了)

该类库使用了一个组件来封装对数据库的操作 为 Microsoft Data Access Application Block, 其实就是将SQLHelper.cs复制到该类下就行了,该类可以自动管理存储过程的连接,参数和名称。

类库下的DataAccessBase类是一个基类,该类库几乎所有的类都会继承它,有两个属性一个是存储过程,以及返回数据库的连接字符串

注意:这里是从web.config文件中获取与数据库连接的字符串,但是在类中无法引用到Configuration类,所以我们要额外的添加引用System.Configuration.dll程序集

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Shop.DataAccess
{
    
public class DataAccessBase
    {
        
//存储过程的名称
        protected string StoredprocedureName { setget; }

        
//获得连接字符串
        protected string ConnectionString
        {
            
get
            {
                
return ConfigurationManager.ConnectionStrings["db_shopConnectionString"].ToString();
            }
        }
    }

}

类库中的StoreProcedure类

利用枚举存储编写的存储过程名称,这样便于更改及管理

但是对于存储过程很多,一个类来存储肯定显得不够,个人建议在细分,控制一个类中的存储过程不超过20个

例如:

StoreProcedure_User,StoreProcedure_Product,StoreProcedure_Orders

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shop.DataAccess
{
    
public class StoredProcedure
    {
        
public enum Name
        {
            ProductByID_Select,
            Products_Select,
            Products_SelectSerach,
            ShoppingCart_Select,
            ShoppingCart_Insert,
            ShoppingCart_Update,
            ShoppingCart_Delete,
            EndUser_Insert,
            EndUserLogin_Select,
            Address_Select,
            ContactInformation_Select,
            AdminLogin_Select,
            Product_Insert,
            ProductCategory_Select,
            Product_Update,
            Orders_Select,
            OrderDetails_Select,
            OrderAll_Select,
            OrderStatus_Select,
            OrdersByID_Select,
            Orders_Update,
            ProductPromotion_Select
        }
    }
}

类库中的DataBaseHelper类

重新包装了SQLHelper类的功能,因为并不是所有SQLHelper类功能我们都会用到,我们只应用到了一小部分

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace Shop.DataAccess
{
    
public class DataBaseHelper:DataAccessBase
    {
        
public  SqlParameter[] Parameters { getset; }

        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="storedproceducename">赋值存储过程</param>
        public DataBaseHelper(string storedproceducename)
        {
            
this.StoredprocedureName = storedproceducename;
        }

        
/// <summary>
        
/// 无数据返回
        
/// </summary>
        
/// <param name="transaction"></param>
        public void Run(SqlTransaction transaction)
        {
            SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, 
this.StoredprocedureName, this.Parameters);
        }

        
/// <summary>
        
/// 无数据返回,自己提供参数
        
/// </summary>
        
/// <param name="transaction"></param>
        
/// <param name="Parameters">参数</param>
        public void Run(SqlTransaction transaction, SqlParameter[] Parameters)
        {
            SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, 
this.StoredprocedureName, Parameters);
        }

        
/// <summary>
        
/// 有参数返回(DataSet)
        
/// </summary>
        
/// <param name="connectionstring"></param>
        
/// <param name="parameters"></param>
        
/// <returns></returns>
        public DataSet Run(string connectionstring, SqlParameter[] parameters)
        {
            DataSet ds;
            ds 
= SqlHelper.ExecuteDataset(connectionstring, this.StoredprocedureName, parameters);
            
return ds;
        }

        
/// <summary>
        
/// 返回第一行第一列的值
        
/// </summary>
        
/// <param name="connectionstring"></param>
        
/// <param name="parameters"></param>
        
/// <returns></returns>
        public object RunScalar(string connectionstring, SqlParameter[] parameters)
        {
            
object obj;
            obj 
= SqlHelper.ExecuteScalar(connectionstring, this.StoredprocedureName, parameters);
            
return obj;
        }

        
/// <summary>
        
/// 返回第一行第一列的值
        
/// </summary>
        
/// <param name="transaction"></param>
        
/// <param name="parameters"></param>
        
/// <returns></returns>
        public object RunScalar(SqlTransaction transaction, SqlParameter[] parameters)
        {
            
object obj;
            obj 
= SqlHelper.ExecuteScalar(transaction, this.StoredprocedureName, parameters);
            
return obj;
        }

        
/// <summary>
        
/// 无参数返回(DataSet)
        
/// </summary>
        
/// <param name="connectionstring"></param>
        
/// <returns></returns>
        public DataSet Run(string connectionstring)
        {
            DataSet ds;
            ds 
= SqlHelper.ExecuteDataset(connectionstring, CommandType.StoredProcedure, this.StoredprocedureName);
            
return ds;
        }

        
/// <summary>
        
/// 无返回值
        
/// </summary>
        public void Run()
        {
            SqlHelper.ExecuteNonQuery(
base.ConnectionString, CommandType.StoredProcedure, this.StoredprocedureName, this.Parameters);
        }

        
/// <summary>
        
/// 返回是SqlDataReader
        
/// </summary>
        
/// <param name="parameters"></param>
        
/// <returns></returns>
        public SqlDataReader Run(SqlParameter[] parameters)
        {
            SqlDataReader dr;
            dr 
= SqlHelper.ExecuteReader(base.ConnectionString, CommandType.StoredProcedure, this.StoredprocedureName, parameters);
            
return dr;
        }
    }
}

业务逻辑层

Shop.BusinessLogic

这里涉及到要编写接口

首先了解下什么是接口,接口就是只包含抽象成员的应用类型,接口中的成员可以是方法,索引器,属性和事件,而不可以是包含任何常量,构造函数,静态成员或数据字段。接口只包含这些成员的声明。而具体的实现必须从实现该接口的任何类中进行初始化。

这里编辑一个接口是因为所有该类库的类都会调用一个函数来实现获得数据

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shop.BusinessLogic
{
    
interface IBusinessLogic
    {
        
void Invoke();
    }
}

Shop.Common类库是编写一个公共类

Shop.Operational自定义一些类

具体的一些内容留在下面文章分析