首页 > 其他分享 >通用结果类用于返回响应结果

通用结果类用于返回响应结果

时间:2024-10-25 14:23:11浏览次数:7  
标签:返回 string Title 结果 title 响应 IsNullOrEmpty message public

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;
    }
}

标签:返回,string,Title,结果,title,响应,IsNullOrEmpty,message,public
From: https://www.cnblogs.com/wandia/p/18502444

相关文章

  • mybatis存储过程返回list
    在MyBatis中,要想通过调用存储过程返回一个List集合,你需要在Mapper接口中定义一个方法,并使用@Param注解来传递存储过程的参数。同时,你需要在MapperXML文件中配置相应的标签,并指定statementType="CALLABLE"来表明这是一个调用存储过程的语句。以下是一个简单的例子:假设你有一个存......
  • HttpClient请求结果
    好的,你想要循环遍历对象列表,对每个GET请求执行HTTP请求,并检查响应状态码是否为200。如果状态码不是200,就打印出响应体。为了实现这个功能,可以使用HttpClient库,它更现代和灵活。以下是一个示例代码,演示如何实现上述需求:首先,添加依赖(如果你使用Maven),在pom.xml中添加HttpClient库......
  • pytest 执行用例 将测试结果追加到表格并生成测试报告并推送至企微
    ReadExcel#-*-coding:utf-8-*-fromopenpyxlimportload_workbookimportosclassTestExcel():defget_TestExcel(self,file_name,sheet_name):print("======",os.getcwd())#workbook=load_workbook('Datas.xlsx......
  • 灰色代码部分:要是输入名字列表,又能输出结果,但是空列表的时候就输出不了?
    大家好,我是Python进阶者。一、前言前几天在Python白银交流群【Aciel】问了一个Python基础的问题,问题如下:灰色代码部分:要是输入名字列表,又能输出结果,但是空列表的时候就输出不了?二、实现过程这里【瑜亮老师】给了一个指导,具体如下所示:@Aciel 循环fornameinnames: 遍历......
  • Day10 函数基础+函数三种定义形式 + 函数的返回值、对象和参数 + 可变长参数
    目录0上节课复习0.1文件是什么0.2操作文件的步骤0.3open0.4指针操作0.5文件的复制1函数基础1.1函数的作用1.2函数的定义形式1.3函数定义的两个阶段2定义函数的三种形式2.1无参函数2.2有参函数2.3空函数3函数的返回值4函数对象5函数参数的应用5.1函数定义分为两个......
  • 创新!高级!【日前、日内非滚动、日内滚动调度以及实时修正】考虑需求侧响应的智慧楼宇多
     ......
  • 统计有多少不同的结果的方法
    记录一类统计结果的计数题的方法很多题目就是初始给你一个对象,然后就是你可以对这个对象进行若干次操作,然后问你能有多少结果这一类题目就是会遇到非常棘手的情况,就是可能有多个生成路径能生成同一个结果,然后统计路径就是会记重的,基本的做法是考虑给定一个序列,你能否检查其是......
  • 唯品会按图搜索唯品会商品(拍立淘)API 返回值说明
    vip.item_search_img- 按图搜索唯品会商品(拍立淘)API返回值说明1.请求参数请求参数:imgid=/xupload.vip.com/38444a01-e842-49bc-99c7-5267f6d36628_1726122211035_tmp_search.jpg&page_token=参数说明:imgid:唯品会图片地址(先调用上传图片(upload_img)接口,返回图片地址)page......
  • 关键词搜索唯品会商品列表API返回值说明
    vip.item_search-按关键字搜索vip商品数据接口返回值说明1.请求参数请求参数:q=鞋子&start_price=&end_price=&page=&cat=&discount_only=&sort=&page_size=&seller_info=&nick=&ppath=参数说明:q:搜索关键字cat:分类IDstart_price:开始价格end_price:结束价格sort:排序[......
  • paramiko执行多个操作系统命令并返回
     @csrf_exemptdefexecute_multi_remote_command(request):#request_dict=json.loads(request.body)#cmd=request_dict.get("cmd")ip='192.168.1.14'ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(pa......