技术开发 频道

ASP.NET2.0:AdventureWorks贸易系统


4. 实现ProductCategory对象


    除了数据访问和业务逻辑层组件之外,本实例还包括另一个名为AdventureWorksTraderEntities的项目,该项目公开了一组直接对应数据库中实体的对象。ProductCategory表对应的是ProductCategory类(如示例3所示),该类可作为容器存储与ProductCategory有关的数据。

    示例3:实现ProductCategory类
using System; 
using System.Collections.Generic;
using System.Text;
namespace AdventureWorksTraderEntities
{
[Serializable]
public class ProductCategory
{
private int _productCategoryID;
private string _name;
private Guid _rowguid;
private DateTime _modifiedDate;

public int ProductCategoryID
{
get { return _productCategoryID; }
set { _productCategoryID = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public Guid Rowguid
{
get { return _rowguid; }
set { _rowguid = value; }
}

public DateTime ModifiedDate
{
get { return _modifiedDate; }
set { _modifiedDate = value; }
}
}
}
    正如看到的,ProductCategory类如同一个占位符,每个属性都直接对应ProductCategory表中的一个列。

使用泛型集合

    读者应该记得GetProductCategories()方法的返回值,该值是ProductCategory对象的泛型集合。以下代码段专用于泛型集合:
while (reader.Read()) 
{
ProductCategory temp = new ProductCategory();
ProductCategory category = (ProductCategory)
DataAccessHelper.PopulateEntity(temp, mappings, reader);
list.Add(category);
}
    在以上代码中,首先,循环整个SqlDataReader对象,然后,将SqlDataReader对象的每一行都转换为一个ProductCategory对象,最后,将ProductCategory对象添加到泛型集合中。

    使用泛型可使得软件组件中的代码更加具有可重用性。泛型是一种数据结构类型,其中包含的代码保持不变。参数的数据类型能够在使用时变化。根据所提供的变量,使用数据结构能够适应不同的数据类型。每次使用泛型时,可自定义不同的数据类型,而无需重写任何内部代码。泛型允许类、结构、接口、委托和方法,将它们所存储和操作的数据类型实现参数化。

    在ASP.NET页面中,GetProductCategories()返回的泛型集合直接绑定到ObjectDataSource控件。当随后讲解本实例Web站点实现时将看到这些内容。

5. 实现ProductSubcategoryDB类

    ProductSubcategoryDB类包括GetProductSubcategories()方法,该方法根据产品类别返回所属所有子类别的列表。这个方法的实现非常类似于ProductCategory.GetProductCategories()。示例4列举了ProductSubcategoryDB类的实现代码。

    示例4:实现ProductSubcategoryDB类
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using AdventureWorksTraderEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace AdventureWorksTraderDataAccess
{
public class ProductSubcategoryDB
{
private DataColumnMapping[] mappings = new DataColumnMapping[] {
new DataColumnMapping("ProductSubcategoryID","ProductSubcategoryID"),
new DataColumnMapping("ProductCategoryID","ProductCategoryID"),
new DataColumnMapping("Name","Name"),
new DataColumnMapping("rowguid","Rowguid"),
new DataColumnMapping("ModifiedDate","ModifiedDate")};

public IList<ProductSubcategory> GetProductSubCategories(int productCategoryID)
{

IList<ProductSubcategory> list = new List<ProductSubcategory>();
Database db = DatabaseFactory.CreateDatabase();
string storedProcedureName = "GetProductSubcategories";
DbCommand dbCommand = db.GetStoredProcCommand(storedProcedureName);
db.AddInParameter(dbCommand, "productCategoryID", DbType.Int32, productCategoryID);
using (IDataReader reader = db.ExecuteReader(dbCommand))
{
while (reader.Read())
{
ProductSubcategory temp = new ProductSubcategory();
ProductSubcategory subCategory = (ProductSubcategory)DataAccessHelper.PopulateEntity(temp, mappings, reader);
list.Add(subCategory);
}
}
return list;
}
}
}
    正如在示例5看到的,GetProductSubcategories()方法执行了GetProductSubcategories存储过程。该存储过程接受productCategoryID参数。此后的实现与GetProductCategories()方法相同。
0
相关文章