Skip to content

Add #45: Parameterize sender type in ITinyMessage and descendant classes #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 70 additions & 8 deletions src/TinyIoC/TinyMessenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,63 @@ public interface ITinyMessage
object Sender { get; }
}

/// <summary>
/// A TinyMessage to be published/delivered by TinyMessenger
/// </summary>
/// <typeparam name="TSender">Type of the messenge sender</typeparam>
public interface ITinyMessage<TSender> : ITinyMessage
where TSender: class
{
/// <summary>
/// The sender of the message, or null if not supported by the message implementation.
/// </summary>
new TSender Sender { get; }
}

/// <summary>
/// Base class for messages that provides weak refrence storage of the sender
/// </summary>
public abstract class TinyMessageBase: TinyMessageBase<object>
{
public TinyMessageBase(object sender)
: base(sender)
{
}
}

/// <summary>
/// Base class for messages that provides weak refrence storage of the sender
/// </summary>
public abstract class TinyMessageBase : ITinyMessage
/// <typeparam name="TSender">Type of the messenge sender</typeparam>
public abstract class TinyMessageBase<TSender> : ITinyMessage<TSender>
where TSender: class
{
/// <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 WeakReference _Sender;
public object Sender
public TSender Sender
{
get
{
return (_Sender == null) ? null : _Sender.Target;
return (_Sender == null) ? null : (TSender)_Sender.Target;
}
}

object ITinyMessage.Sender
{
get
{
return Sender;
}
}

/// <summary>
/// Initializes a new instance of the MessageBase class.
/// </summary>
/// <param name="sender">Message sender (usually "this")</param>
public TinyMessageBase(object sender)
public TinyMessageBase(TSender sender)
{
if (sender == null)
throw new ArgumentNullException("sender");
Expand All @@ -63,11 +97,25 @@ public TinyMessageBase(object sender)
}
}

/// <summary>
/// Generic message with user specified content
/// </summary>
/// <typeparam name="TContent">Content type to store</typeparam>
public class GenericTinyMessage<TContent> : GenericTinyMessage<object, TContent>
{
public GenericTinyMessage(object sender, TContent content)
: base(sender, content)
{
}
}

/// <summary>
/// Generic message with user specified content
/// </summary>
/// <typeparam name="TSender">Type of the messenge sender</typeparam>
/// <typeparam name="TContent">Content type to store</typeparam>
public class GenericTinyMessage<TContent> : TinyMessageBase
public class GenericTinyMessage<TSender, TContent> : TinyMessageBase<TSender>
where TSender: class
{
/// <summary>
/// Contents of the message
Expand All @@ -79,7 +127,7 @@ public class GenericTinyMessage<TContent> : TinyMessageBase
/// </summary>
/// <param name="sender">Message sender (usually "this")</param>
/// <param name="content">Contents of the message</param>
public GenericTinyMessage(object sender, TContent content)
public GenericTinyMessage(TSender sender, TContent content)
: base(sender)
{
Content = content;
Expand All @@ -90,7 +138,21 @@ public GenericTinyMessage(object sender, TContent content)
/// Basic "cancellable" generic message
/// </summary>
/// <typeparam name="TContent">Content type to store</typeparam>
public class CancellableGenericTinyMessage<TContent> : TinyMessageBase
public class CancellableGenericTinyMessage<TContent> : CancellableGenericTinyMessage<object, TContent>
{
public CancellableGenericTinyMessage(object sender, TContent content, Action cancelAction)
: base(sender, content, cancelAction)
{
}
}

/// <summary>
/// Basic "cancellable" generic message
/// </summary>
/// <typeparam name="TSender">Type of the messenge sender</typeparam>
/// <typeparam name="TContent">Content type to store</typeparam>
public class CancellableGenericTinyMessage<TSender, TContent> : TinyMessageBase<TSender>
where TSender: class
{
/// <summary>
/// Cancel action
Expand All @@ -108,7 +170,7 @@ public class CancellableGenericTinyMessage<TContent> : TinyMessageBase
/// <param name="sender">Message sender (usually "this")</param>
/// <param name="content">Contents of the message</param>
/// <param name="cancelAction">Action to call for cancellation</param>
public CancellableGenericTinyMessage(object sender, TContent content, Action cancelAction)
public CancellableGenericTinyMessage(TSender sender, TContent content, Action cancelAction)
: base(sender)
{
if (cancelAction == null)
Expand Down