PetShop项目学习笔记(二)

今天计划学习一下PetShop中的工厂模式。PetShop中的工厂模式是工厂方法模式,

还是按照原来的顺序看:NavigationControl.ascx进入,查看Page_Load事件中重点查看BindCategories()方法。

View Code
1 private void BindCategories() {
2 Category category = new Category();
3 repCategories.DataSource = category.GetCategories();
4 repCategories.DataBind();
5 }

第二句定义了一个Category对象category,主意这里引用的是BLL程序集中的Category,通过GetCategories()方法返回了IList<CategoryInfo>数组作为数据源。这里有两个类BLL.Category和Model.CategoryInfo,前者建立了对象关系映射,后者仅仅是一个对象类,记录了对象的一些属性信息。

重点看BLL.Category类开始定义的代码,BLL(A business component to manage categories业务逻辑),对OracleDAL的类做一次包装:

 private static readonly ICategory dal = PetShop.DALFactory.DataAccess.CreateCategory();

这里通过DALFactory工作空间下的类DataAccess的方法CreateCategory()创建了一个ICatefory实例,这个Category类是OracleDAL程序集中的。通过查看类DataAccess的CreateCategory()方法可以看到这一点:

1 private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
2 private static readonly string orderPath = ConfigurationManager.AppSettings["OrdersDAL"];
View Code
1 public static PetShop.IDAL.ICategory CreateCategory() {
2 string className = path + ".Category";
3 return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
4 }

可以看到这里通过读取OrdersDAL字符串,根据字符串创建了一个OrdersDAL程序集下的Category实例。这里DataAccess类的作用主要就是创建了实例。
下面分析程序的架构。

文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流,转载请注明出处。
原文地址:https://www.cnblogs.com/yhlx125/p/2395458.html