using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer
{
/// 结果状态,Code 为零时表示正常,Code 0 与 NULL 相等。
[Serializable]
public class Result
{
private int _code;
private string _message;
private Exception _exception;
/// 代码。
public int Code { get { return _code; } }
/// 消息。
public string Message { get { return _message; } }
/// 异常。
public Exception Exception { get { return _exception; } }
/// 创建实例:Code = 0,Message = NULL,Exception = NULL。
public Result() { Construct(0, null, null); }
/// 创建实例:Message = NULL,Exception = NULL。
public Result(int code) { Construct(code, null, null); }
/// 创建实例:Exception = NULL。
public Result(int code, string message) { Construct(code, message, null); }
/// 创建实例:Code = -1,Exception = NULL。
public Result(string message) { Construct(-1, message, null); }
/// 创建实例:Code = -2。
public Result(Exception exception) { Construct(-2, null, exception); }
private void Construct(int code, string message, Exception exception)
{
_code = code;
if (exception == null)
{
_message = message;
}
else
{
_exception = exception;
try { _message = _exception.Message; } catch { }
}
}
/// 判断相等。
public override bool Equals(object obj)
{
return Equals(this, obj as Result);
}
/// 获取哈希码。
public override int GetHashCode()
{
return _code;
}
/// 返回格式化的字符串,格式为:Code 0 : Message Content
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("Code ");
sb.Append(_code.ToString());
sb.Append(" : ");
if (!string.IsNullOrEmpty(_message)) sb.Append(_message);
var text = sb.ToString();
return text;
}
/// 判断相等。
public static bool operator ==(Result left, Result right)
{
return Equals(left, right);
}
/// 判断不等。
public static bool operator !=(Result left, Result right)
{
return !Equals(left, right);
}
/// 与 System.Int32 转换 Code 属性。
public static implicit operator int(Result result)
{
return result == null ? 0 : result._code;
}
/// 与 System.Int32 转换 Code 属性。
public static implicit operator Result(int code)
{
return new Result(code);
}
private static bool Equals(Result left, Result right)
{
if (left as object == null)
{
if (right as object == null)
{
return true;
}
else
{
return right._code == 0;
}
}
else
{
if (right as object == null)
{
return left._code == 0;
}
else
{
return left._code == right._code;
}
}
}
/// Code = 0,Message = NULL,Exception = NULL。
public static Result Zero { get; } = new Result();
}
/// 装箱返回结果,T 不适用于 System.String。
[Serializable]
public class Result : IDisposable
{
/// 对象。
public T Entity { get; internal set; }
/// 异常。
public Exception Exception { get; internal set; }
/// 代码。
public int Code { get; internal set; }
/// 消息,此属性永不为 Null 值,当存在 Exception 时将被 Exception.Message 强制覆盖。
public string Message { get; internal set; }
/// 含有实体对象。
public bool HasEntity { get { return Entity != null; } }
/// 执行与释放或重置非托管资源关联的应用程序定义的任务。
public void Dispose()
{
var entity = Entity as IDisposable;
if (entity != null) entity.Dispose();
}
private void Construct(T entity, Exception exception, string message)
{
Entity = entity;
Exception = exception;
if (string.IsNullOrEmpty(message))
{
if (exception != null)
{
var error = Exception.Message;
if (string.IsNullOrEmpty(error)) error = "存在带有无效消息的异常。";
Message = error;
}
}
else Message = message;
}
/// 创建空结果,不含有实体、消息和异常。
public Result() { }
/// 创建带有异常的结果。
public Result(Exception exception) => Construct(default(T), exception, null);
/// 创建带有实体的结果。
public Result(T entity) => Construct(entity, null, null);
/// 创建带有实体和消息的结果。
public Result(T entity, params string[] message) => Construct(entity, null, TextGenerator.Merge(message));
/// 创建带有错误消息的结果。
public static Result Error(params string[] message)
{
var instance = new Result();
var merged = TextGenerator.Merge(message);
instance.Construct(default(T), null, merged.IsEmpty() ? "存在无效消息。" : merged);
return instance;
}
/// 创建带有异常的结果,Exception 会覆盖 Message。
public static Result Error(Exception exception)
{
var instance = new Result();
instance.Construct(default(T), exception, (exception == null ? "存在 NULL 异常。" : null));
return instance;
}
}
}