(更新时间)2021年5月30日 商城高并发秒杀系统(.NET Core版) 11-通用结果集的封装

举报
愚公搬代码 发表于 2021/10/20 00:56:31 2021/10/20
【摘要】 一:通用结果集的封装 /// <summary> /// 通用结果 /// </summary> public class CommonResult { public c...

一:通用结果集的封装

/// <summary>
/// 通用结果
/// </summary>
public class CommonResult
{
    public const string SUCCESS = "0";
    public string ErrorNo { set; get; } // 是否成功状态
    public string ErrorInfo { set; get; } // 失败信息
    public IDictionary<string, object> resultDic { set; get; }// 用于非结果集返回
    public IList<IDictionary<string, object>> resultList { set; get; }// 用于结果集返回

    public dynamic Result { set; get; }// 返回动态结果(通用)

    public CommonResult()
    {
        resultDic = new Dictionary<string, object>();
        resultList = new List<IDictionary<string, object>>();
    }

    public CommonResult(string errorNo, string errorInfo)
    {
        this.ErrorNo = errorNo;
        this.ErrorInfo = errorInfo;
        resultDic = new Dictionary<string, object>();
        resultList = new List<IDictionary<string, object>>();
    }

    public CommonResult(string errorNo, string erroInfo, IDictionary<string, object> resultDic, IList<IDictionary<string, object>> resultList) : this(errorNo, erroInfo)
    {
        this.resultDic = resultDic;
        this.resultList = resultList;
        this.resultDic = resultDic;
        this.resultList = resultList;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
/// <summary>
/// 页面通用结果包装器
/// </summary>
public class FrontResultWapper : IAsyncResultFilter
{
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (context.Result is ObjectResult objectResult)
        {
            int? StatusCode = objectResult.StatusCode;
            if (StatusCode == 200
                || StatusCode == 201
                || StatusCode == 202
                || !StatusCode.HasValue)
            {
                // 1、包装正常结果
                objectResult.Value = WrapSuccessResult(objectResult.Value);
            }
            else
            {
                // 2、包装异常结果
                objectResult.Value = WrapFailResult(objectResult);
            }
        }
        await next();
    }

    private object WrapFailResult(ObjectResult objectResult)
    {
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = objectResult.StatusCode;
        if (objectResult.Value is string info)
        {
            // 1、字符串异常信息
            warpResult.ErrorInfo = info;
        }
        else
        {
            // 2、类型异常信息
            warpResult.ErrorInfo = new JsonResult(objectResult.Value).Value;
        }

        return warpResult;
    }

    private object WrapSuccessResult(object value)
    {
        // 1、创建返回结果
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = "0";
        warpResult.ErrorInfo = "";

        // 2、获取结果(输出到页面:结果集List<>,非结果集Dic)
        if (value.GetType().Name.Contains("List"))
        {
            // 转换成json
            warpResult.ResultList = new JsonResult(value).Value;
        }
        else if (value.GetType().Name.Contains("Dictionary"))
        {
            //2.1 判断是否含有ErrorInfo
            IDictionary dictionary = (IDictionary)value;
            if (dictionary.Contains("ErrorInfo"))
            {
                warpResult.ErrorNo = dictionary["ErrorNo"];
                warpResult.ErrorInfo = dictionary["ErrorInfo"];

                // 2.2 删除字典里面的ErrorNo,ErrorInfo
                dictionary.Remove("ErrorNo");
                dictionary.Remove("ErrorInfo");
            }
            warpResult.ResultDic = new JsonResult(value).Value;
        }
        else
        {
            warpResult.ResultDic = new JsonResult(value).Value;
        }
        return warpResult;
    }

    /// <summary>
    ///  对象转换成字典
    /// </summary>
    /// <param name="value2"></param>
    /// <returns></returns>
    private IDictionary<string, object> ToDictionary(object value2)
    {
        IDictionary<string, object> valuePairs = new Dictionary<string, object>();
        // 1、获取反射类型
        Type type = value2.GetType();

        // 2、获取所有反射属性
        PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // 3、遍历PropertyInfo
        foreach (PropertyInfo info in propertyInfos)
        {
            valuePairs.Add(info.Name, Convert.ToString(info.GetValue(value2)));
        }

        return valuePairs;
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
/// <summary>
/// action执行结果包装过滤器
/// </summary>
public class FrontResultWrapperFilter : IAsyncResultFilter
{
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (context.Result is ObjectResult objectResult)
        {
            int? StatusCode = objectResult.StatusCode;
            if (StatusCode == 200
                || StatusCode == 201
                || StatusCode == 202
                || !StatusCode.HasValue)
            {
                // 1、包装正常结果
                objectResult.Value = WrapSuccessResult(objectResult.Value);
            }
            else
            {
                // 2、包装异常结果
                objectResult.Value = WrapFailResult(objectResult);
            }
        }
        await next();
    }

    private object WrapFailResult(ObjectResult objectResult)
    {
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = objectResult.StatusCode;
        if (objectResult.Value is string info)
        {
            // 1、字符串异常信息
            warpResult.ErrorInfo = info;
        }
        else
        {
            // 2、类型异常信息
            warpResult.ErrorInfo = new JsonResult(objectResult.Value).Value;
        }

        return warpResult;
    }

    private object WrapSuccessResult(object value)
    {
        // 1、创建返回结果
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = "0";
        warpResult.ErrorInfo = "";

        // 2、获取结果
        if (value.GetType().Name.Contains("List"))
        {
            // 转换成json
            warpResult.ResultList = new JsonResult(value).Value;
        }
        else if (value.GetType().Name.Contains("Dictionary"))
        {
            //2.1 判断是否含有ErrorInfo
            IDictionary dictionary = (IDictionary)value;
            if (dictionary.Contains("ErrorInfo"))
            {
                warpResult.ErrorNo = dictionary["ErrorNo"];
                warpResult.ErrorInfo = dictionary["ErrorInfo"];

                // 2.2 删除字典里面的ErrorNo,ErrorInfo
                dictionary.Remove("ErrorNo");
                dictionary.Remove("ErrorInfo");
            }
            warpResult.ResultDic = new JsonResult(value).Value;
        }
        else
        {
            warpResult.ResultDic = new JsonResult(value).Value;
        }
        return warpResult;
    }

    /// <summary>
    ///  对象转换成字典
    /// </summary>
    /// <param name="value2"></param>
    /// <returns></returns>
    private IDictionary<string, object> ToDictionary(object value2)
    {
        IDictionary<string, object> valuePairs = new Dictionary<string, object>();
        // 1、获取反射类型
        Type type = value2.GetType();

        // 2、获取所有反射属性
        PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // 3、遍历PropertyInfo
        foreach (PropertyInfo info in propertyInfos)
        {
            valuePairs.Add(info.Name, Convert.ToString(info.GetValue(value2)));
        }

        return valuePairs;
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
/// <summary>
/// 通过结果包装器
/// </summary>
public class MiddlewareResultWapper : IAsyncResultFilter
{
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (context.Result is ObjectResult objectResult)
        {
            int? StatusCode = objectResult.StatusCode;
            if (StatusCode == 200
                || StatusCode == 201
                || StatusCode == 202
                || !StatusCode.HasValue)
            {
                // 1、包装正常结果
                objectResult.Value = WrapSuccessResult(objectResult.Value);
            }
            else
            {
                // 2、包装异常结果
                objectResult.Value = WrapFailResult(objectResult);
            }
        }
        await next();
    }

    private object WrapFailResult(ObjectResult objectResult)
    {
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = objectResult.StatusCode;
        if (objectResult.Value is string info)
        {
            // 1、字符串异常信息
            warpResult.ErrorInfo = info;
        }
        else
        {
            // 2、类型异常信息
            warpResult.ErrorInfo = new JsonResult(objectResult.Value).Value;
        }

        return warpResult;
    }

    private object WrapSuccessResult(object value)
    {
        // 1、创建返回结果
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = "0";
        warpResult.ErrorInfo = "";

        // 2、判断是否为字典
        if (value.GetType().Name.Contains("Dictionary"))
        {
            //2.1 判断是否含有ErrorInfo
            IDictionary dictionary = (IDictionary)value;
            if (dictionary.Contains("ErrorInfo"))
            {
                warpResult.ErrorNo = dictionary["ErrorNo"];
                warpResult.ErrorInfo = dictionary["ErrorInfo"];

                // 2.2 删除字典里面的ErrorNo,ErrorInfo
                dictionary.Remove("ErrorNo");
                dictionary.Remove("ErrorInfo");
            }
        }

        // 3、获取结果
        warpResult.Result = new JsonResult(value).Value;

        return warpResult;
    }

    /// <summary>
    ///  对象转换成字典
    /// </summary>
    /// <param name="value2"></param>
    /// <returns></returns>
    private IDictionary<string, object> ToDictionary(object value2)
    {
        IDictionary<string, object> valuePairs = new Dictionary<string, object>();
        // 1、获取反射类型
        Type type = value2.GetType();

        // 2、获取所有反射属性
        PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // 3、遍历PropertyInfo
        foreach (PropertyInfo info in propertyInfos)
        {
            valuePairs.Add(info.Name, Convert.ToString(info.GetValue(value2)));
        }

        return valuePairs;
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
/// <summary>
/// action执行结果包装过滤器
/// </summary>
public class MiddlewareResultWrapperFilter : IAsyncResultFilter
{
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        if (context.Result is ObjectResult objectResult)
        {
            int? StatusCode = objectResult.StatusCode;
            if (StatusCode == 200
                || StatusCode == 201
                || StatusCode == 202
                || !StatusCode.HasValue)
            {
                // 1、包装正常结果
                objectResult.Value = WrapSuccessResult(objectResult.Value);
            }
            else
            {
                // 2、包装异常结果
                objectResult.Value = WrapFailResult(objectResult);
            }
        }
        await next();
    }

    private object WrapFailResult(ObjectResult objectResult)
    {
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = objectResult.StatusCode;
        if (objectResult.Value is string info)
        {
            // 1、字符串异常信息
            warpResult.ErrorInfo = info;
        }
        else
        {
            // 2、类型异常信息
            warpResult.ErrorInfo = new JsonResult(objectResult.Value).Value;
        }

        return warpResult;
    }

    private object WrapSuccessResult(object value)
    {
        // 1、创建返回结果
        dynamic warpResult = new ExpandoObject();
        warpResult.ErrorNo = "0";
        warpResult.ErrorInfo = "";

        // 2、判断是否为字典
        if (value.GetType().Name.Contains("Dictionary"))
        {
            //2.1 判断是否含有ErrorInfo
            IDictionary dictionary = (IDictionary)value;
            if (dictionary.Contains("ErrorInfo"))
            {
                warpResult.ErrorNo = dictionary["ErrorNo"];
                warpResult.ErrorInfo = dictionary["ErrorInfo"];

                // 2.2 删除字典里面的ErrorNo,ErrorInfo
                dictionary.Remove("ErrorNo");
                dictionary.Remove("ErrorInfo");
            }
        }

        // 3、获取结果
        warpResult.Result = new JsonResult(value).Value;

        return warpResult;
    }

    /// <summary>
    ///  对象转换成字典
    /// </summary>
    /// <param name="value2"></param>
    /// <returns></returns>
    private IDictionary<string, object> ToDictionary(object value2)
    {
        IDictionary<string, object> valuePairs = new Dictionary<string, object>();
        // 1、获取反射类型
        Type type = value2.GetType();

        // 2、获取所有反射属性
        PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // 3、遍历PropertyInfo
        foreach (PropertyInfo info in propertyInfos)
        {
            valuePairs.Add(info.Name, Convert.ToString(info.GetValue(value2)));
        }

        return valuePairs;
    }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/117406384

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。