using System.Net;
using System.Runtime.Serialization;
using EasyDevCore.Common;
namespace EasyDevCore.Remote
{
///
/// Enum ResultMode
///
public enum ResultMode
{
///
/// The success
///
Success = 0,
///
/// The error
///
Error = 1,
///
/// The information
///
Info = 2,
///
/// The warning
///
Warning = 3,
///
/// The Confirmation
///
Confirmation = 4
}
///
///
///
/// The type of the data.
public interface IValueResult
{
///
/// Gets or sets the data.
///
///
/// The data.
///
TData Data { get; set; }
///
/// 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 abstract class ValueResultBase : IValueResult
{
///
/// Gets or sets the data.
///
///
/// The data.
///
public TData Data { get; set; }
///
/// 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 error.
///
///
/// true if this instance is error; otherwise, false.
///
public virtual bool IsError => ResultStatus == ResultMode.Error;
///
/// Gets a value indicating whether this instance is success.
///
///
/// true if this instance is success; otherwise, false.
///
public bool IsSuccess => !IsError;
///
/// Maps the specified convert.
///
/// The type of the map data.
/// The convert.
///
public abstract ValueResultBase Map(Func convert);
}
///
///
///
/// The type of the value.
public class Result : ValueResultBase
{
///
/// 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 override 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.
///
internal Result()
{
}
///
/// 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 override Result Map(Func convert)
{
return new Result
{
Data = convert(Data!),
Message = Message,
ResultCode = ResultCode,
ResultStatus = ResultStatus,
Error = Error,
HttpResultStatus = HttpResultStatus
};
}
}
///
///
///
/// The type of the value.
///
public class UpdateResult : Result
{
///
/// Gets or sets the updated date.
///
///
/// The updated date.
///
public object UpdatedDate { get; set; }
///
/// Gets or sets the updated by.
///
///
/// The updated by.
///
public object UpdatedBy { get; set; }
///
/// Initializes a new instance of the class.
///
internal UpdateResult()
{
}
///
/// Initializes a new instance of the class.
///
/// The data.
/// The updated date.
/// The updated by.
/// The result status.
/// The result code.
/// The message.
public UpdateResult(TData data, object updatedDate, object updatedBy, ResultMode resultStatus = ResultMode.Success, string resultCode = "", string message = "") : base(data, resultStatus, resultCode, message)
{
UpdatedDate = updatedDate;
UpdatedBy = updatedBy;
}
///
/// Initializes a new instance of the class.
///
/// The data.
/// The exception.
public UpdateResult(TData data, Exception error) : base(data, error)
{
}
///
/// Maps the specified convert.
///
/// The type of the map data.
/// The convert.
///
public override UpdateResult Map(Func convert)
{
return new UpdateResult
{
Data = convert(Data!),
Message = Message,
ResultCode = ResultCode,
ResultStatus = ResultStatus,
UpdatedBy = UpdatedBy,
UpdatedDate = UpdatedDate,
Error = Error,
HttpResultStatus = HttpResultStatus
};
}
}
}