滑兔

博客园 首页 新随笔 联系 订阅 管理

第一篇中,用CodeSmith生成的业务对象是继承自一个IBizObject接口的,该接口实现一般业务对象的常见操作,以便在界面层中的页面基类中处理基本的业务对象操作,如删除,数据绑定等。IBizObject是继承自ICollection的接口,实现该接口的目的是让DataGrid绑定时能够正常实现分页功能。
以下是IBizObject的代码:
 public interface IBizObject : ICollection
 {
  /// <summary>
  /// 业务对象的数据表格
  /// </summary>
  DataTable Source { get; }

  /// <summary>
  /// 业务对象集合名称
  /// </summary>
  string Name { get; }

  /// <summary>
  /// 该业务对象是否保存在缓存中
  /// </summary>
  bool IsCached { get; }

  /// <summary>
  /// 初始化数据表格
  /// </summary>
  void InitTable();

  /// <summary>
  /// 删除集合中的第N个业务对象
  /// </summary>
  void Remove(int index);

  /// <summary>
  /// 移除集合中所有对象,并可更新到数据库中
  /// </summary>
  void RemoveAll();

  /// <summary>
  /// 清楚集合中的所有业务对象
  /// </summary>
  void Clear();

  /// <summary>
  /// 载入表格数据,并转为业务对象集合
  /// </summary>
  void Load();

  /// <summary>
  /// 根据过滤条件载入表格数据,并转为业务对象集合
  /// </summary>
  /// <param name="filter">过滤条件</param>
  void Load(string filter);

  /// <summary>
  /// 将业务对象集合内对象的更新保存到数据库中
  /// </summary>
  void Save();
 }

利用该模板生成的代码(使用Northwind数据库中的Customers表格生成)如下:
 /// <summary>
 /// 数据库表格 Customers 的集合类 CustomerCollection
 /// </summary>
 [Serializable()]
 public class CustomerCollection : IBizObject
 {
  #region 变量定义
  
  private DataTable table = null;
 
  #endregion
  
  #region 构造函数
  
  /// <summary>
  /// 构造函数
  /// </summary>
  public CustomerCollection()
  {
   InitTable();
  }
  
  #endregion
  
  #region 方法
   
  /// <summary>
  /// 初始化表格对象
  /// </summary>
  public void InitTable()
  {
   table = new DataTable("Customers");
   
   DataColumn colCustomerID = new DataColumn("CustomerID", typeof(string), "", MappingType.Element);
   colCustomerID.AllowDBNull = false;
   colCustomerID.Caption = "Customer ID";
   colCustomerID.Unique = true;
   colCustomerID.DefaultValue = Convert.DBNull;
   colCustomerID.ExtendedProperties.Add("IsKey", "true");
   colCustomerID.ExtendedProperties.Add("ReadOnly", "false");
   colCustomerID.ExtendedProperties.Add("Description", "Customer ID");
   colCustomerID.ExtendedProperties.Add("Decimals", "0");
   colCustomerID.ExtendedProperties.Add("AllowDBNulls", "false");
   table.Columns.Add(colCustomerID);
   
   DataColumn colCompanyName = new DataColumn("CompanyName", typeof(string), "", MappingType.Element);
   colCompanyName.AllowDBNull = false;
   colCompanyName.Caption = "Company Name";
   colCompanyName.MaxLength = 40;
   colCompanyName.Unique = false;
   colCompanyName.DefaultValue = Convert.DBNull;
   colCompanyName.ExtendedProperties.Add("IsKey", "false");
   colCompanyName.ExtendedProperties.Add("ReadOnly", "false");
   colCompanyName.ExtendedProperties.Add("Description", "Company Name");
   colCompanyName.ExtendedProperties.Add("Length", "40");
   colCompanyName.ExtendedProperties.Add("Decimals", "0");
   colCompanyName.ExtendedProperties.Add("AllowDBNulls", "false");
   table.Columns.Add(colCompanyName);
   
   DataColumn colContactName = new DataColumn("ContactName", typeof(string), "", MappingType.Element);
   colContactName.AllowDBNull = true;
   colContactName.Caption = "Contact Name";
   colContactName.MaxLength = 30;
   colContactName.Unique = false;
   colContactName.DefaultValue = Convert.DBNull;
   colContactName.ExtendedProperties.Add("IsKey", "false");
   colContactName.ExtendedProperties.Add("ReadOnly", "false");
   colContactName.ExtendedProperties.Add("Description", "Contact Name");
   colContactName.ExtendedProperties.Add("Length", "30");
   colContactName.ExtendedProperties.Add("Decimals", "0");
   colContactName.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colContactName);
   
   DataColumn colContactTitle = new DataColumn("ContactTitle", typeof(string), "", MappingType.Element);
   colContactTitle.AllowDBNull = true;
   colContactTitle.Caption = "Contact Title";
   colContactTitle.MaxLength = 30;
   colContactTitle.Unique = false;
   colContactTitle.DefaultValue = Convert.DBNull;
   colContactTitle.ExtendedProperties.Add("IsKey", "false");
   colContactTitle.ExtendedProperties.Add("ReadOnly", "false");
   colContactTitle.ExtendedProperties.Add("Description", "Contact Title");
   colContactTitle.ExtendedProperties.Add("Length", "30");
   colContactTitle.ExtendedProperties.Add("Decimals", "0");
   colContactTitle.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colContactTitle);
   
   DataColumn colAddress = new DataColumn("Address", typeof(string), "", MappingType.Element);
   colAddress.AllowDBNull = true;
   colAddress.Caption = "Address";
   colAddress.MaxLength = 60;
   colAddress.Unique = false;
   colAddress.DefaultValue = Convert.DBNull;
   colAddress.ExtendedProperties.Add("IsKey", "false");
   colAddress.ExtendedProperties.Add("ReadOnly", "false");
   colAddress.ExtendedProperties.Add("Description", "Address");
   colAddress.ExtendedProperties.Add("Length", "60");
   colAddress.ExtendedProperties.Add("Decimals", "0");
   colAddress.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colAddress);
   
   DataColumn colCity = new DataColumn("City", typeof(string), "", MappingType.Element);
   colCity.AllowDBNull = true;
   colCity.Caption = "City";
   colCity.MaxLength = 15;
   colCity.Unique = false;
   colCity.DefaultValue = Convert.DBNull;
   colCity.ExtendedProperties.Add("IsKey", "false");
   colCity.ExtendedProperties.Add("ReadOnly", "false");
   colCity.ExtendedProperties.Add("Description", "City");
   colCity.ExtendedProperties.Add("Length", "15");
   colCity.ExtendedProperties.Add("Decimals", "0");
   colCity.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colCity);
   
   DataColumn colRegion = new DataColumn("Region", typeof(string), "", MappingType.Element);
   colRegion.AllowDBNull = true;
   colRegion.Caption = "Region";
   colRegion.MaxLength = 15;
   colRegion.Unique = false;
   colRegion.DefaultValue = Convert.DBNull;
   colRegion.ExtendedProperties.Add("IsKey", "false");
   colRegion.ExtendedProperties.Add("ReadOnly", "false");
   colRegion.ExtendedProperties.Add("Description", "Region");
   colRegion.ExtendedProperties.Add("Length", "15");
   colRegion.ExtendedProperties.Add("Decimals", "0");
   colRegion.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colRegion);
   
   DataColumn colPostalCode = new DataColumn("PostalCode", typeof(string), "", MappingType.Element);
   colPostalCode.AllowDBNull = true;
   colPostalCode.Caption = "Postal Code";
   colPostalCode.MaxLength = 10;
   colPostalCode.Unique = false;
   colPostalCode.DefaultValue = Convert.DBNull;
   colPostalCode.ExtendedProperties.Add("IsKey", "false");
   colPostalCode.ExtendedProperties.Add("ReadOnly", "false");
   colPostalCode.ExtendedProperties.Add("Description", "Postal Code");
   colPostalCode.ExtendedProperties.Add("Length", "10");
   colPostalCode.ExtendedProperties.Add("Decimals", "0");
   colPostalCode.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colPostalCode);
   
   DataColumn colCountry = new DataColumn("Country", typeof(string), "", MappingType.Element);
   colCountry.AllowDBNull = true;
   colCountry.Caption = "Country";
   colCountry.MaxLength = 15;
   colCountry.Unique = false;
   colCountry.DefaultValue = Convert.DBNull;
   colCountry.ExtendedProperties.Add("IsKey", "false");
   colCountry.ExtendedProperties.Add("ReadOnly", "false");
   colCountry.ExtendedProperties.Add("Description", "Country");
   colCountry.ExtendedProperties.Add("Length", "15");
   colCountry.ExtendedProperties.Add("Decimals", "0");
   colCountry.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colCountry);
   
   DataColumn colPhone = new DataColumn("Phone", typeof(string), "", MappingType.Element);
   colPhone.AllowDBNull = true;
   colPhone.Caption = "Phone";
   colPhone.MaxLength = 24;
   colPhone.Unique = false;
   colPhone.DefaultValue = Convert.DBNull;
   colPhone.ExtendedProperties.Add("IsKey", "false");
   colPhone.ExtendedProperties.Add("ReadOnly", "false");
   colPhone.ExtendedProperties.Add("Description", "Phone");
   colPhone.ExtendedProperties.Add("Length", "24");
   colPhone.ExtendedProperties.Add("Decimals", "0");
   colPhone.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colPhone);
   
   DataColumn colFax = new DataColumn("Fax", typeof(string), "", MappingType.Element);
   colFax.AllowDBNull = true;
   colFax.Caption = "Fax";
   colFax.MaxLength = 24;
   colFax.Unique = false;
   colFax.DefaultValue = Convert.DBNull;
   colFax.ExtendedProperties.Add("IsKey", "false");
   colFax.ExtendedProperties.Add("ReadOnly", "false");
   colFax.ExtendedProperties.Add("Description", "Fax");
   colFax.ExtendedProperties.Add("Length", "24");
   colFax.ExtendedProperties.Add("Decimals", "0");
   colFax.ExtendedProperties.Add("AllowDBNulls", "true");
   table.Columns.Add(colFax);
   
   table.PrimaryKey = new DataColumn[] {colCustomerID};
  }
  
  /// <summary>
  /// 集合中是否包括有Customer对象
  /// </summary>
  /// <param name="obj">Customer对象</param>
  /// <returns>包含该对象返回true;否则为false</returns>
  public bool Contains(Customer obj)
  {
   bool contains = false;
   if( table != null )
   {
    object[] keys = new object[]{obj.CustomerID};
    contains = table.Rows.Contains(keys);
   }
   return contains;
  }
  
  /// <summary>
  /// 通过主键获取Customer对象
  /// </summary>
  /// <returns>返回Customer对象</returns>
  public Customer GetCustomer(string customerID)
  {
   if( table != null )
   {
    object[] keys = new object[]{customerID};
    DataRow row = table.Rows.Find(keys);
    if( row != null )
    {
     Customer obj = new Customer(Convert.ToString(row["CustomerID"]));
     obj.FromDataRow(row);
     return obj;
    }
   }
   return null;
  }
  
  /// <summary>
  /// 更新集合中的Customer对象,如果该对象不存在,则抛出异常
  /// </summary>
  /// <param name="obj">Customer对象</param>
  public void UpdateCustomer(Customer obj)
  {
   object[] keys = new object[]{obj.CustomerID};
   DataRow row = table.Rows.Find(keys);
   if( row != null )
   {
    obj.ToDataRow(ref row);
   }
   else
   {
    throw new NullReferenceException();
   }
  }
  
  /// <summary>
  /// 将Customer对象加入集合,如果已经存在,则更新该对象。
  /// </summary>
  /// <param name="obj">Customer对象</param>
  public void Add(Customer obj)
  {
   if(Contains(obj))
   {
    UpdateCustomer(obj);
   }
   else
   {
    DataRow row = table.NewRow();
    obj.ToDataRow(ref row);
    table.Rows.Add(row);
   }
  }
  
  /// <summary>
  /// 从集合中移除Customer对象,如果对象不存在,则抛出异常
  /// </summary>
  /// <param name="obj"></param>
  public void Remove(Customer obj)
  {
   object[] keys = new object[]{obj.CustomerID};
   DataRow row = table.Rows.Find(keys);
   if( row != null )
   {
    row.Delete();
   }
   else
   {
    throw new NullReferenceException();
   }
  }
  
  /// <summary>
  /// 从集合中移除第N个对象,如果不存在,则抛出异常
  /// </summary>
  /// <param name="index">第N个</param>
  public void Remove(int index)
  {
   if( index < 0 || index > table.Rows.Count - 1 )
    throw new NullReferenceException();

   DataRow row = table.Rows[index];
   if( row != null )
   {
    row.Delete();
   }
  }
  
  /// <summary>
  /// 根据过滤条件,从集合中获取相应的 Customer对象数组
  /// </summary>
  /// <param name="filter">根据过滤条件</param>
  /// <returns> Customer对象</returns>
  public Customer[] Select(string filter)
  {
   DataRow[] rows = table.Select(filter);
   if( rows != null && rows.Length > 0 )
   {
    Customer[] objs = new Customer[rows.Length];
    for( int i=0; i < rows.Length; i++ )
    {
     DataRow row = rows[i];
     Customer obj = new Customer(Convert.ToString(row["CustomerID"]));
     obj.FromDataRow(row);
     objs[i] = obj;
    }
    return objs;
   }
   return null;
  }
  
  /// <summary>
  /// 移除集合中所有对象,并可更新到数据库中
  /// </summary>
  public void RemoveAll()
  {
   if( table != null )
   {
    foreach(DataRow row in table.Rows)
     row.Delete();
   }
  }
  
  /// <summary>
  /// 清楚集合中的所有对象,但不可更新到数据库中
  /// </summary>
  public void Clear()
  {
   if( table != null )
   {
    table.Rows.Clear();
   }
  }
  
  /// <summary>
  /// 载入数据库中的所有该业务对象
  /// </summary>
  public void Load()
  {
   DataAccess da = new DataAccess();
   DataSet ds = new DataSet();
   ds.Tables.Add(table);
   da.LoadDataSet(ds,table.TableName);
   ds.Tables.Remove(table);
   ds.Dispose();
  }
  
  /// <summary>
  /// 根据过滤条件,载入数据库中的对象集合
  /// </summary>
  /// <param name="filter">过滤条件</param>
  public void Load(string filter)
  {
   DataAccess da = new DataAccess();
   DataSet ds = new DataSet();
   ds.Tables.Add(table);
   string sql = string.Format("SELECT * FROM Customers WHERE 1=1{0}",filter == "" ? "" : " AND " + filter);
   DataSet tempDs = da.ExecuteDataSet(CommandType.Text,sql);
   tempDs.Tables[0].TableName = table.TableName;
   ds.Merge(tempDs);
   ds.Tables.Remove(table);
   tempDs.Dispose();
   ds.Dispose();
  }
  
  /// <summary>
  /// 保存集合中的所有修改至数据库中
  /// </summary>
  public void Save()
  {
   DataSet ds = new DataSet();
   ds.Tables.Add(table.Copy());
   if( ds.HasChanges() )
   {
    DataAccess da = new DataAccess();
    da.UpdateDataSet(ds.GetChanges());
    table.AcceptChanges();
   }
  }
  
  /// <summary>
  /// 获取当前集合中第N个对象
  /// </summary>
  public Customer this[int index]
  {
   get
   {
    DataRow row = Source.Rows[index];
    if( row != null )
    {
     Customer obj = new Customer(Convert.ToString(row["CustomerID"]));
     obj.FromDataRow(row);
     return obj;
    }
    return null;
   }
  }
     
  /// <summary>
  /// 复制对象至数据中
  /// </summary>
  public void CopyTo(Array array, int index)
  {
   Customer[] objs = new Customer[this.Count];
   for(int i=0; i<this.Count; i++)
    objs[i] = this[i];
   Array.Copy(objs,0,array,index,this.Count);
  }

  /// <summary>
  /// 获取集合的枚举类
  /// </summary>
  /// <returns>集合的枚举类</returns>
  public IEnumerator GetEnumerator()
  {
   return new CustomerEnumerator(this);
  }
   
  #endregion
  
  #region 属性
   
  /// <summary>
  /// 集合中对象个数
  /// </summary>
  public int Count
  {
   get
   {
    if( table == null )
     return 0;
    return table.Rows.Count;
   }
  }
  
  public bool IsSynchronized
  {
   get
   {
    return false;
   }
  }
  
  public object SyncRoot
  {
   get
   {
    return null;
   }
  }

  /// <summary>
  /// 集合的数据库源表格
  /// </summary>
  public DataTable Source
  {
   get
   {
    return table;
   }
  }
  
  /// <summary>
  /// 集合的名称
  /// </summary>
  public string Name
  {
   get
   {
    return "Customers";
   }
  }

  
  /// <summary>
  /// 该Customers对象是否保存于缓存中
  /// </summary>
  public bool IsCached
  {
   get
   {
    return false;
   }
  }

  #endregion
 }

 /// <summary>
 /// 数据库表格 Customers 的实体类 Customer
 /// </summary>
 [Serializable()]
 public class Customer
 {
  #region 变量定义
  
  #region 主键
  
  protected string customerID = String.Empty;

  #endregion

  protected string companyName = String.Empty;
  protected string contactName = String.Empty;
  protected string contactTitle = String.Empty;
  protected string address = String.Empty;
  protected string city = String.Empty;
  protected string region = String.Empty;
  protected string postalCode = String.Empty;
  protected string country = String.Empty;
  protected string phone = String.Empty;
  protected string fax = String.Empty;

  #endregion
  
  #region 构造函数
  
  /// <summary>
  /// 构造函数
  /// </summary>
  public Customer()
  {
   customerID = String.Empty;
  }
  
  /// <summary>
  /// 构造函数
  /// </summary>
  public Customer(string customerID)
  {
   this.customerID = customerID;
  }
  
  #endregion
  
  #region 方法
  
  /// <summary>
  /// 从数据库源的记录行设置对象属性
  /// </summary>
  /// <param name="row">记录行</param>
  internal void FromDataRow(DataRow row)
  {
   bool isCurrent = false;
   isCurrent =  this.customerID == Convert.ToString(row["CustomerID"]);
   if( !isCurrent )
    throw new NullReferenceException();
   this.CompanyName = Convert.ToString(row["CompanyName"]);
   this.ContactName = Convert.ToString(row["ContactName"]);
   this.ContactTitle = Convert.ToString(row["ContactTitle"]);
   this.Address = Convert.ToString(row["Address"]);
   this.City = Convert.ToString(row["City"]);
   this.Region = Convert.ToString(row["Region"]);
   this.PostalCode = Convert.ToString(row["PostalCode"]);
   this.Country = Convert.ToString(row["Country"]);
   this.Phone = Convert.ToString(row["Phone"]);
   this.Fax = Convert.ToString(row["Fax"]);
  }
  
  /// <summary>
  /// 将对象的属性设置到引用的记录行
  /// </summary>
  /// <param name="row">记录行</param>
  internal void ToDataRow(ref DataRow row)
  {
   row["CustomerID"] = this.CustomerID;
   row["CompanyName"] = this.CompanyName;
   row["ContactName"] = this.ContactName;
   row["ContactTitle"] = this.ContactTitle;
   row["Address"] = this.Address;
   row["City"] = this.City;
   row["Region"] = this.Region;
   row["PostalCode"] = this.PostalCode;
   row["Country"] = this.Country;
   row["Phone"] = this.Phone;
   row["Fax"] = this.Fax;
  }
  
  #endregion
  
  #region 属性
  
  #region 主键
  
  /// <summary>
  /// 属性CustomerID
  /// </summary>
  public string CustomerID
  {
   get {return customerID;}
  }
  
  #endregion
  
  /// <summary>
  /// 属性CompanyName
  /// </summary>
  public string CompanyName
  {
   get {return companyName;}
   set {companyName = value;}
  }

  /// <summary>
  /// 属性ContactName
  /// </summary>
  public string ContactName
  {
   get {return contactName;}
   set {contactName = value;}
  }

  /// <summary>
  /// 属性ContactTitle
  /// </summary>
  public string ContactTitle
  {
   get {return contactTitle;}
   set {contactTitle = value;}
  }

  /// <summary>
  /// 属性Address
  /// </summary>
  public string Address
  {
   get {return address;}
   set {address = value;}
  }

  /// <summary>
  /// 属性City
  /// </summary>
  public string City
  {
   get {return city;}
   set {city = value;}
  }

  /// <summary>
  /// 属性Region
  /// </summary>
  public string Region
  {
   get {return region;}
   set {region = value;}
  }

  /// <summary>
  /// 属性PostalCode
  /// </summary>
  public string PostalCode
  {
   get {return postalCode;}
   set {postalCode = value;}
  }

  /// <summary>
  /// 属性Country
  /// </summary>
  public string Country
  {
   get {return country;}
   set {country = value;}
  }

  /// <summary>
  /// 属性Phone
  /// </summary>
  public string Phone
  {
   get {return phone;}
   set {phone = value;}
  }

  /// <summary>
  /// 属性Fax
  /// </summary>
  public string Fax
  {
   get {return fax;}
   set {fax = value;}
  }
  
  #endregion
 }
 
 /// <summary>
 /// 数据库表格 Customers 的枚举类 Customer
 /// </summary>
 public class CustomerEnumerator : IEnumerator
 {
  private int position = -1;
  private CustomerCollection colObj;

  /// <summary>
  /// 构造函数
  /// </summary>
  public CustomerEnumerator(CustomerCollection colObj)
  {
   this.colObj = colObj;
  }

  #region IEnumerator 成员

  /// <summary>
  /// 重置枚举位置为-1
  /// </summary>
  public void Reset()
  {
   position = -1;
  }

  /// <summary>
  /// 获取枚举中的当前对象
  /// </summary>
  public object Current
  {
   get
   {
    if( position < 0 || position > colObj.Source.Rows.Count - 1 )
     return null;

    DataRow row = colObj.Source.Rows[position];

    while( row != null && (row.RowState == DataRowState.Deleted || row.RowState == DataRowState.Detached) )
    {
     position ++;
     if( position > colObj.Source.Rows.Count - 1 )
     {
      row = null;
      break;
     }
     row = colObj.Source.Rows[position];
    }
    if( row != null )
    {
     Customer obj = new Customer(Convert.ToString(row["CustomerID"]));
     obj.FromDataRow(row);
     return obj;
    }
    return null;
   }
  }

  /// <summary>
  /// 设置枚举的当前位置加1,并返回是否存在下一个枚举对象
  /// </summary>
  public bool MoveNext()
  {
   position ++;
   if( position > colObj.Source.Rows.Count - 1 )
    return false;

   DataRow row = colObj.Source.Rows[position];

   while( row != null && (row.RowState == DataRowState.Deleted || row.RowState == DataRowState.Detached) )
   {
    position ++;
    if( position > colObj.Source.Rows.Count - 1 )
    {
     row = null;
     break;
    }
    row = colObj.Source.Rows[position];
   }
   return row != null;
  }

  #endregion

 

posted on 2005-11-11 08:59  sneak  阅读(591)  评论(0)    收藏  举报