using System;
namespace Umbraco.Core
{
///
/// Represents the result of an operation attempt.
///
/// The type of the attempted operation result.
/// The type of the attempted operation status.
[Serializable]
public struct Attempt
{
///
/// Gets a value indicating whether this was successful.
///
public bool Success { get; }
///
/// Gets the exception associated with an unsuccessful attempt.
///
public Exception Exception { get; }
///
/// Gets the attempt result.
///
public TResult Result { get; }
///
/// Gets the attempt status.
///
public TStatus Status { get; }
// private - use Succeed() or Fail() methods to create attempts
private Attempt(bool success, TResult result, TStatus status, Exception exception)
{
Success = success;
Result = result;
Status = status;
Exception = exception;
}
///
/// Creates a successful attempt.
///
/// The status of the attempt.
/// The successful attempt.
public static Attempt Succeed(TStatus status)
{
return new Attempt(true, default(TResult), status, null);
}
///
/// Creates a successful attempt with a result.
///
/// The status of the attempt.
/// The result of the attempt.
/// The successful attempt.
public static Attempt Succeed(TStatus status, TResult result)
{
return new Attempt(true, result, status, null);
}
///
/// Creates a failed attempt.
///
/// The status of the attempt.
/// The failed attempt.
public static Attempt Fail(TStatus status)
{
return new Attempt(false, default(TResult), status, null);
}
///
/// Creates a failed attempt with an exception.
///
/// The status of the attempt.
/// The exception causing the failure of the attempt.
/// The failed attempt.
public static Attempt Fail(TStatus status, Exception exception)
{
return new Attempt(false, default(TResult), status, exception);
}
///
/// Creates a failed attempt with a result.
///
/// The status of the attempt.
/// The result of the attempt.
/// The failed attempt.
public static Attempt Fail(TStatus status, TResult result)
{
return new Attempt(false, result, status, null);
}
///
/// Creates a failed attempt with a result and an exception.
///
/// The status of the attempt.
/// The result of the attempt.
/// The exception causing the failure of the attempt.
/// The failed attempt.
public static Attempt Fail(TStatus status, TResult result, Exception exception)
{
return new Attempt(false, result, status, exception);
}
///
/// Creates a successful or a failed attempt.
///
/// A value indicating whether the attempt is successful.
/// The status of the successful attempt.
/// The status of the failed attempt.
/// The attempt.
public static Attempt If(bool condition, TStatus succStatus, TStatus failStatus)
{
return new Attempt(condition, default(TResult), condition ? succStatus : failStatus, null);
}
///
/// Creates a successful or a failed attempt, with a result.
///
/// A value indicating whether the attempt is successful.
/// The status of the successful attempt.
/// The status of the failed attempt.
/// The result of the attempt.
/// The attempt.
public static Attempt If(bool condition, TStatus succStatus, TStatus failStatus, TResult result)
{
return new Attempt(condition, result, condition ? succStatus : failStatus, null);
}
///
/// Implicitly operator to check if the attempt was successful without having to access the 'success' property
///
///
///
public static implicit operator bool(Attempt a)
{
return a.Success;
}
}
}