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