添加dto模式的demo测试

This commit is contained in:
chenchun
2023-01-01 23:51:05 +08:00
parent b9384afd5d
commit 5d7d115910
55 changed files with 582 additions and 98 deletions

View File

@@ -4,10 +4,10 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System
namespace Yi.Framework.Common.Attribute
{
[AttributeUsage(AttributeTargets.Property)]
public class AutowiredAttribute : Attribute
public class AutowiredAttribute : System.Attribute
{
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Yi.Framework.Common.Enum;
namespace Yi.Framework.Common.Exceptions;
[Serializable]
public class BusinessException : Exception,
IHasErrorCode,
IHasErrorDetails,
IHasLogLevel
{
public ResultCodeEnum Code { get; set; }
public string Details { get; set; }
public LogLevel LogLevel { get; set; }
public BusinessException(
ResultCodeEnum code = ResultCodeEnum.NotSuccess,
string message = null,
string details = null,
Exception innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(message, innerException)
{
Code = code;
Details = details;
LogLevel = logLevel;
}
/// <summary>
/// 序列化参数的构造函数
/// </summary>
public BusinessException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
public BusinessException WithData(string name, object value)
{
Data[name] = value;
return this;
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Runtime.ExceptionServices;
using Microsoft.Extensions.Logging;
using Yi.Framework.Common.Enum;
namespace Yi.Framework.Common.Exceptions;
/// <summary>
/// <see cref="Exception"/><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// ʹ<><CAB9> <see cref="ExceptionDispatchInfo.Capture"/> <20>ٴ<EFBFBD><D9B4>׳<EFBFBD><D7B3>
/// </summary>
/// <param name="exception">Exception to be re-thrown</param>
public static void ReThrow(this Exception exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
/// <summary>
/// <20><>ȡ<EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD>ȼ<EFBFBD>
/// </summary>
/// <param name="exception"></param>
/// <param name="defaultLevel"></param>
/// <returns></returns>
public static LogLevel GetLogLevel(this Exception exception, LogLevel defaultLevel = LogLevel.Error)
{
return (exception as IHasLogLevel)?.LogLevel ?? defaultLevel;
}
/// <summary>
/// <20><>ȡ<EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="exception"></param>
/// <param name="defaultLevel"></param>
/// <returns></returns>
public static ResultCodeEnum GetLogErrorCode(this Exception exception, ResultCodeEnum defaultCode = ResultCodeEnum.NotSuccess)
{
return (exception as IHasErrorCode)?.Code ?? defaultCode;
}
}

View File

@@ -0,0 +1,8 @@
using Yi.Framework.Common.Enum;
namespace Yi.Framework.Common.Exceptions;
public interface IHasErrorCode
{
ResultCodeEnum Code { get; }
}

View File

@@ -0,0 +1,6 @@
namespace Yi.Framework.Common.Exceptions;
public interface IHasErrorDetails
{
string Details { get; }
}

View File

@@ -0,0 +1,14 @@
using Microsoft.Extensions.Logging;
namespace Yi.Framework.Common.Exceptions;
/// <summary>
/// <20><><EFBFBD><EFBFBD><see cref="LogLevel"/> <20><><EFBFBD>ԵĽӿ<C4BD>
/// </summary>
public interface IHasLogLevel
{
/// <summary>
/// Log severity.
/// </summary>
LogLevel LogLevel { get; set; }
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Yi.Framework.Common.Enum;
namespace Yi.Framework.Common.Exceptions;
/// <summary>
/// <20>û<EFBFBD><C3BB>Ѻ<EFBFBD><D1BA>
/// </summary>
[Serializable]
public class UserFriendlyException : BusinessException
{
public UserFriendlyException(
string message,
ResultCodeEnum code = ResultCodeEnum.NotSuccess,
string details = null,
Exception innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(
code,
message,
details,
innerException,
logLevel)
{
Details = details;
}
/// <summary>
/// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><ECBAAF>
/// </summary>
public UserFriendlyException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
}

View File

@@ -7,66 +7,66 @@ namespace Yi.Framework.Common.Models
public class Result
{
public static IStringLocalizer<LocalLanguage> _local;
public ResultCodeEnum code { get; set; }
public ResultCodeEnum Code { get; set; }
public bool status { get; set; }
public string message { get; set; }
public object data { get; set; }
public static Result Expire(ResultCodeEnum code, string msg="")
public bool Status { get; set; }
public string Message { get; set; }
public object Data { get; set; }
public static Result Expire(ResultCodeEnum Code, string msg="")
{
return new Result() { code = code, status=false, message = Get(msg, "token_expiration") };
return new Result() { Code = Code, Status=false, Message = Get(msg, "token_expiration") };
}
public static Result Error(string msg = "")
{
return new Result() { code = ResultCodeEnum.NotSuccess,status=false, message =Get(msg, "fail") };
return new Result() { Code = ResultCodeEnum.NotSuccess, Status = false, Message = Get(msg, "fail") };
}
public static Result Success(string msg = "")
{
return new Result() { code = ResultCodeEnum.Success,status=true, message =Get( msg, "succeed" )};
return new Result() { Code = ResultCodeEnum.Success, Status = true, Message = Get( msg, "succeed" )};
}
public static Result SuccessError(string msg = "")
{
return new Result() { code = ResultCodeEnum.Success, status = false, message = Get(msg, "fail") };
return new Result() { Code = ResultCodeEnum.Success, Status = false, Message = Get(msg, "fail") };
}
public static Result UnAuthorize(string msg = "")
{
return new Result() { code = ResultCodeEnum.NoPermission,status=false, message = Get(msg, "unAuthorize") };
return new Result() { Code = ResultCodeEnum.NoPermission, Status = false, Message = Get(msg, "unAuthorize") };
}
public Result SetStatus(bool _status)
public Result SetStatus(bool _Status)
{
if (_status)
if (_Status)
{
this.code = ResultCodeEnum.Success;
this.message = "操作成功";
this.Code = ResultCodeEnum.Success;
this.Message = "操作成功";
}
else
{
this.code = code = ResultCodeEnum.NotSuccess;
this.message = "操作失败";
this.Code = ResultCodeEnum.NotSuccess;
this.Message = "操作失败";
}
this.status = _status;
this.Status = _Status;
return this;
}
public Result SetData(object obj)
{
this.data = obj;
this.Data = obj;
return this;
}
public Result SetCode(ResultCodeEnum Code)
{
this.code = Code;
this.Code = Code;
return this;
}
public Result StatusFalse()
{
this.status = false;
this.Status = false;
return this;
}
public Result StatusTrue()
{
this.status = true;
this.Status = true;
return this;
}
@@ -81,31 +81,31 @@ namespace Yi.Framework.Common.Models
}
public class Result<T>
{
public ResultCodeEnum code { get; set; }
public string message { get; set; }
public T data { get; set; }
public ResultCodeEnum Code { get; set; }
public string Message { get; set; }
public T Data { get; set; }
public static Result<T> Error(string msg = "fail")
{
return new Result<T>() { code = ResultCodeEnum.NotSuccess, message = msg };
return new Result<T>() { Code = ResultCodeEnum.NotSuccess, Message = msg };
}
public static Result<T> Success(string msg = "succeed")
{
return new Result<T>() { code = ResultCodeEnum.Success, message = msg };
return new Result<T>() { Code = ResultCodeEnum.Success, Message = msg };
}
public static Result<T> UnAuthorize(string msg = "unAuthorize")
{
return new Result<T>() { code = ResultCodeEnum.NoPermission, message = msg };
return new Result<T>() { Code = ResultCodeEnum.NoPermission, Message = msg };
}
public Result<T> SetData(T TValue)
{
this.data = TValue;
this.Data = TValue;
return this;
}
public Result<T> SetCode(ResultCodeEnum Code)
{
this.code = Code;
this.Code = Code;
return this;
}
}