using System.Net;
using System.Runtime.Serialization;
using System.Xml.Linq;
using EasyDevCore.Common;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace EasyDevCore.Remote
{
///
/// Enum ResultMode
///
public enum ResultMode
{
///
/// The success
///
Success = 0,
///
/// The failed
///
Failed = 1,
///
/// The information
///
Info = 2,
///
/// The warning
///
Warning = 4,
///
/// The Confirmation
///
Confirmation = 8,
///
/// The error
///
Error = 16
}
///
///
///
public interface IResult
{
///
/// Gets a value indicating whether this instance is error.
///
///
/// true if this instance is error; otherwise, false.
///
bool IsError { get; }
///
/// Gets a value indicating whether this instance is success.
///
///
/// true if this instance is success; otherwise, false.
///
bool IsSuccess { get; }
///
/// Gets or sets the message.
///
///
/// The message.
///
string Message { get; set; }
///
/// Gets or sets the result code.
///
///
/// The result code.
///
string ResultCode { get; set; }
///
/// Gets or sets the result status.
///
///
/// The result status.
///
ResultMode ResultStatus { get; set; }
}
///
///
///
/// The type of the data.
public interface IResult : IResult
{
///
/// Gets or sets the data.
///
///
/// The data.
///
TData Data { get; set; }
///
/// Maps the specified convert.
///
/// The type of the map data.
/// The convert.
///
public abstract Result Map(Func convert);
}
///
///
///
///
public class Result: IResult
{
///
/// Gets or sets the result status.
///
///
/// The result status.
///
public ResultMode ResultStatus { get; set; } = ResultMode.Success;
///
/// Gets or sets the result code.
///
///
/// The result code.
///
public string ResultCode { get; set; } = "";
///
/// Gets or sets the message.
///
///
/// The message.
///
public string Message { get; set; } = "";
///
/// Gets a value indicating whether this instance is success.
///
///
/// true if this instance is success; otherwise, false.
///
public virtual bool IsSuccess => (ResultStatus != ResultMode.Error && ResultStatus != ResultMode.Failed);
///
/// Gets or sets the error.
///
///
/// The error.
///
[IgnoreDataMember]
public Exception Error { get; set; }
///
/// Gets a value indicating whether this instance is error.
///
///
/// true if this instance is error; otherwise, false.
///
public virtual bool IsError => ResultStatus == ResultMode.Error || Error != null;
///
/// Gets or sets the HTTP result status.
///
///
/// The HTTP result status.
///
[IgnoreDataMember]
public HttpStatusCode HttpResultStatus { get; set; } = HttpStatusCode.OK;
///
/// Initializes a new instance of the class.
///
public Result()
{
}
///
/// Initializes a new instance of the class.
///
/// The error.
public Result(Exception error)
{
ResultCode = error.HResult.ToString();
ResultStatus = ResultMode.Error;
Message = error.Message;
HttpResultStatus = HttpStatusCode.BadRequest;
Error = error;
}
///
/// Initializes a new instance of the class.
///
/// The result status.
/// The result code.
/// The message.
public Result(ResultMode resultStatus = ResultMode.Success, string resultCode = "", string message = "")
{
ResultStatus = resultStatus;
ResultCode = resultCode;
Message = message;
}
}
///
///
///
/// The type of the value.
public class Result : Result, IResult
{
///
/// Gets or sets the data.
///
///
/// The data.
///
public TData Data { get ; set ; }
///
/// Initializes a new instance of the class.
///
public Result()
{
}
///
/// Initializes a new instance of the class.
///
/// The error.
public Result(Exception error)
{
ResultCode = error.HResult.ToString();
ResultStatus = ResultMode.Error;
Message = error.Message;
HttpResultStatus = HttpStatusCode.BadRequest;
Error = error;
}
///
/// Initializes a new instance of the class.
///
/// The data.
/// The result status.
/// The result code.
/// The message.
public Result(TData data, ResultMode resultStatus = ResultMode.Success, string resultCode = "", string message = "")
{
Data = data;
ResultStatus = resultStatus;
ResultCode = resultCode;
Message = message;
}
///
/// Initializes a new instance of the class.
///
/// The data.
/// The exception.
public Result(TData data, Exception error)
{
ResultCode = error.HResult.ToString();
ResultStatus = ResultMode.Error;
Message = error.Message;
HttpResultStatus = HttpStatusCode.BadRequest;
Error = error;
}
///
/// Maps the specified convert.
///
/// The type of the map data.
/// The convert.
///
public virtual Result Map(Func convert)
{
return new Result
{
Data = convert(Data!),
Message = Message,
ResultCode = ResultCode,
ResultStatus = ResultStatus,
Error = Error,
HttpResultStatus = HttpResultStatus
};
}
}
}