.NET 文件系统(三)-- ThenBy开发,CRUD方法与封装返回值类
作者:互联网
FileDownLoadSystem.Core
LambdaExtensions 中的GetIQueryOrderBy 添加thenBy逻辑
点击查看代码
/// <summary>
/// 拼接表达式
/// </summary>
/// <param name="queryable"></param>
/// <param name="orderBySelector"></param>
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
public static IQueryable<TModel> GetIQueryOrderBy<TModel>(
this IQueryable<TModel> queryable,
Dictionary<string, QueryOrderBy> orderBySelector)
{
string[] orderByKeys = orderBySelector.Select(s => s.Key).ToArray();
//如果orderByKeys长度为0,说明当前无需排序
if (orderByKeys == null || orderByKeys.Length == 0)
{
return queryable;
}
//如果存在则重组Queryable;
IOrderedQueryable<TModel> queryableOrderBy = null;
string orderByKey = orderByKeys[orderByKeys.Length - 1];
queryableOrderBy = orderBySelector[orderByKey] == QueryOrderBy.Desc
? queryableOrderBy = queryable.OrderByDescending(orderByKey.GetExpression<TModel>())
: queryableOrderBy = queryable.OrderBy(orderByKey.GetExpression<TModel>());
for (int i = orderByKey.Length - 2; i >= 0; i--)
{
queryableOrderBy = orderBySelector[orderByKeys[i]] == QueryOrderBy.Desc
? queryableOrderBy = queryableOrderBy.ThenByDescending(orderByKeys[i].GetExpression<TModel>())
: queryableOrderBy = queryableOrderBy.ThenBy(orderByKeys[i].GetExpression<TModel>());
}
return queryableOrderBy;
}
改造BaseService
BaseService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FileDownLoadSystem.Core.Enums;
using FileDownLoadSystem.Entity;
namespace FileDownLoadSystem.Core.BaseProvider
{
public class BaseService<TModel, TRepository>
where TModel : BaseModel
where TRepository : IRepository<TModel>
{
protected readonly TRepository _repository;
public BaseService(TRepository repository)
{
this._repository = repository;
}
public TModel FindFirst(Expression<Func<TModel, bool>> predicate,
Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null)
{
return _repository.FindFirst(predicate, orderBy);
}
}
}
改造BaseRepository 增加CRUD方法
BaseRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FileDownLoadSystem.Core.EfDbContext;
using FileDownLoadSystem.Core.Enums;
using FileDownLoadSystem.Core.Extensions;
using FileDownLoadSystem.Entity;
using Microsoft.EntityFrameworkCore;
namespace FileDownLoadSystem.Core.BaseProvider
{
public class BaseRepository<TModel> where TModel : BaseModel
{
private readonly FileDownloadSystemDbContext _defaultDbContext;
public BaseRepository(FileDownloadSystemDbContext dbContext)
{
this._defaultDbContext = dbContext;
}
public FileDownloadSystemDbContext DbContext => _defaultDbContext;
private DbSet<TModel> DbSet => _defaultDbContext.Set<TModel>();
public virtual TModel FindFirst(
Expression<Func<TModel, bool>> predicate,
Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null)
{
return FindAsIQueryable(predicate, orderBy).FirstOrDefault();
}
public IQueryable<TModel> FindAsIQueryable(
Expression<Func<TModel, bool>> predicate,
Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null)
{
if (orderBy != null)
{
//
//DbSet等价于DbContext.Set<TModel>
return DbSet.Where(predicate).GetIQueryOrderBy(orderBy.GetExpressionToDic());
}
return DbSet.Where(predicate);
}
/// <summary>
/// 增加这个方法的目的是为了为上端提供直接拼接where的方法
/// </summary>
/// <returns></returns>
public IQueryable<TModel> FindAsIQuery()
{
return DbSet;
}
/// <summary>
/// 修改当前表数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public virtual int Update(TModel entity)
{
DbSet.Update(entity);
// 这里请注意,SaveChanges()执行后返回的是受影响的行数
return DbContext.SaveChanges();
}
/// <summary>
/// 更新其他表数据
/// </summary>
/// <param name="entity"></param>
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
public virtual int Update<TModel>(TModel entity)
where TModel : BaseModel
{
DbContext.Set<TModel>().Update(entity);
return DbContext.SaveChanges();
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public virtual int Insert(TModel entity)
{
DbSet.Add(entity);
return DbContext.SaveChanges();
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public virtual int Delete(TModel entity)
{
DbSet.Remove(entity);
return DbContext.SaveChanges();
}
/// <summary>
/// 整体保存
/// </summary>
/// <returns></returns>
public virtual int SaveChanges()
{
return DbContext.SaveChanges();
}
}
}
IRepository改造
IRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FileDownLoadSystem.Core.EfDbContext;
using FileDownLoadSystem.Core.Enums;
using FileDownLoadSystem.Entity;
namespace FileDownLoadSystem.Core.BaseProvider
{
/// <summary>
/// 基类仓储
/// </summary>
/// <typeparam name="TModel"></typeparam>
public interface IRepository<TModel> where TModel : BaseModel
{
FileDownloadSystemDbContext DbContext { get; }
int Delete(TModel entity);
IQueryable<TModel> FindAsIQuery();
IQueryable<TModel> FindAsIQueryable(Expression<Func<TModel, bool>> predicate, Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null);
TModel FindFirst(Expression<Func<TModel, bool>> predicate, Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null);
int Insert(TModel entity);
int SaveChanges();
int Update(TModel entity);
int Update<TModel>(TModel entity) where TModel : BaseModel;
}
}
返回值类型封装
新增Utility文件夹,新建WebResponseContent类
WebResponseContent
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading.Tasks;
namespace FileDownLoadSystem.Core.Utility
{
public class WebResponseContent
{
public bool Status { get; set; }
//2xx 属于正确请求
//204代表请求正确但是没有返回值
//202请求正确,但是服务器中没有做任何数据的变化
//203代表没有权限
//3xx 地址迁移 是错误的,一般不包含返回值
//
public string Code { get; set; }
public string Message { get; set; }
public object Data { get; set; }
public WebResponseContent()
{
}
public static WebResponseContent Instance => new WebResponseContent();
// public static WebResponseContent Instance
// {
// get {return new WebResponseContent();}
// }
public WebResponseContent(bool status)
{
this.Status = status;
}
public WebResponseContent OK()
{
this.Status = true;
return this;
}
public WebResponseContent OK(ResponseType responseType)
{
return Set(responseType, true);
}
public WebResponseContent OK(string msg = null, object data = null)
{
this.Status = true;
this.Message = msg;
this.Data = data;
return this;
}
public WebResponseContent Error(string msg = null)
{
this.Status = false;
this.Message = msg;
return this;
}
public WebResponseContent Error(ResponseType responseType)
{
return Set(responseType, false);
}
public WebResponseContent Set(ResponseType responseType, bool? status)
{
return this.Set(responseType, null, status);
}
public WebResponseContent Set(ResponseType responseType, string msg)
{
bool? b = null;
return this.Set(responseType, msg, b);
}
public WebResponseContent Set(ResponseType responseType, string msg, bool? status)
{
if (status != null)
{
this.Status = (bool)status;
}
this.Code = ((int)responseType).ToString();
if (!string.IsNullOrEmpty(msg))
{
Message = msg;
return this;
}
// Message = responseType.GetMsg();
return this;
}
}
public enum ResponseType
{
ServerError = 1,
LoginExpiration = 302,
ParametersLack = 303,
TokenExpiration,
PINError,
NoPermissions,
NoRolePermissions,
LoginError,
AccountLocked,
LoginSuccess,
SaveSuccess,
AuditSuccess,
OperSuccess,
RegisterSuccess,
ModifyPwdSuccess,
EidtSuccess,
DelSuccess,
NoKey,
NoKeyDel,
KeyError,
Other
}
}
标签:return,--,CRUD,System,WebResponseContent,TModel,ThenBy,using,public 来源: https://www.cnblogs.com/rookiewang/p/16659196.html