ResultDto.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. namespace Safeguard.Dtos
  2. {
  3. public enum CodeEnum
  4. {
  5. Success,
  6. Fail,
  7. Error
  8. }
  9. public class ResultDto
  10. {
  11. public ResultDto(CodeEnum? code, string? message)
  12. {
  13. Code = code;
  14. Message = message;
  15. }
  16. public CodeEnum? Code { get; set; }
  17. public string? Message { get; set; }
  18. public static ResultDto Success()
  19. {
  20. return new ResultDto(CodeEnum.Success, "Ok!");
  21. }
  22. public static ResultDto Fail(string message)
  23. {
  24. return new ResultDto(CodeEnum.Fail, message);
  25. }
  26. public static ResultDto Error(string error)
  27. {
  28. return new ResultDto(CodeEnum.Error, error);
  29. }
  30. }
  31. public class ResultDto<T> : ResultDto
  32. {
  33. public ResultDto(CodeEnum code, string? message)
  34. : base(code, message)
  35. {
  36. }
  37. public ResultDto(CodeEnum code, string? message, T? data)
  38. : base(code, message)
  39. {
  40. Data = data;
  41. }
  42. public T? Data { get; set; }
  43. public static new ResultDto<T> Success()
  44. {
  45. return new ResultDto<T>(CodeEnum.Success, "Ok!");
  46. }
  47. public static ResultDto<T> Success(T data)
  48. {
  49. return new ResultDto<T>(CodeEnum.Success, "Ok!", data);
  50. }
  51. public static new ResultDto<T> Fail(string message)
  52. {
  53. return new ResultDto<T>(CodeEnum.Fail, message);
  54. }
  55. public static new ResultDto<T> Error(string message)
  56. {
  57. return new ResultDto<T>(CodeEnum.Error, message);
  58. }
  59. }
  60. }