petshop中工厂模式中的应用
来源:互联网 发布:知乎怎么邀请别人回答 编辑:程序博客网 时间:2024/04/29 08:54
petshop 4。0是个好东西,是个微软出的DEMO,里面用了了如工厂模式,策略模式,MSMQ和大量的asp.net 2.0新特性,
下面简要小结下其中的工厂模式的应用,可以在平常的项目中用到
首先,petshop提供了sql server和oracle的实现类,其中的petshop.IDAL是访问的接口,其中用到了泛型,比如IProduct接口
public interface IProduct{
/// <summary>
/// Method to search products by category name
/// </summary>
/// <param name="category">Name of the category to search by</param>
/// <returns>Interface to Model Collection Generic of search results</returns>
IList<ProductInfo> GetProductsByCategory(string category);
/// <summary>
/// Method to search products by a set of keyword
/// </summary>
/// <param name="keywords">An array of keywords to search by</param>
/// <returns>Interface to Model Collection Generic of search results</returns>
IList<ProductInfo> GetProductsBySearch(string[] keywords);
/// <summary>
/// Query for a product
/// </summary>
/// <param name="productId">Product Id</param>
/// <returns>Interface to Model ProductInfo for requested product</returns>
ProductInfo GetProduct(string productId);
}
之后在SQLServerDAL中实现这些接口,这里就不说了,可以看代码。而数据访问工厂类是放在DALFactory包中的DataAccess类完成,其中代码为
public sealed class DataAccess {
// Look up the DAL implementation we should be using
private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];
private DataAccess() { }
public static PetShop.IDAL.ICategory CreateCategory() {
//方法路径
string className = path + ".Category";
//利用反射加载数据库访问处理类,同时创建接口实例
return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
}
这里的ConfigurationManager.AppSettings["WebDAL"];
是在web.config文件中定义的,即
<add key="WebDAL" value="PetShop.SQLServerDAL"/>,即指定实现类为采用SQL SERVER方式实现
之后利用反射加载数据库访问处理类,同时创建接口实例,这里注意强制转型为Category类型的实例
以上这个模式,可以用来在。NET中做工厂方法使用的一个例子和最佳实践
- petshop中工厂模式中的应用
- petshop中工厂模式中的应用
- petshop中工厂模式中的应用
- 工厂模式---mybatis中的应用
- petshop模式中的更新方法
- 工厂模式在数据库访问中的应用
- Remoting中的工厂模式的应用
- 工厂模式及在项目中的应用
- 工厂模式与抽象工厂在实际项目中的应用
- 在Java中应用工厂(Factory)模式
- 解析Petshop 3.0 简单工厂(Simple Factory)模式
- 设计模式中简单工厂模式的应用----java
- C# 反射在工厂设计模式中的应用
- 浅析Command模式,反射工厂在系统中的应用
- 设计模式:工厂在收费系统中的应用
- 设计模式在游戏中的应用--工厂方法(五)
- 反射技术在工厂设计模式中的应用
- springmvc3.0 工厂模式 在controller 中的应用
- 一段删除某个数据库下所有数据的好脚本
- 看盘ABC 欧奈尔:CANSLIM法则
- asp.net 2.0 中用好delete功能
- 一段javascript实现缩略图的好代码
- (转)看盘ABC:相对强弱指标(RSI)
- petshop中工厂模式中的应用
- (转)关于委托的精彩解说
- 看盘ABC:平滑异同移动平均线(MACD)
- sqlhelper简单小结
- asp.net 2.0的gridview基本知识
- IBM DW上关于SOA的规范归纳
- (转)Web 服务提示与技巧: JAX-RPC 与 JAX-WS 的比较
- 软件工程心理学之5--提前感知用户的需求
- smarty学习2