通用结果类用于返回响应结果
public interface IResult
{
bool Result { get; set; }
string Message { get; set; }
string Title { get; set; }
string Code { get; set; }
object Tag { get; set; }
}
public interface IResult<T>
{
T Data { get; set; }
}
public interface IResultList<T>
{
List<T> Data { get; set; }
}
public abstract class BaseResult : IResult
{
public virtual bool Result { get; set; }
public virtual string Message { get; set; }
public virtual string Title { get; set; }
public virtual string Code { get; set; }
public virtual object Tag { get; set; }
#region 转换
public Result<T> ToResult<T>()
{
return this as Result<T>;
}
public ResultList<T> ToResultList<T>()
{
return this as ResultList<T>;
}
#endregion
#region 设置结果
public BaseResult SetResult(bool result, string message, string title = "")
{
this.Result = result;
this.Title = title;
this.Message = message;
return this;
}
#endregion
#region 设置OK
public BaseResult SetOK(string message, string title = "")
{
this.Result = true;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this;
}
public Result<T> SetOK_Result<T>(string message, string title = "")
{
this.Result = true;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as Result<T>;
}
public ResultList<T> SetOK_ResultList<T>(string message, string title = "")
{
this.Result = true;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as ResultList<T>;
}
#endregion
#region 设置NG
public BaseResult SetNG(string message, string title = "")
{
this.Result = false;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this;
}
public Result<T> SetNG_Result<T>(string message, string title = "")
{
this.Result = false;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as Result<T>;
}
public ResultList<T> SetNG_ResultList<T>(string message, string title = "")
{
this.Result = false;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as ResultList<T>;
}
#endregion
#region 设置消息
public BaseResult SetMsg(string message, string title = "")
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this;
}
public Result<T> SetMsg_Result<T>(string message, string title = "")
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as Result<T>;
}
public ResultList<T> SetMsg_ResultList<T>(string message, string title = "")
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
return this as ResultList<T>;
}
public BaseResult SetMsgIF(bool condition, string message, string title = "")
{
if (condition)
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
}
return this;
}
public Result<T> SetMsgIF_AsResult<T>(bool condition, string message, string title = "")
{
if (condition)
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
}
return this as Result<T>;
}
public ResultList<T> SetMsgIF_AsResultList<T>(bool condition, string message, string title = "")
{
if (condition)
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
}
return this as ResultList<T>;
}
public Result<T> SetMsgIF_AsResult<T>(Func<Result<T>, bool> condition, string message, string title = "")
{
Result<T> THIS = this as Result<T>;
if (condition != null && condition(THIS))
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
}
return THIS;
}
public ResultList<T> SetMsgIF_AsResultList<T>(Func<ResultList<T>, bool> condition, string message, string title = "")
{
ResultList<T> THIS = this as ResultList<T>;
if (condition != null && condition(THIS))
{
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
this.Message = message;
}
return THIS;
}
#endregion
#region 按条件设置属性
public BaseResult SetIF<T>(Func<BaseResult, bool> condition, Action<BaseResult> setAction)
{
if (condition != null && condition(this) && setAction != null) setAction(this);
return this;
}
public Result<T> SetIF_Result<T>(Func<Result<T>, bool> condition, Action<Result<T>> setAction)
{
Result<T> THIS = this as Result<T>;
if (condition != null && condition(THIS) && setAction != null) setAction(THIS);
return THIS;
}
public ResultList<T> SetIF_AsResultList<T>(Func<ResultList<T>, bool> condition, Action<ResultList<T>> setAction)
{
ResultList<T> THIS = this as ResultList<T>;
if (condition != null && condition(THIS) && setAction != null) setAction(THIS);
return THIS;
}
#endregion
}
/// <summary>
/// 简单类
/// </summary>
public class SimpleResult : BaseResult { }
/// <summary>
/// 泛型结果
/// </summary>
public class Result<T> : BaseResult, IResult<T>
{
public T Data { get; set; }
public Result<T> SetResult(bool result, T data, string message, string title = "")
{
this.Result = result;
this.Data = data;
this.Message = message;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
return this;
}
public Result<T> SetData(T data)
{
this.Data = data;
return this;
}
}
/// <summary>
/// 泛型集合结果
/// </summary>
public class ResultList<T> : BaseResult, IResultList<T>
{
public List<T> Data { get; set; }
public ResultList()
{
this.Data = new List<T>();
}
public ResultList<T> SetResult(bool result, List<T> data, string message, string title = "")
{
this.Result = result;
this.Data = data;
this.Message = message;
if (this.Title == null || (string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(title)))
this.Title = title;
return this;
}
public ResultList<T> SetData(List<T> data)
{
this.Data = data;
return this;
}
}
作者:老板娘的神秘商店
出处:https://www.cnblogs.com/wandia/p/18502444
版权:本作品采用「Base On WTFPL License」许可协议进行许可。
都打工的,贴出来不收费,干啥不CV
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?