.Net Core3.0 WebApi 五:項目分層


.Net Core3.0 WebApi 項目框架搭建:目錄

理論介紹

倉儲(Respository)是存在於工作單元和數據庫之間單獨分離出來的一層,是對數據訪問的封裝。其優點:

1)業務層不需要知道它的具體實現,達到了分離關注點。

2)提高了對數據庫訪問的維護,對於倉儲的改變並不會改變業務的邏輯,數據庫可以用Sql Server、MySql等

基本項目架構

 Infrastructure

 基礎設施層。一些數據庫的配置,幫助了,都放在這里。

Common

 一些公共方法,比如一些擴展方法,放在這里。

Models

 一些,枚舉,請求實體,響應實體,SQLsugar的表實體,都放在這里。

因為在前端接口中,需要固定的格式,以及操作,不能把數據直接發出去。請求實體,響應實體,基礎封裝,如下:

public class PageRequest<T> where T : class
{
    /// <summary>
    /// 當前頁
    /// </summary>
    public int PageIndex { get; set; } = 1;

    /// <summary>
    /// 一頁條數
    /// </summary>
    public int PageSize { get; set; } = 10;

    /// <summary>
    /// 請求參數
    /// </summary>
    public T Data { get; set; }
}
PageRequest
public class BaseResponse
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool IsSuccess { get; set; } = true;

    /// <summary>
    /// 返回消息
    /// </summary>
    public string Msg { get; set; } = "";
}

public class BaseResponse<T> : BaseResponse where T : class
{
    /// <summary>
    /// 返回實體
    /// </summary>
    public T Data { get; set; }
}
BaseResponse
public class PageResponse<T>: BaseResponse where T : class
{
    /// <summary>
    /// 總記錄條數
    /// </summary>
    public int Total { get; set; }

    /// <summary>
    /// 響應數據
    /// </summary>
    public List<T> Data { get; set; }
}
PageResponse

分層,最基礎的是三層,層次再多,也是根據三層發展的。至於其他的分層,以及使用,個人有個人的看法。這里就不多做贅述了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM