人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

August 17, 2006

PetShop数据访问架构

Filed under: 代码学习

–by Aloneplayer 2006.08.17

1. IDAL
定义了各个数据实体的数据访问层的interface
如Product,对应一个
public interface IProduct
{
IList<ProductInfo> GetProductsByCategory(string category);

IList<ProductInfo> GetProductsBySearch(string[] keywords);

ProductInfo GetProduct(string productId);
}

2. Model
定义了各个数据实体类,如:
[Serializable]
public class ProductInfo
{

private string id;
private string name;
private string description;
private string image;
private string categoryId;
}

3. BLL
定义了使用数据一些函数,这些函数封装了对数据访问层函数的使用.
public class Product
{
private static readonly IProduct dal = PetShop.DALFactory.DataAccess.CreateProduct();

IList<ProductInfo> GetProductsByCategory(string category)
{
return dal.GetProductsByCategory(category);
}

IList<ProductInfo> GetProductsBySearch(string[] keywords);

ProductInfo GetProduct(string productId);
}

4. SQLServerDAL模块中定义了各个数据实体对应的数据访问层对象
每个对象都实现了IDAL层的对应Interface,如
public class Product : IProduct
{

}
每个数据访问层对象都依赖于一个封装了数据库操作的工具类,比如SQLHelper和OracleHelper,来完成对数
据库的操作.

5. DALFactory此模块中只有一个class,负责根据web.config来加载各个数据访问层对象

public sealed class DataAccess
{
private static readonly string path = ConfigurationManager.AppSettings[”WebDAL”];

public static PetShop.IDAL.IProduct CreateProduct()
{
string className = path + “.Product”;
return (PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
}

}

在web.config中WebDAL对应了当前使用了数据访问层对象所在的模块,
<add key=”WebDAL” value=”PetShop.SQLServerDAL”/>
显然,要使用其他数据库,只需要修改这个配置.

>>整个体系由数据实体(model),数据逻辑层(BLL),数据访问层构成(DAL),BLL依赖于DAL,由于数据访问层的不确定性
(可能是sql,也可能是orcal),故而将DAL拆分为IDAL和DAL,使BLL依赖于IDAL,DAL实现IDAL.
同时使用DALFactory根据配置动态生成所需DAL.

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/08/17/p149/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham