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

ASP.NET MVC5 文章管理架構設計:CommonModel 統一入口、領域模型與分層搭建(第7篇)

     編輯:ASP.NET 基礎

功能規劃

文章管理包含會員端(我的文章、發布文章)與管理端(審核、列表、編輯、刪除)。核心實體關係:

功能架構圖

領域模型

類別圖

  • Category:分類,支援多模型共用
  • CommonModel:內容管理統一基類(標題、分類、狀態、時間、點擊、作者、模型類型)
  • Article:文章擴展(作者、來源、摘要、內容),與 CommonModel 1:1
  • Attachment:附件(檔案路徑、類型、擁有者),與 CommonModel 1:N

分層架構搭建順序

分層搭建順序

1. IDAL(介面層)

namespace Ninesky.IDAL
{
    public interface ICommonModelRepository : IBaseRepository { }
    public interface ICategoryRepository : IBaseRepository { }
    public interface IArticleRepository : IBaseRepository
{ } public interface IAttachmentRepository : IBaseRepository { } }

2. DAL(資料存取層)

namespace Ninesky.DAL
{
    public class CommonModelRepository : BaseRepository, ICommonModelRepository { }
    public class CategoryRepository : BaseRepository, ICategoryRepository { }
    public class ArticleRepository : BaseRepository
, IArticleRepository { } public class AttachmentRepository : BaseRepository, IAttachmentRepository { } }

3. IBLL(業務邏輯介面)

namespace Ninesky.IBLL
{
    public interface ICategoryService : IBaseService { }
    public interface IArticleService : IBaseService
{ } public interface IAttachmentService : IBaseService { } public interface ICommonModelService : IBaseService { // 共用分頁查詢,供 Article/Consultation 等複用 IQueryable FindPageList(out int total, int pageIndex, int pageSize, string model, string title, int categoryId, string inputer, DateTime? fromDate, DateTime? toDate, int orderCode); } }

4. BLL(業務邏輯層)

namespace Ninesky.BLL
{
    public class CategoryService : BaseService, ICategoryService
    {
        public CategoryService() : base(RepositoryFactory.CategoryRepository) { }
    }

    public class ArticleService : BaseService
, IArticleService { public ArticleService() : base(RepositoryFactory.ArticleRepository) { } } public class AttachmentService : BaseService, IAttachmentService { public AttachmentService() : base(RepositoryFactory.AttachmentRepository) { } } public class CommonModelService : BaseService, ICommonModelService { public CommonModelService() : base(RepositoryFactory.CommonModelRepository) { } // 實作 FindPageList... } }

5. Web - Member 區域控制器

namespace Ninesky.Web.Areas.Member.Controllers
{
    [Authorize]
    public class CategoryController : Controller
    {
        private readonly ICategoryService _service = new CategoryService();
    }

    public class ArticleController : Controller
    {
        private readonly IArticleService _articleService = new ArticleService();
        private readonly ICommonModelService _commonModelService = new CommonModelService();
    }

    [Authorize]
    public class AttachmentController : Controller
    {
        private readonly IAttachmentService _attachmentService = new AttachmentService();
    }
}

架構設計亮點

設計決策優勢
CommonModel 統一入口Article、Attachment、Comment 皆掛載於 CommonModel,Service 層針對 CommonModel 操作,避免重複建立 DAL/BLL
模型擴展模式Article 為獨立實體,透過 CommonModel.Article 導覽屬性關聯,符合開放封閉原則
Repository FactoryBLL 建構子注入 Repository,降低耦合,利於單元測試
Area 隔離Member 區域獨立控制器,配合 [Authorize] 統一權限控管
共用查詢介面ICommonModelService.FindPageList 服務於所有內容模型

後續開發規劃

  1. 第8篇:文章發布(KindEditor、附件上傳、縮圖、生命週期)
  2. 第9篇:文章列表(EasyUI DataGrid、多條件搜尋、分頁)
  3. 第10篇:修改與刪除(TryUpdateModel、關聯刪除順序、附件清理)
  4. 第11-13篇:諮詢模組(架構、會員端、管理端)

結語

本篇確立文章管理的領域模型與分層架構。關鍵創新在於 CommonModel 作為內容管理的「統一抽象」,透過導覽屬性擴展不同業務實體,配合共用的 ICommonModelService 實現查詢邏輯複用。這種設計讓新增內容類型(如產品、公告、招聘)只需建立擴展實體並掛載至 CommonModel,無需修改基礎架構,極大提升系統擴展性。

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