(2)CM.Api項目創建Json包裝類與數據庫操作接口

上一篇:(1)解決方案搭建以及初步調試

  • 在 CM.Api 添加文件夾,取名爲 Utility,並在 Utility 文件夾下創建類,取名爲 ResponseJson(注意大小寫)
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 建立這個類的目的是:該項目前端使用的是 LayUI 的框架,它在進行 AJAX 請求進行數據包裝的時候就需要用到 Json 數據,那麼ResponseJson 實現的是後端對 Json 數據進行包裝轉換
    在這裏插入圖片描述
    ResponseJson 代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CM.Api.Utility
{
    public class ResponseJson
    {
        public string Code { get; set; }
        public string Msg { get; set; }
        public int Count { get; set; }
        public object Data { get; set; }
        public ResponseJson()
        {
            Code = "200";
            Msg = "ok";
            Count = 0;
        }
    }
}

  • CM.Repository 主要用於數據庫操作,在該目錄下定義接口 IRepository.cs
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    IRepository.cs代碼:
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace CM.Repository
{
    public interface IRepository<T> where T : class, new()
    {
        #region
        //  獲取模型對象
        T GetModel(dynamic id);
        T GetModel(Expression<Func<T, bool>> expression);

        //  獲取所有
        List<T> GetList();
        List<T> GetList(string where);
        List<T> GetList(Expression<Func<T, bool>> expression);

        //   獲取分頁列表
        List<T> GetPageList(int pageIndex, int pageSize);
        List<T> GetPageList(int pageIndex, int pageSize, string where);
        List<T> GetPageList(int pageIndex, int pageSize, Expression<Func<T, bool>> expression);

        //  增加
        bool Insert(T obj);

        //  根據主鍵刪除
        bool Delete(dynamic id);
        bool Delete(Expression<Func<T, bool>> expression);

        //  根據主鍵批量刪除
        bool DeleteList(List<dynamic> ids);
        bool DeleteList(string ids);

        //  更新
        bool Update(T obj);

        //  判斷主鍵是否存在
        bool Exists(dynamic id);
        #endregion
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章