namespace Swan.Messaging
{
using System;
///
/// Base class for messages that provides weak reference storage of the sender.
///
public abstract class MessageHubMessageBase
: IMessageHubMessage
{
///
/// Store a WeakReference to the sender just in case anyone is daft enough to
/// keep the message around and prevent the sender from being collected.
///
private readonly WeakReference _sender;
///
/// Initializes a new instance of the class.
///
/// The sender.
/// sender.
protected MessageHubMessageBase(object sender)
{
if (sender == null)
throw new ArgumentNullException(nameof(sender));
_sender = new WeakReference(sender);
}
///
public object Sender => _sender.Target;
}
///
/// Generic message with user specified content.
///
/// Content type to store.
public class MessageHubGenericMessage
: MessageHubMessageBase
{
///
/// Initializes a new instance of the class.
///
/// The sender.
/// The content.
public MessageHubGenericMessage(object sender, TContent content)
: base(sender)
{
Content = content;
}
///
/// Contents of the message.
///
public TContent Content { get; protected set; }
}
}