生产环境VS2008+SQL2005
模仿PETSHOP4.0架构模式,自己写了一个中小公司网站(不过没有应用membership 和Cache)把开发过程写下来(仅供参考,如果哪位老鸟能够留言指点错误,不胜感激)PETSHOP4.0产品学习的优点:面向对象编程1:方便使用不同的数据库2:扩展功能模块更容易实现
一:数据库设计图
字段解释
Admin表:管理员用户,密码
Company表:公司介绍,联系方式
Links表:友情链接名称,URL地址
ProductsClass表:产品分类名称
Products表:产品名称,描述,图片文件名,是否推荐产品,分类ID
News表:新闻标题,内容,日期,来源
Solutions表:解决方案标题,内容
Jobs表:招聘标题,内容,日期
二:类库设计
1:DBHelper 对数据访问的简单封装 有SQLHelper.cs和OracleHelper.cs
贴出自己用的SQLHelper.cs
Code
1public class SQLHelper
2 {
3 //连接字符串
4 private static string dbConnectionStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
5 /**//// <summary>
6 /// 创建command对象,指定command类型为存储过程
7 /// </summary>
8 /// <returns></returns>
9 public static SqlCommand CreateCommand()
10 {
11 SqlConnection conn = new SqlConnection(dbConnectionStr);
12 SqlCommand comm = conn.CreateCommand();
13 comm.CommandType = CommandType.StoredProcedure;//代码看起来更清爽,所有SQL语句写在存储过程
14 return comm;
15 }
16 /**//// <summary>
17 /// 执行查询
18 /// </summary>
19 /// <param name="comm"></param>
20 /// <returns></returns>
21 public static SqlDataReader ExcuteReader(SqlCommand comm)
22 {
23 try
24 {
25 comm.Connection.Open();
26 SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
27 //comm.Parameters.Clear();
28 return dr;
29
30 }
31 catch (Exception ex)
32 {
33 comm.Connection.Close();
34 throw new Exception(ex.Message);
35 }
36 }
37 /**//// <summary>
38 /// 执行Insert Update Delete操作
39 /// </summary>
40 /// <param name="comm"></param>
41 /// <returns></returns>
42 public static int ExcuteNonQuery(SqlCommand comm)
43 {
44 int rows = -1;//受影响的行
45 try
46 {
47 comm.Connection.Open();
48 rows = comm.ExecuteNonQuery();
49
50 }
51 catch (Exception ex)
52 {
53 throw new Exception(ex.Message);
54 }
55 finally
56 {
57 comm.Connection.Close();
58 }
59 return rows;
60 }
61 /**//// <summary>
62 /// return第一行第一列值
63 /// </summary>
64 /// <param name="comm"></param>
65 /// <returns></returns>
66 public static object ExcuteScalar(SqlCommand comm)
67 {
68 object value = null;
69 try
70 {
71 comm.Connection.Open();
72 value = comm.ExecuteScalar();
73
74 }
75 catch (Exception ex)
76 {
77 throw new Exception(ex.Message);
78 }
79 finally { comm.Connection.Close(); }
80 return value;
81 }
82 }
83}
2:Model 相关实体类,对应每一个数据表,对字段的读取和写入的载体(命名习惯 类名等于各数据表名)
3:IDAL 数据访问的接口,定义对数据表操作的所有方法(命名习惯 接口名等于I+各数据表名 如:IProducts.cs)
需添加引用Model项目
贴出Iproducts.cs代码
Code
1 namespace IDAL
2 {
3 public interface IProducts
4 {
5 /// <summary>
6 /// 显示所有产品
7 /// </summary>
8 /// <param name="pageIndex"></param>
9 /// <param name="pageSize"></param>
10 /// <param name="productsCount"></param>
11 /// <returns></returns>
12 IList<Products> GetProducts(int pageIndex,int pageSize, out int productsCount);
13 /// <summary>
14 /// 显示推荐产品
15 /// </summary>
16 /// <param name="pageIndex"></param>
17 /// <param name="pageSize"></param>
18 /// <param name="productsCount"></param>
19 /// <returns></returns>
20 IList<Products> GetProductsOnPromotion(int pageIndex, int pageSize, out int productsCount);
21 /// <summary>
22 /// 显示分类下产品
23 /// </summary>
24 /// <param name="pageIndex"></param>
25 /// <param name="pageSize"></param>
26 /// <param name="productsCount"></param>
27 /// <param name="ClassId"></param>
28 /// <returns></returns>
29 IList<Products> GetProductsInClass(int pageIndex, int pageSize,out int productsCount, int ClassId);
30 /// <summary>
31 /// 显示单个产品
32 /// </summary>
33 /// <param name="productId"></param>
34 /// <returns></returns>
35 Products GetSingleProduct(int productId);
36 /// <summary>
37 /// 添加
38 /// </summary>
39 /// <param name="products"></param>
40 bool AddProduct(Products products);
41 /// <summary>
42 /// 修改
43 /// </summary>
44 /// <param name="products"></param>
45 bool ModProduct(Products products);
46 /// <summary>
47 /// 删除
48 /// </summary>
49 /// <param name="ProductId"></param>
50 bool DelProduct(int productId);
51 }
52 }
4:SQLServeDAL 和OracleDAL 二种数据库类型相关数据访问层,继承IDAL(命名习惯 各数据表名+SQL 如ProductsSQL.cs) 需添加引用项目IDAL,Model和DBHelper
贴出ProductsSQL.cs代码
Code
1 namespace SQLServerDAL
2 {
3 class ProductsSQL:IDAL.IProducts//实现接口IProducts
4 {
5 /// <summary>
6 /// 显示所有产品
7 /// </summary>
8 /// <param name="pageIndex"></param>
9 /// <param name="pageSize"></param>
10 /// <param name="productsCount"></param>
11 /// <returns></returns>
12 public IList<Products> GetProducts(int pageIndex, int pageSize,out int productsCount)
13 {
14 IList<Products> ProductsList = new List<Products>();
15 SqlCommand comm = SQLHelper.CreateCommand();
16 comm.CommandText = "GetProducts";//存储过程名
17 SqlParameter param = comm.CreateParameter();
18 param.ParameterName = "@PageIndex";
19 param.Value = pageIndex;
20 param.DbType = DbType.Int32;
21 comm.Parameters.Add(param);
22
23 param = comm.CreateParameter();
24 param.ParameterName = "@PageSize";
25 param.Value = pageSize;
26 param.DbType = DbType.Int32;
27 comm.Parameters.Add(param);
28
29 param = comm.CreateParameter();
30 param.ParameterName = "@ProductsCount";
31 param.DbType = DbType.Int32;
32 param.Direction = ParameterDirection.Output;
33 comm.Parameters.Add(param);
34
35 using (SqlDataReader dr = SQLHelper.ExcuteReader(comm))
36 {
37
38 while (dr.Read())
39 {
40 Products products = new Products();
41 products.ProductId = Int32.Parse(dr["ProductId"].ToString());
42 products.ProductName = dr["ProductName"].ToString();
43 products.ProductDescription = dr["ProductDescription"].ToString();
44 products.ProductImage = dr["ProductImage"].ToString();
45 products.IsPromotion = bool.Parse(dr["IsPromotion"].ToString());
46 products.ClassId = Int32.Parse(dr["ClassId"].ToString());
47 products.ClassName = dr["ClassName"].ToString();
48 ProductsList.Add(products);
49 products = null;
50 }
51
52 }
53 productsCount = Int32.Parse(comm.Parameters["@ProductsCount"].Value.ToString());//小贴士:对output或者value参数的访问,需要在DataReader Close之后才可以,如果这段代码写在using里面的话,一般会有错误提示:"未将对象引用设置到对象的实例"
54 return ProductsList;
55 }
56 .略实现接口中定义的所有方法
5:DALFactory 创建对象工程类,创建IDAL所有接口的实例,此项目下只有DataAccess.cs类.需添加引用项目IDAL
代码片段:namespace DALFactory
{
public class DataAccess
{
//<add key="WebDAL" value="SQLServerDAL"/>用到的数据访问程序集
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
//创建IProducts接口的实例
public static IDAL.IProducts CreateProducts()
{
string className = path + ".ProductsSQL";
return (IDAL.IProducts)Assembly.Load(path).CreateInstance(className);
//Assembly.Load("程序集名称").CreateInstance("命称空间.类名称")
}
6:BLL层 业务逻辑层(命名习惯 各数据表名+Sys 如ProductsSys.cs)
需添加引用项目DALFactory,IDAL和Model
贴出ProductsSys.cs代码:
Code
namespace BLL
{
public class ProductsSys
{
private static readonly IProducts dal = DataAccess.CreateProducts();//接口实例
/// <summary>
/// 显示所有产品
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="productsCount"></param>
/// <returns></returns>
public static IList<Products> GetProducts(int pageIndex, int pageSize, out int productsCount)
{
return dal.GetProducts(pageIndex, pageSize, out productsCount);
}
7:WEB和Utility就不多说了.
总结:面向接口编程,BLL和DAL 层层之间通过接口调用.不同的数据库类型访问层都实现了这样的接口.即使将来更换数据库,或修改数据访问层的实现.BLL层也不需要更改.
公司网站源代码http://files.cnblogs.com/freegarden/CMS.rar