在分層架構中,介面扮演關鍵角色:它不僅決定各層操作類別必須實作的契約,更明確劃分各層職責,也是依賴注入機制的基石。本文遵循「由 UI 識別業務邏輯,由業務邏輯確定資料存取」的順序,完整展示留言板系統的介面設計過程。
原則:層間資料傳遞僅使用實體類別或集合,禁用 DataTable 等強型別物件。
| 代號 | 頁面描述 |
|---|---|
| UI01 | 主頁面:分頁顯示留言與評論,管理員可審核/刪除/回覆,遊客可發表 |
| UI02 | 發表留言頁面 |
| UI03 | 發表評論頁面 |
| UI04 | 管理員回覆留言頁面 |
| UI05 | 管理員登入頁面 |
| UI06 | 管理員修改密碼頁面 |
| UI07 | 超級管理員頁面:管理員列表與刪除 |
| UI08 | 新增管理員頁面 |
| UI09 | 操作成功跳轉提示頁面 |
| UI10 | 系統異常友善錯誤頁面 |
using System;
using System.Collections.Generic;
using NGuestBook.Entity;
namespace NGuestBook.IBLL
{
public interface IAdminBLL
{
bool Add(AdminInfo admin);
bool Remove(int id);
bool ChangePassword(int id, string password);
AdminInfo Login(string name, string password);
IList<AdminInfo> GetAll();
}
}
public interface IMessageBLL
{
bool Add(MessageInfo message);
bool Remove(int id);
bool Revert(int id, string reply);
bool Pass(int id);
IList<MessageInfo> GetByPage(int pageSize, int pageNumber);
}
public interface ICommentBLL
{
bool Add(CommentInfo comment);
bool Remove(int id);
IList<CommentInfo> GetByMessage(int messageId);
}
public interface IAdminDAL
{
bool Insert(AdminInfo admin);
bool Delete(int id);
bool Update(AdminInfo admin);
AdminInfo GetByID(int id);
AdminInfo GetByName(string name);
AdminInfo GetByNameAndPassword(string name, string password);
IList<AdminInfo> GetAll();
}
public interface IMessageDAL
{
bool Insert(MessageInfo message);
bool Delete(int id);
bool Update(MessageInfo message);
MessageInfo GetByID(int id);
IList<MessageInfo> GetByPage(int pageSize, int pageNumber);
}
public interface ICommentDAL
{
bool Insert(CommentInfo comment);
bool Delete(int id);
IList<CommentInfo> GetByMessage(int messageId);
}
AdminInfo、MessageInfo、CommentInfo)交換資料,維持鬆耦合。Pass、Revert),DAL 方法名採用資料操作語言(Insert、Update),語意清晰區分。IAdminDAL 額外提供 GetByName 以支援新增管理員時的重名檢查,體現介面設計應考慮上層業務規則。良好的介面設計是分層架構成功的關鍵。本文展示的由 UI 反推 BLL、由 BLL 反推 DAL 的系統化方法,配合實體類別傳遞與命名規範,能構建出清晰、可測試、易擴充的企業級應用程式骨架。後續將進入具體實作類別與依賴注入容器整合階段。