You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Models/InvokeResult.cs

133 lines
3.4 KiB

6 years ago
using System;
using System.Runtime.Serialization;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Models
{
/// <summary>
/// Action result
6 years ago
/// </summary>
[DataContract]
public class InvokeResult :
IBinarySerializable
{
#region Static
6 years ago
private static readonly InvokeResult _successResultWitoutComment = new InvokeResult(true, String.Empty);
5 years ago
private static readonly InvokeResult _faultResultWitoutComment = new InvokeResult(false, String.Empty);
#endregion Static
6 years ago
#region Ctor
6 years ago
public InvokeResult()
{
}
public InvokeResult(bool success, string comment)
{
Success = success;
Comment = comment;
}
#endregion Ctor
6 years ago
#region Properties
6 years ago
/// <summary>
/// true when action successfully invoked
6 years ago
/// </summary>
[DataMember]
public bool Success;
6 years ago
/// <summary>
/// Comment
6 years ago
/// </summary>
[DataMember]
public string Comment;
#endregion Properties
6 years ago
#region Fabric methods
6 years ago
/// <summary>
/// Error when action invoking
6 years ago
/// </summary>
public static InvokeResult Fault(string comment) { return new InvokeResult(false, comment); }
5 years ago
public static InvokeResult Fault() { return _faultResultWitoutComment; }
6 years ago
/// <summary>
/// Successfully
/// </summary>
6 years ago
public static InvokeResult Succeeding(string comment = "") { return new InvokeResult(true, comment); }
6 years ago
/// <summary>
/// Successfully
6 years ago
/// </summary>
public static InvokeResult Succeeding() { return _successResultWitoutComment; }
public static InvokeResult<T> Succeeding<T>(T value, string comment = "")
{
return new InvokeResult<T>(value, true, comment);
}
public static InvokeResult<T> Fault<T>(string comment)
{
return new InvokeResult<T>(false, comment);
}
#endregion Fabric methods
6 years ago
public virtual void Serialize(IBinaryWriter writer)
{
writer.WriteBoolean(this.Success);
writer.WriteString(this.Comment);
}
public virtual void Deserialize(IBinaryReader reader)
{
this.Success = reader.ReadBoolean();
this.Comment = reader.ReadString();
}
}
public sealed class InvokeResult<T> :
InvokeResult
{
private T _value;
public T Value { get { return _value; } }
#region Ctor
5 years ago
public InvokeResult() { }
6 years ago
public InvokeResult(bool success, string comment)
{
Success = success;
Comment = comment;
}
public InvokeResult(T value, bool success, string comment)
{
_value = value;
Success = success;
Comment = comment;
}
#endregion Ctor
6 years ago
public override void Serialize(IBinaryWriter writer)
{
writer.WriteBoolean(this.Success);
writer.WriteString(this.Comment);
writer.WriteCompatible(this.Value);
}
public override void Deserialize(IBinaryReader reader)
{
this.Success = reader.ReadBoolean();
this.Comment = reader.ReadString();
this._value = reader.ReadCompatible<T>();
}
}
}

Powered by TurnKey Linux.