解析Microsoft .NET Pet Shop
Web.config配置代码如下:
<!—当前调用的是SqlServer 的实现类 -->
<add key="WebDAL" value="PetShop.SQLServerDAL" />
<add key="OrdersDAL" value="PetShop.SQLServerDAL" />
BLL层的Item类实现业务逻辑,代码如下:
Item item = new Item(); //调用BLL层的Item类
itemsByProduct = item.GetItemsByProduct(productId); //实现查询方法
BLL层的Item类实现业务逻辑,代码如下:
using System.Collections;
//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;
//PetShop DAL interfaces
using PetShop.IDAL;
namespace PetShop.BLL ...{
/**//// <summary>
/// A business component to manage product items
/// </summary>
public class Item ...{
/**//// <summary>
/// A method to list items by productId
/// Every item is associated with a parent product
/// </summary>
/// <param name="productId">The productId to search by</param>
/// <param name="productName">Name of the product associated with the product</param>
/// <returns>An interface to an arraylist</returns>
public IList GetItemsByProduct(string productId) ...{
// Validate input
if (productId.Trim() == string.Empty)
return null;
// 通过数据访问工厂类创建访问实例
IItem dal = PetShop.DALFactory.Item.Create();
// 实例实现了IDAL接口,调用实例实现查询
return dal.GetItemsByProduct(productId);
}
}
}
数据工厂类根据web.config中的配置创建实例,代码如下:
using System;
using System.Reflection;
using System.Configuration;
namespace PetShop.DALFactory ...{
/**//// <summary>
/// Factory implementaion for the Item DAL object
/// </summary>
public class Item ...{
public static PetShop.IDAL.IItem Create() ...{
/**////查找系统设置
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Item";
// 通过config文件中给定的前缀创建实例
return (PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);
}
}
}
IDAL层IItem 定义了数据访问接口,代码如下:
using System.Collections;
//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.Model;
namespace PetShop.IDAL...{
/**//// <summary>
/// Interface to the Item DAL
/// </summary>
public interface IItem...{
IList GetItemsByProduct(string productId);
ItemInfo GetItem(string itemId);
}
}
SQLServerDAL层实现了IDAL层接口,完成了具体的数据库访问,代码如下:
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using PetShop.Model;
using PetShop.IDAL;
![]()
namespace PetShop.SQLServerDAL ...{
![]()
public class Item : IItem...{
![]()
// Static constants
private const string SQL_SELECT_ITEM = "SELECT Item.ItemId, Item.Attr1, Inventory.Qty, Item.ListPrice, Product.Name, Product.Descn FROM Item INNER JOIN Inventory ON Item.ItemId = Inventory.ItemId INNER JOIN Product ON Item.ProductId = Product.ProductId WHERE Item.ItemId = @ItemId";
private const string SQL_SELECT_ITEMS_BY_PRODUCT = "SELECT ItemId, Attr1, ListPrice, Name FROM Item INNER JOIN Product ON Item.ProductId = Product.ProductId WHERE Item.ProductId = @ProductId";
![]()
private const string PARM_ITEM_ID = "@ItemId";
private const string PARM_PRODUCT_ID = "@ProductId";
![]()
/**//// <summary>
/// Function to get a list of items within a product group
/// </summary>
/// <param name="productId"></param>
/// <returns></returns>
public IList GetItemsByProduct(string productId) ...{
![]()
IList itemsByProduct = new ArrayList();
![]()
SqlParameter parm = new SqlParameter(PARM_PRODUCT_ID, SqlDbType.Char, 10);
parm.Value = productId;
![]()
//Execute the query against the database
using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm)) ...{
// Scroll through the results
while (rdr.Read())...{
ItemInfo item = new ItemInfo(rdr.GetString(0).Trim(), rdr.GetString(1), rdr.GetDecimal(2), rdr.GetString(3), null);
//Add each item to the arraylist
itemsByProduct.Add(item);
}
}
![]()
return itemsByProduct;
}
![]()
![]()
/**//// <summary>
/// Get an individual item based on a the unique key
/// </summary>
/// <param name="itemId">unique key</param>
/// <returns></returns>
public ItemInfo GetItem(string itemId) ...{
![]()
//Set up a return value
ItemInfo item = null;
![]()
//Create a parameter
SqlParameter parm = new SqlParameter(PARM_ITEM_ID, SqlDbType.Char, 10);
//Bind the parameter
parm.Value = itemId;
![]()
//Execute the query
using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_ITEM, parm)) ...{
rdr.Read();
item = new ItemInfo(rdr.GetString(0).Trim(), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5));
![]()
}
return item;
}
}
}
同样,OracleDAL层也实现了IDAL层接口,完成了Oracle的数据库访问。

图3:.NET Pet Shop 3.0 中的数据工厂的实现
这意味着,如果要创建应用程序的 DB2 版本,我们不需要改动业务逻辑层(或者 UI 层)。 创建 DB2 兼容版本的步骤如下:
1.创建 DB2 的数据库访问类,它应该实现 IDAL 接口。
2.将 DB2 访问类编译成一个程序集。
3.测试和部署新的数据程序集到一台运行中的服务器上。
4.更改配置文件,指向新的数据库访问类。
无需更改或重新编译业务逻辑组件。
0
相关文章