fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Learning
  6. {
  7.  
  8. public class Item
  9. {
  10. public string Field1 { get; set; }
  11. public string Field2 { get; set; }
  12. public int IntField { get; set; }
  13. }
  14.  
  15. public class ValidationError
  16. {
  17. public string Message { get; set; }
  18. }
  19.  
  20.  
  21. public class ValidationException : Exception
  22. {
  23. private IEnumerable<ValidationError> _errors;
  24.  
  25. public ValidationException(IEnumerable<ValidationError> errors)
  26. {
  27. _errors = errors;
  28. }
  29.  
  30. public IEnumerable<ValidationError> Errors { get { return _errors; } }
  31. }
  32.  
  33. class Program
  34. {
  35.  
  36.  
  37. static void Main(string[] args)
  38. {
  39. var items = new List<Item>();
  40.  
  41. for (int i = 0; i < 10; i++)
  42. {
  43. items.Add(new Item { Field1 = "", Field2 = "content", IntField = i });
  44. }
  45.  
  46. // Validate Items returns a list of validation errors, this can be
  47. // used to control flow of program
  48.  
  49. var validationResults = ValidateItems(items, 5);
  50.  
  51. if (validationResults.Count != 0)
  52. {
  53. Console.WriteLine($"Enountered {validationResults.Count} errors");
  54. }
  55.  
  56. try
  57. {
  58. // OperationThatValidates returns a result, so it notifies of errors
  59. // via exceptions
  60. var result = OperationThatValidates(items, 5);
  61. }
  62. catch (ValidationException ve)
  63. {
  64. Console.WriteLine($"{ve.Errors.Count()} validation failures!");
  65. }
  66. catch (AggregateException ae)
  67. {
  68. Console.WriteLine($"{ae.InnerExceptions.Count} exeptions encountered");
  69. }
  70. try
  71. {
  72. var result = OperationThatValidates(items, 30);
  73. }
  74. catch (ValidationException ve)
  75. {
  76. Console.WriteLine($"{ve.Errors.Count()} validation failures!");
  77. }
  78. catch (AggregateException ae)
  79. {
  80. Console.WriteLine($"{ae.InnerExceptions.Count} exeptions encountered");
  81. }
  82. }
  83.  
  84. public static ICollection<ValidationError> ValidateItems(List<Item> items, int requiredMinValue)
  85. {
  86. var validationErrors = new List<ValidationError>();
  87. for (int i = 0; i < items.Count; i++)
  88. {
  89. if (items[i].IntField < requiredMinValue)
  90. {
  91. validationErrors.Add(new ValidationError { Message = $"Value must be >= {requiredMinValue}" });
  92. }
  93. }
  94. return validationErrors;
  95. }
  96.  
  97. public static string OperationThatValidates(List<Item> items, int minValue)
  98. {
  99. var validationResults = ValidateItems(items, minValue);
  100. if (validationResults.Count > 0)
  101. {
  102. // Validation failed, so throw an exception. We only need to throw a single exception,
  103. // Because we haven't encountered any exceptions so far since ValidateItems doesn't throw
  104. // So use the custom exception type to return important information
  105. throw new ValidationException(validationResults);
  106. }
  107.  
  108. foreach (var item in items)
  109. {
  110. List<Exception> failures = new List<Exception>();
  111. try
  112. {
  113. OperationThatThrows(item);
  114. }
  115. catch (Exception ex)
  116. {
  117. failures.Add(ex);
  118. }
  119. if (failures.Count != 0)
  120. {
  121. // Throw AggregateException because one of more exceptions has been encountered
  122. // and we need to keep information about all of them.
  123. throw new AggregateException("Unexpected Errors", failures);
  124. }
  125. }
  126.  
  127. return "This is the result you were hoping for";
  128. }
  129.  
  130. private static void OperationThatThrows(Item item)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. }
  135. }
Success #stdin #stdout 0.05s 24040KB
stdin
Standard input is empty
stdout
Enountered 5 errors
5 validation failures!
10 validation failures!