using System;
using System.Diagnostics;
namespace iiie.Logs.DBO
{
///
/// Represent the result of an operation, with details in case of failure and data in case of success
///
/// The data type
public class OpResult
{
///
/// Status of the operation
///
public ResultStatus Status { get; set; }
///
/// True if Status is Success, false otherwise
///
public bool Success
{
get
{
return Status == ResultStatus.Success;
}
}
///
/// Details on the error, or null if error details must not be shown to the user
/// Only relevent when Success is false
///
public string PublicDetails { get; set; }
///
/// Technical details on the error
/// Only relevent when Success is false
///
public string PrivateDetails { get; set; }
public StackFrame[] StackFrames { get; set; }
///
/// The data returned by the call
/// Only relevent if Success is true
///
public T Data { get; set; }
///
/// Create and returns a new OpResult with
/// {Public,Private}Details = null, Data = data, Status = true
/// Used for successfull operations
///
public static OpResult Ok(T data)
{
return new OpResult
{
PublicDetails = null,
PrivateDetails = null,
StackFrames = new StackTrace().GetFrames(),
Data = data,
Status = ResultStatus.Success
};
}
///
/// Create and returns a new OpResult with
/// PrivateDetails = privateDetails, Data = null, Status = status
/// If publicDetails == "", privateDetails will be used
/// Used for all errors (invalid input, ...)
///
public static OpResult Error(ResultStatus status, string privateDetails, string publicDetails = null)
{
return new OpResult
{
PublicDetails = publicDetails == "" ? privateDetails : publicDetails,
PrivateDetails = privateDetails,
StackFrames = new StackTrace().GetFrames(),
Data = default(T),
Status = status
};
}
///
/// Create and returns a new OpResult with
/// PrivateDetails = e to string, Data = null, Status = status
/// If publicDetails == "", privateDetails will be used
/// Used for all errors (invalid input, ...)
///
public static OpResult Error(ResultStatus status, Exception e, string userDetails = null)
{
string message = "";
Exception inner = e;
while (inner != null)
{
message += inner.GetType().Name + ": " + inner.Message + (inner.InnerException != null ? "\n" : "");
inner = inner.InnerException;
}
return Error(status, message, userDetails);
}
///
/// Translate an error to another type
///
/// The original type
/// The original error
/// The original error with the type T
public static OpResult Error(OpResult other)
{
return new OpResult
{
PublicDetails = other.PublicDetails,
PrivateDetails = other.PrivateDetails,
StackFrames = other.StackFrames,
Data = default(T),
Status = other.Status
};
}
///
/// Translate an error to another type
///
/// The original type
/// The original error with the type T
public OpResult To()
{
return OpResult.Error(this);
}
///
/// Implicit converion to bool
///
/// The result to be converted
/// True if Success, false otherwise
public static implicit operator bool(OpResult res)
{
return res.Success;
}
protected OpResult()
{
}
}
}