通用结果类用于返回响应结果
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;
}
}
转载保留源出处即可,商业使用请自行鉴别,使用本博客中公开内容做任何违法犯罪于本作者无关