Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
namespace ExternalLibrary
{
public class NetworkMessage
{
static readonly TimeSpan TimeToLive = TimeSpan.FromSeconds(10);
private readonly DateTime _creation;
private Status _status;
private readonly object _content;
public NetworkMessage(Status status, object content)
{
_creation = DateTime.UtcNow;
_status = status;
_content = content;
}
public bool IsSuccess()
{
if (HasTimedOut())
_status = Status.Failed;
return _status == Status.Succeeded;
}
private bool HasTimedOut()
{
return (_creation.Add(TimeToLive)) < DateTime.UtcNow;
}
public T GetContentAs<T>() where T : class
{
var castedContent = _content as T;
if (castedContent == null)
throw new InvalidCastException("contet couldn't be casted into " + typeof(T));
return _content as T;
}
}
public enum Status
{
Pending,
Succeeded,
Failed
}
public class NetworkService
{
public Tuple<int, object> SendMessage()
{
// for ske of simplicity, some static data...
return new Tuple<int, object>(1, new object());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment