ResultDto.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Quadarax.Foundation.Core.Exceptions;
  5. namespace Quadarax.Foundation.Core.Data.Interface.Entity.Dto
  6. {
  7. public abstract class ResultDto : IDto, IResult
  8. {
  9. /// <summary>
  10. /// <inheritdoc cref="IResult.IsSuccess"/>
  11. /// </summary>
  12. public bool IsSuccess => !Errors.Any();
  13. /// <summary>
  14. /// <inheritdoc cref="IResult.Errors"/>
  15. /// </summary>
  16. public IEnumerable<ErrorMessageDto> Errors { get; set; }
  17. //public IEnumerable<IError> Errors { get; set; }
  18. IEnumerable<IError> IResult.Errors => Errors;
  19. /// <summary>
  20. /// <inheritdoc cref="IResult.GetValue()"/>
  21. /// </summary>
  22. /// <returns></returns>
  23. public abstract object GetValue();
  24. protected ResultDto(IEnumerable<IError> errors)
  25. {
  26. Errors = new List<ErrorMessageDto>(errors.Select(x=>(ErrorMessageDto) x));
  27. //Errors = new List<IError>(errors);
  28. }
  29. protected ResultDto()
  30. {
  31. Errors = new List<ErrorMessageDto>();
  32. }
  33. public void AppendException(Exception e)
  34. {
  35. if (e == null) throw new ArgumentNullException(nameof(e));
  36. if (e is CodeException)
  37. {
  38. Errors = Errors.Append(new ErrorMessageDto(e));
  39. }
  40. }
  41. public AggregateException ToAggregateException()
  42. {
  43. if (IsSuccess)
  44. return null;
  45. var errors = new List<Exception>();
  46. foreach (var e in Errors)
  47. {
  48. var addExc = new Exception(e.Message);
  49. addExc.SetCode(e.Code);
  50. errors.Add(addExc);
  51. }
  52. return new AggregateException(errors);
  53. }
  54. }
  55. }