RaspberryIO/Unosquare.Swan/Components/MessageHubMessageBase.cs
2019-12-03 18:44:25 +01:00

55 lines
1.8 KiB
C#

using System;
namespace Unosquare.Swan.Components {
/// <summary>
/// Base class for messages that provides weak reference storage of the sender.
/// </summary>
public abstract class MessageHubMessageBase
: IMessageHubMessage {
/// <summary>
/// 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.
/// </summary>
private readonly WeakReference _sender;
/// <summary>
/// Initializes a new instance of the <see cref="MessageHubMessageBase"/> class.
/// </summary>
/// <param name="sender">The sender.</param>
/// <exception cref="System.ArgumentNullException">sender.</exception>
protected MessageHubMessageBase(Object sender) {
if(sender == null) {
throw new ArgumentNullException(nameof(sender));
}
this._sender = new WeakReference(sender);
}
/// <summary>
/// The sender of the message, or null if not supported by the message implementation.
/// </summary>
public Object Sender => this._sender?.Target;
}
/// <summary>
/// Generic message with user specified content.
/// </summary>
/// <typeparam name="TContent">Content type to store.</typeparam>
public class MessageHubGenericMessage<TContent>
: MessageHubMessageBase {
/// <summary>
/// Initializes a new instance of the <see cref="MessageHubGenericMessage{TContent}"/> class.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="content">The content.</param>
public MessageHubGenericMessage(Object sender, TContent content)
: base(sender) => this.Content = content;
/// <summary>
/// Contents of the message.
/// </summary>
public TContent Content {
get; protected set;
}
}
}