添加项目文件。
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
/// <summary>
|
||||
/// 此处是为了 后续代替 IsSuccess IsSuccess暂时还是保留,接下来的接口判断现在用Code 判断 不用IsSuccess就好了
|
||||
|
||||
/// </summary>
|
||||
public enum ApiResponseCodeEnum
|
||||
{
|
||||
//正常的 相当于之前的 IsSuccess = true
|
||||
OK = 0,
|
||||
|
||||
//Api 输入参数有问题 相当于之前的 IsSuccess = false
|
||||
ApiInputError = 1,
|
||||
|
||||
//业务验证不通过 相当于之前的 IsSuccess = false
|
||||
BusinessValidationFailed = 2,
|
||||
|
||||
//数据不存在
|
||||
DataNotExist=3,
|
||||
|
||||
//程序异常 相当于之前的 IsSuccess = false
|
||||
ProgramException = 4,
|
||||
|
||||
//需要提示 ,需要提示 从Result 取数据
|
||||
NeedTips = 5
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
/// <summary>
|
||||
/// 响应数据输出接口
|
||||
/// </summary>
|
||||
public interface IResponseOutput
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否成功
|
||||
/// </summary>
|
||||
bool IsSuccess { get; }
|
||||
|
||||
public ApiResponseCodeEnum Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
string ErrorMessage { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 响应数据输出泛型接口
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface IResponseOutput<T> : IResponseOutput
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回数据
|
||||
/// </summary>
|
||||
T Data { get; }
|
||||
}
|
||||
|
||||
public interface IResponseOutput<T,T2> : IResponseOutput
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回数据
|
||||
/// </summary>
|
||||
T Data { get; }
|
||||
|
||||
T2 OtherInfo { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
public static class NUllCheckExtension
|
||||
{
|
||||
|
||||
|
||||
[DoesNotReturn]
|
||||
public static TEntity IfNullThrowException<TEntity>(this TEntity businessObject) where TEntity : class
|
||||
{
|
||||
if(businessObject == null)
|
||||
{
|
||||
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused");
|
||||
}
|
||||
else
|
||||
{
|
||||
return businessObject!;
|
||||
}
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
public static TEntity IfNullThrowException<TEntity>(this TEntity? businessStruct) where TEntity : struct
|
||||
{
|
||||
if (businessStruct == null)
|
||||
{
|
||||
throw new QueryBusinessObjectNotExistException($"The query object {typeof(TEntity).Name} does not exist , or was deleted by someone else, or an incorrect parameter query caused");
|
||||
}
|
||||
else
|
||||
{
|
||||
return (TEntity)businessStruct;
|
||||
}
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
public static TEntity IfNullThrowConvertException<TEntity>(this TEntity businessObject) where TEntity : class
|
||||
{
|
||||
if (businessObject == null)
|
||||
{
|
||||
throw new QueryBusinessObjectNotExistException($" Can not Convert to {typeof(TEntity).Name} Type, Please check parameter");
|
||||
}
|
||||
else
|
||||
{
|
||||
return businessObject!;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class QueryBusinessObjectNotExistException : Exception
|
||||
{
|
||||
|
||||
public QueryBusinessObjectNotExistException()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public QueryBusinessObjectNotExistException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class ConversionException : Exception
|
||||
{
|
||||
public ConversionException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
/// <summary>
|
||||
/// 分页信息输出 泛型
|
||||
/// </summary>
|
||||
public class PageOutput<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前页索引
|
||||
/// </summary>
|
||||
public int PageIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 每页的记录条数
|
||||
/// </summary>
|
||||
public int PageSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据总数
|
||||
/// </summary>
|
||||
public long TotalCount { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public IList<T> CurrentPageData { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 分页数据 可能额外返回其他类型的查询数据 必须一些配置
|
||||
/// </summary>
|
||||
public object OtherData { get; set; }
|
||||
|
||||
public PageOutput()
|
||||
{
|
||||
}
|
||||
|
||||
public PageOutput(int pageIndex, int pageSize, long totalCount, IList<T> data)
|
||||
{
|
||||
PageIndex = pageIndex;
|
||||
PageSize = pageSize;
|
||||
TotalCount = totalCount;
|
||||
CurrentPageData = data;
|
||||
}
|
||||
|
||||
public PageOutput(int pageIndex, int pageSize, IQueryable<T> list)
|
||||
{
|
||||
PageIndex = pageIndex;
|
||||
PageSize = pageSize;
|
||||
TotalCount = list.Count();
|
||||
CurrentPageData = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
/// <summary>
|
||||
/// 响应数据输出 泛型
|
||||
/// </summary>
|
||||
public class ResponseOutput<T> : IResponseOutput<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否成功标记
|
||||
/// </summary>
|
||||
public bool IsSuccess { get; private set; }
|
||||
|
||||
|
||||
public ApiResponseCodeEnum Code { get; set; } = ApiResponseCodeEnum.OK;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 消息
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据 兼顾以前 Json序列化的时候返回属性名为“Result”
|
||||
/// </summary>
|
||||
[JsonProperty("Result")]
|
||||
public T Data { get; private set; }
|
||||
|
||||
|
||||
|
||||
public object OtherData { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="msg">消息</param>
|
||||
//public ResponseOutput<T> Ok(T data, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
//{
|
||||
// IsSuccess = true;
|
||||
// Code = code;
|
||||
// Data = data;
|
||||
// ErrorMessage = msg;
|
||||
|
||||
// return this;
|
||||
//}
|
||||
|
||||
public ResponseOutput<T> Ok(T data, object otherData, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
{
|
||||
IsSuccess = true;
|
||||
Code = code;
|
||||
Data = data;
|
||||
OtherData=otherData;
|
||||
ErrorMessage = msg;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败
|
||||
/// </summary>
|
||||
/// <param name="msg">提示消息</param>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns></returns>
|
||||
public ResponseOutput<T> NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
{
|
||||
IsSuccess = false;
|
||||
Code = code;
|
||||
ErrorMessage = msg;
|
||||
Data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class ResponseOutput<T, T2> : IResponseOutput<T, T2>
|
||||
{
|
||||
[JsonProperty("Result")]
|
||||
public T Data { get; private set; }
|
||||
|
||||
public T2 OtherInfo { get; private set; }
|
||||
|
||||
public bool IsSuccess { get; private set; }
|
||||
|
||||
public ApiResponseCodeEnum Code { get; set; }
|
||||
|
||||
|
||||
public string ErrorMessage { get; private set; }
|
||||
|
||||
|
||||
public ResponseOutput<T, T2> Ok(T data, T2 otherInfo, string msg = "")
|
||||
{
|
||||
IsSuccess = true;
|
||||
Data = data;
|
||||
OtherInfo = otherInfo;
|
||||
ErrorMessage = msg;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 响应数据静态输出 为了代码简洁 不用每处都New
|
||||
/// </summary>
|
||||
public static class ResponseOutput
|
||||
{
|
||||
|
||||
|
||||
public static IResponseOutput<T, T2> Ok<T, T2>(T data, T2 otherInfo, string msg = "")
|
||||
{
|
||||
return new ResponseOutput<T, T2>().Ok(data, otherInfo);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 成功 -----适合查询
|
||||
/// </summary>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="msg">消息</param>
|
||||
/// <returns></returns>
|
||||
//public static IResponseOutput<T> Ok<T>(T data = default, string msg = "")
|
||||
//{
|
||||
// return new ResponseOutput<T>().Ok(data, msg);
|
||||
//}
|
||||
|
||||
public static IResponseOutput<T> Ok<T>(T data = default, object otherData = default, string msg = "")
|
||||
{
|
||||
return new ResponseOutput<T>().Ok(data, otherData, msg);
|
||||
}
|
||||
/// <summary>
|
||||
/// 成功
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IResponseOutput Ok()
|
||||
{
|
||||
return Ok<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败
|
||||
/// </summary>
|
||||
/// <param name="msg">消息</param>
|
||||
/// <param name="data">数据</param>
|
||||
/// <returns></returns>
|
||||
public static IResponseOutput<T> NotOk<T>(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
{
|
||||
return new ResponseOutput<T>().NotOk(msg, data, code);
|
||||
}
|
||||
|
||||
public static IResponseOutput<T> NotOk<T>( T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
{
|
||||
return new ResponseOutput<T>().NotOk("", data, code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败
|
||||
/// </summary>
|
||||
/// <param name="msg">消息</param>
|
||||
/// <returns></returns>
|
||||
public static IResponseOutput<string> NotOk(string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK)
|
||||
{
|
||||
return new ResponseOutput<string>().NotOk(msg,code:code);
|
||||
}
|
||||
|
||||
public static IResponseOutput<string> DBNotExistIfNUll(object businessObject)
|
||||
{
|
||||
return new ResponseOutput<string>().NotOk($"The business object{businessObject.GetType().Name} does not exist in the database, or was deleted by someone else, or an incorrect parameter query caused");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据布尔值返回结果 --适合删除
|
||||
/// </summary>
|
||||
/// <param name="success"></param>
|
||||
/// <returns></returns>
|
||||
public static IResponseOutput Result(bool success)
|
||||
{
|
||||
return success ? Ok() : NotOk("Expect a change, but the database data has not changed");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据布尔值返回结果 --适合添加和更新一起
|
||||
/// </summary>
|
||||
/// <param name="success"></param>
|
||||
/// <returns></returns>
|
||||
public static IResponseOutput<T> Result<T>(bool success, T data = default)
|
||||
{
|
||||
return success ? Ok<T>(data) : NotOk<T>("Saved failed");
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 根据布尔值返回结果
|
||||
///// </summary>
|
||||
///// <param name="success"></param>
|
||||
///// <returns></returns>
|
||||
//public static IResponseOutput Result<T>(bool success)
|
||||
//{
|
||||
// return success ? Ok<T>() : NotOk<T>();
|
||||
//}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user