通用結果類用於返回響應結果

老板娘的神秘商店發表於2024-10-25
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> where T : class
{
    T Data { get; set; }
}
public interface IResultList<T> where T : class
{
    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; }

    public Result<T> ToResult<T>() where T : class
    {
        return this as Result<T>;
    }
    public ResultList<T> ToResultList<T>() where T : class
    {
        return this as ResultList<T>;
    }
    public BaseResult SetResult(bool result, string message, string title = "")
    {
        this.Result = result;
        this.Title = title;
        this.Message = message;
        return this;
    }
    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 = "") where T : class
    {
        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 = "") where T : class
    {
        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>;
    }

    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 = "") where T : class
    {
        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 = "") where T : class
    {
        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>;
    }
    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 = "") where T : class
    {
        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 = "") where T : class
    {
        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 = "") where T : class
    {
        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 = "") where T : class
    {
        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 class Result<T> : BaseResult, IResult<T> where T : class
{
    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;
    }
}

public class ResultList<T> : BaseResult, IResultList<T> where T : class
{
    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;
    }
}

相關文章