using System; using System.Collections.Generic; namespace Salesforce.NET { public sealed class Result : Result { public T Data { get; set; } /// /// Creates a new failed result ( is false) /// public Result() { } /// /// Creates a new result /// /// public Result(bool success) : base(success) { } /// /// Creates a new failed result with error message /// /// public Result(string error) : base(error) { } } public class Result { public bool Success { get; set; } public string Message { get; set; } /// /// Collection of errors that have occurred during the request /// public ICollection Errors { get; set; } = new List(); #region Constructor /// /// Creates a new failed result ( is false) /// public Result() { } /// /// Creates a new result /// /// public Result(bool success) => Success = success; /// /// Creates a new failed result with error message /// /// public Result(string error) => AddError(error); #endregion /// /// Adds an error to the collection and sets to false /// /// public void AddError(string error) { // force to false if previously true Success = false; AddDistinctError(error); } /// /// Adds an error to the collection and sets to false /// /// public void AddError(Exception ex) { // force to false if previously true Success = false; if (ex == null) { return; } AddDistinctError(ex.Message); } /// /// Adds a list of errors to the collection and sets to false /// /// public void AddErrors(IEnumerable errors) { // force to false if previously true Success = false; if (errors == null) { return; } foreach (var error in errors) { AddDistinctError(error); } } #region Private Methods /// /// Adds only a distinct error string to the list /// /// private void AddDistinctError(string error) { if (!string.IsNullOrWhiteSpace(error) && !Errors.Contains(error)) { Errors.Add(error); } } #endregion } }