iT邦幫忙

2026 iThome 鐵人賽

DAY 28
0
自我挑戰組

學習C#續系列 第 28 篇

Day 28: 綜合實作專案(一):Console 系統架構與選單設計

  • 分享至 

  • xImage
  •  

觀念重點複習

分層架構設計(Layered Architecture):

  • 模型層(Model):定義領域資料結構(如 Transaction),僅包含屬性與基礎驗證,不涉及任何商業規則與儲存細節。
  • 倉儲層(Repository):負責資料的保存與存取(如記憶體清單或資料庫),透過介面抽換符合 DIP(依賴反轉原則)。
  • 服務層(Service):封裝核心商務邏輯(如餘額累算、收支防呆、統計分析),隔離 UI 與資料持久化。
  • 表現層(UI / Menu):負責終端機畫面的輸出格式化、接收使用者輸入與互動迴圈,符合 SRP(單一職責原則)。

互動選單與狀態迴圈設計:

  • 使用 while (isRunning) 驅動系統主迴圈,透過 switch-case 進行功能分流。
  • 輸入防呆使用 int.TryParse 或 decimal.TryParse,避免因不合法字串輸入導致程式崩潰。

介面隔離與依賴注入思維:

  • 定義 ITransactionRepository 介面,讓 FinanceService 依賴介面而非具體類別,便於後續擴充至 SQLite 或 JSON 儲存,且利於編寫單元測試。

完整實作範例:個人資產記帳管理系統
https://ithelp.ithome.com.tw/upload/images/20261011/20178925jB0L7f77sl.png

https://ithelp.ithome.com.tw/upload/images/20261011/201789255jepZJfvPa.png

https://ithelp.ithome.com.tw/upload/images/20261011/20178925NI8cbbB6Gg.png

https://ithelp.ithome.com.tw/upload/images/20261011/20178925MJ6IssQmSr.png

https://ithelp.ithome.com.tw/upload/images/20261011/20178925qCCdRCdbf8.png

https://ithelp.ithome.com.tw/upload/images/20261011/20178925b5IiJ8eWmG.png

成果
收入
https://ithelp.ithome.com.tw/upload/images/20261011/20178925tDumO8JD7r.png
支出
https://ithelp.ithome.com.tw/upload/images/20261011/20178925roXNCBG9KB.png
紀錄
https://ithelp.ithome.com.tw/upload/images/20261011/20178925Vk9fGQKKLA.png
離開
https://ithelp.ithome.com.tw/upload/images/20261011/20178925gQ7HuT5dhr.png


上一篇
Day 27: 單元測試實戰(Unit Testing)
下一篇
Day 29: 綜合實作專案(二):資料庫串接、LINQ 統計與非同步匯出
系列文
學習C#續 共 29 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言