PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Commands/CommandResult.cs

#
C# | 89 lines | 37 code | 8 blank | 44 comment | 10 complexity | 5f5860586021b4fdb41ea1b49d7a3245 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. #region License
  2. //
  3. // Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
  4. //
  5. // Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
  6. // With one exception :
  7. // Commercial libraries that is based partly or fully on Bifrost and is sold commercially,
  8. // must obtain a commercial license.
  9. //
  10. // You may not use this file except in compliance with the License.
  11. // You may obtain a copy of the license at
  12. //
  13. // http://bifrost.codeplex.com/license
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. #endregion
  22. using System;
  23. using System.Collections.Generic;
  24. using System.ComponentModel.DataAnnotations;
  25. using System.Linq;
  26. namespace Bifrost.Commands
  27. {
  28. /// <summary>
  29. /// Represents the result from the <see cref="ICommandCoordinator">CommandCoordinator</see>
  30. /// </summary>
  31. public class CommandResult
  32. {
  33. /// <summary>
  34. /// Gets or sets the ValidationResults generated during handling of a command
  35. /// </summary>
  36. public IEnumerable<ValidationResult> ValidationResults { get; set; }
  37. /// <summary>
  38. /// Gets or sets the exception, if any, that occured during a handle
  39. /// </summary>
  40. public Exception Exception { get; set; }
  41. /// <summary>
  42. /// Gets the success state of the result
  43. ///
  44. /// If there are invalid validationresult, this is false.
  45. /// If an exception occured, this is false.
  46. /// Otherwise, its true
  47. /// </summary>
  48. public bool Success
  49. {
  50. get { return null == Exception && !Invalid; }
  51. }
  52. /// <summary>
  53. /// Gets the validation state of the result
  54. ///
  55. /// If there are any validationresults this returns false, true if not
  56. /// </summary>
  57. public bool Invalid
  58. {
  59. get { return ValidationResults != null && ValidationResults.Count() > 0; }
  60. }
  61. /// <summary>
  62. /// Merges this instance of a CommandResult with another
  63. /// </summary>
  64. /// <param name="commandResultToMerge">The <see cref="CommandResult"/> to merge with the current instance</param>
  65. public void MergeWith(CommandResult commandResultToMerge)
  66. {
  67. if (Exception == null)
  68. Exception = commandResultToMerge.Exception;
  69. if (commandResultToMerge.ValidationResults == null)
  70. return;
  71. if (ValidationResults == null)
  72. {
  73. ValidationResults = commandResultToMerge.ValidationResults;
  74. return;
  75. }
  76. var validationResults = ValidationResults.ToList();
  77. validationResults.AddRange(commandResultToMerge.ValidationResults);
  78. ValidationResults = validationResults.ToArray();
  79. }
  80. }
  81. }