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