首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Hxj.Data 實體產生工具:介面操作、支援資料庫、單表/批次產生實戰演示

     編輯:關於ASP.NET

工具介面與支援範圍

實體產生工具主介面

  • 支援:**MS Access、SQL Server**(作者未接觸其他資料庫暫未擴展)。
  • 功能:連線資料庫 → 選取資料表 → 設定命名空間/輸出路徑 → 產生實體類別。

單表產生演示:Northwind Customers 表

選取 Customers 表

產生完成

產出實體程式碼結構(以 Products 為例)

[Serializable]
public class Products : Entity {
    public Products() : base("Products") {}
    #region Model
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public int? SupplierID { get; set; }
    // ... 其他屬性,Setter 內呼叫 OnPropertyValueChange 追蹤異動
    #endregion
    #region Method
    public override Field GetIdentityField() { return _.ProductID; }
    public override Field[] GetPrimaryKeyFields() { return new[] { _.ProductID }; }
    public override Field[] GetFields() { return new[] { _.ProductID, _.ProductName, ... }; }
    public override object[] GetValues() { return new object[] { _ProductID, _ProductName, ... }; }
    public override void SetPropertyValues(IDataReader reader) { /* 逐欄位 ConvertValue */ }
    public override void SetPropertyValues(DataRow row) { /* 同上 */ }
    #endregion
    #region _Field
    public class _ {
        public static readonly Field All = new Field("*", "Products");
        public static readonly Field ProductID = new Field("ProductID", "Products", "ProductID");
        public static readonly Field ProductName = new Field("ProductName", "Products", "ProductName");
        // ... 所有欄位對應靜態唯讀 Field
    }
    #endregion
}

關鍵設計亮點

  • 繼承 Entity 基類:內建異動追蹤、主鍵/身分識別、欄位/值陣列存取。
  • 靜態巢狀類別 _:提供強型別 Field 存取(Products._.ProductName),智慧提示友善。
  • 屬性說明自動提取:從資料庫欄位 Description 產生 XML 註解。
  • DataUtils.ConvertValue:安全處理 DBNull、型別轉換、nullable。
  • 可序列化:支援 Session、ViewState、WCF 傳遞。

批次產生與自訂

  • 可一次勾選多張表批次產生。
  • 新版新增「首字母大寫」選項,符合 .NET 命名慣例(product_id → ProductId)。
  • 產出程式碼為 partial class,方便手工擴充部份方法而不怕重產覆蓋。

實體產生工具讓「資料庫優先」開發流程極度順暢,秒級產出完整可用的強型別實體,是 Hxj.Data 開發體驗的關鍵拼圖。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved