上一篇:(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
}
}