Result.cs 1.7 KB

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