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

分層架構異常處理機制:從 DAC 到 UI 的完整實作範例

     編輯:關於.NET

針對典型三層架構,建立分層異常處理機制:自訂異常類別逐層包裝上拋,最終在表現層統一記錄與友善提示。本文提供完整程式碼範例,涵蓋 DAC、BLL、UI 三層。

異常處理組件圖

一、自訂異常基礎:BLLException

[Serializable]
public class BLLException : Exception, ISerializable {
    private string myBLLMsg;

    public string MyBLLMsg { get { return myBLLMsg; } }

    public override string Message {
        get { return myBLLMsg == null ? base.Message : myBLLMsg + base.Message; }
    }

    // 多重建構子支援:標準、自訂訊息、內部異常、序列化
    public BLLException() : base() { }
    public BLLException(string message) : base(message) { }
    public BLLException(string message, Exception inner) : base(message, inner) { }
    public BLLException(string message, string myExcuteMsg) : base(message) { this.myBLLMsg = myExcuteMsg; }
    public BLLException(string message, string myExcuteMsg, Exception inner) : base(message, inner) { this.myBLLMsg = myExcuteMsg; }

    protected BLLException(SerializationInfo info, StreamingContext context) : base(info, context) {
        myBLLMsg = info.GetString("MydelMsg");
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context) {
        info.AddValue("MydelMsg", myBLLMsg);
        base.GetObjectData(info, context);
    }
}

設計亮點:重寫 Message 屬性融合自訂訊息與原始異常;實作 ISerializable 保證跨應用程式域/遠端呼叫時自訂欄位正確序列化。

二、資料存取層 (DAC) 異常處理


public class BaseDataHelper : IBaseDataHelper {
    public DataSet BaseQuery(string strSql) {
        try { return DbHelperOra.Query(strSql); }
        catch (DACException ex) { throw new DACException("資料庫存取錯誤", "查詢公用模組錯誤", ex); }
    }

    public int ExecuteSql(string strSql) {
        try { return DbHelperOra.ExecuteSql(strSql); }
        catch (DACException ex) { throw new DACException("資料庫存取錯誤", "執行 SQL 錯誤", ex); }
    }

    public bool ExecuteSqlTran(ArrayList SQLStringList) {
        try { DbHelperOra.ExecuteSqlTran(SQLStringList); }
        catch (DACException ex) { throw new DACException("資料庫存取錯誤", "執行交易錯誤", ex); }
        return true;
    }
    // Hashtable 版本略同
    public object GetSingle(string strSql) {
        try { return DbHelperOra.GetSingle(strSql); }
        catch (DACException ex) { throw new DACException("資料庫存取錯誤", "取得單一物件錯誤", ex); }
    }
}

策略:捕獲底層具體異常(DACException),包裝為含業務語意的新異常上拋,保留 InnerException 完整呼叫堆疊。

三、業務邏輯層 (BLL) 異常處理


public class UserBLL : IUserManager {
    public DataTable GetUserDataTable() {
        try {
            string strsql = "select * from napp_accounts_user";
            return DataAccess.CreateBaseDataHelper().BaseQuery(strsql).Tables[0];
        }
        catch (BLLException ex) { throw new BLLException("查詢使用者業務", ex); }
    }
}

BLL 層捕獲 DAC 層異常,附加業務操作語境("查詢使用者業務")重新拋出,形成清晰的異常鏈。

四、表現層 (UI) 異常處理


log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_Default));

protected void Page_Load(object sender, EventArgs e) {
    try {
        DataTable dt = BLLAccess.CreateUserBLL().GetUserDataTable();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
    catch (Exception ex) {
        logger.Error("查詢使用者綁定 GetUserDataTable() Error: " + ex.ToString());
        // 可導向錯誤頁面或顯示友善訊息
    }
}

UI 層統一捕獲所有異常,使用 log4net 記錄完整堆疊(ex.ToString() 包含巢狀 InnerException),避免技術細節外洩給使用者。

五、核心原則總結

  1. 分層包裝:每層捕獲下層異常,附加層級語意重拋
  2. 保留堆疊:永遠傳遞 innerException
  3. 自訂異常可序列化:支援分散式場景
  4. UI 層統一攔截:記錄日誌 + 友善回饋,不拋給使用者
  5. 區分技術異常與業務異常:自訂異常承載業務語意
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved