PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms 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
 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel.DataAnnotations;
25using System.Linq;
26
27namespace Bifrost.Commands
28{
29    /// <summary>
30    /// Represents the result from the <see cref="ICommandCoordinator">CommandCoordinator</see>
31    /// </summary>
32    public class CommandResult
33    {
34        /// <summary>
35        /// Gets or sets the ValidationResults generated during handling of a command
36        /// </summary>
37        public IEnumerable<ValidationResult> ValidationResults { get; set; }
38
39        /// <summary>
40        /// Gets or sets the exception, if any, that occured during a handle
41        /// </summary>
42        public Exception Exception { get; set; }
43
44        /// <summary>
45        /// Gets the success state of the result
46        ///
47        /// If there are invalid validationresult, this is false.
48        /// If an exception occured, this is false.
49        /// Otherwise, its true
50        /// </summary>
51        public bool Success
52        {
53            get { return null == Exception && !Invalid; }
54        }
55
56        /// <summary>
57        /// Gets the validation state of the result
58        ///
59        /// If there are any validationresults this returns false, true if not
60        /// </summary>
61        public bool Invalid
62        {
63            get { return ValidationResults != null && ValidationResults.Count() > 0; }
64        }
65
66        /// <summary>
67        /// Merges this instance of a CommandResult with another
68        /// </summary>
69        /// <param name="commandResultToMerge">The <see cref="CommandResult"/> to merge with the current instance</param>
70        public void MergeWith(CommandResult commandResultToMerge)
71        {
72            if (Exception == null)
73                Exception = commandResultToMerge.Exception;
74
75            if (commandResultToMerge.ValidationResults == null) 
76                return;
77
78            if (ValidationResults == null)
79            {
80                ValidationResults = commandResultToMerge.ValidationResults;
81                return;
82            }
83
84            var validationResults = ValidationResults.ToList();
85            validationResults.AddRange(commandResultToMerge.ValidationResults);
86            ValidationResults = validationResults.ToArray();
87        }
88    }
89}