Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuResult.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. namespace Luticate2.Utils.Dbo.Result
  3. {
  4. public class LuResult<T>
  5. {
  6. public LuStatus Status { get; set; }
  7. public bool Success => Status == LuStatus.Success;
  8. public string PublicDetails { get; set; }
  9. public string PrivateDetails { get; set; }
  10. public string StackTrace { get; set; }
  11. public Exception Exception { get; set; }
  12. public T Data { get; set; }
  13. protected LuResult()
  14. {
  15. }
  16. public static LuResult<T> Ok(T data)
  17. {
  18. return new LuResult<T>
  19. {
  20. StackTrace = new Exception().StackTrace,
  21. Data = data,
  22. Status = LuStatus.Success
  23. };
  24. }
  25. public static LuResult<T> Error(LuStatus status, string privateDetails, string publicDetails = null)
  26. {
  27. return new LuResult<T>
  28. {
  29. PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
  30. PrivateDetails = privateDetails,
  31. StackTrace = new Exception().StackTrace,
  32. Status = status
  33. };
  34. }
  35. public static LuResult<T> Error(LuStatus status, Exception e, string publicDetails = null)
  36. {
  37. return new LuResult<T>
  38. {
  39. PublicDetails = publicDetails,
  40. PrivateDetails = e.ToString(),
  41. StackTrace = new Exception().StackTrace,
  42. Exception = e,
  43. Status = status
  44. };
  45. }
  46. public static LuResult<T> Error<T2>(LuResult<T2> other)
  47. {
  48. return new LuResult<T>
  49. {
  50. Status = other.Status,
  51. PublicDetails = other.PublicDetails,
  52. PrivateDetails = other.PrivateDetails,
  53. StackTrace = other.StackTrace,
  54. Exception = other.Exception
  55. };
  56. }
  57. public LuResult<T2> To<T2>()
  58. {
  59. return LuResult<T2>.Error(this);
  60. }
  61. public LuResult<T2> To<T2>(Func<T, T2> convert)
  62. {
  63. return new LuResult<T2>
  64. {
  65. Data = Success ? convert(Data) : default(T2),
  66. Status = Status,
  67. PublicDetails = PublicDetails,
  68. PrivateDetails = PrivateDetails,
  69. StackTrace = StackTrace,
  70. Exception = Exception
  71. };
  72. }
  73. public static implicit operator bool(LuResult<T> res)
  74. {
  75. return res.Success;
  76. }
  77. }
  78. }