using System;
namespace Swan.Messaging {
///
/// 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));
}
this._sender = new WeakReference(sender);
}
///
public Object Sender => this._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) => this.Content = content;
///
/// Contents of the message.
///
public TContent Content {
get; protected set;
}
}
}