Message générique avec CallBack sur MVVM Light

Adepte de l'excellent MVVM Light de Laurent Bugnion (@lbugnion), et notamment du Messenger, il me fallait un type de message me permettant de passer un objet en paramètre, ainsi qu'une fonction de CallBack, besoin dont je n'ai pas trouvé trace de solution dans le framework (je m'excuse d'avance si ce besoin est déjà comblé par la bibliothèque).

J'ai donc développé 2 petites classes pour le Messenger que je partage avec vous.

La première permettant d'envoyer un message générique


using System;
using GalaSoft.MvvmLight.Messaging;

/// <summary>
/// Provides a message class with a built-in callback and generic value (Content) to a recipient. When the recipient
/// is done processing the message, it can execute the callback to
/// notify the sender that it is done. Use the <see cref="Execute" />
/// method to execute the callback. The callback method has one parameter.
/// <seealso cref="GenericMessageActionGeneric<T, TCallBack>"/>.
/// </summary>
/// <typeparam name="T">The type of the Content property.</typeparam>
/// <typeparam name="TCallBack">The type of the callback method's
/// only parameter.</typeparam>
public class GenericMessageActionGeneric<T, TCallBack> : MessageBase
{
    private readonly Delegate _callBack;

    /// <summary>
    /// Gets or sets the message's content.
    /// </summary>
    public T Content
    {
        get;
        protected set;
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="GenericMessageActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public GenericMessageActionGeneric(T content, Action<TCallBack> callBack)
    {
        Content = content;
        CheckCallback(callBack);
        _callBack = callBack;
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="GenericMessageActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="sender">The message's sender.</param>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public GenericMessageActionGeneric(object sender, T content, Action<TCallBack> callBack)
        :base(sender)
    {
        Content = content;
        CheckCallback(callBack);
        _callBack = callBack;
        
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="GenericMessageActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="sender">The message's sender.</param>
    /// <param name="target">The message's intended target. This parameter can be used
    /// to give an indication as to whom the message was intended for. Of course
    /// this is only an indication, amd may be null.</param>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public GenericMessageActionGeneric(object sender, object target, T content, Action<TCallBack> callBack)
        : base(sender, target)
    {
        Content = content;
        CheckCallback(callBack);
        _callBack = callBack;

    }

    /// <summary>
    /// Check if callBack parameter is not null
    /// </summary>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    private static void CheckCallback(Delegate callBack)
    {
        if (callBack == null)
        {
            throw new ArgumentNullException("callBack", "Callback may not be null");
        }
    }

    /// <summary>
    /// Executes the callback that was provided with the message with an
    /// arbitrary number of parameters.
    /// </summary>
    /// <param name="arguments">A  number of parameters that will
    /// be passed to the callback method.</param>
    /// <returns>The object returned by the callback method.</returns>
    public virtual object Execute(params object[] arguments)
    {
        return _callBack.DynamicInvoke(arguments);
    }
}

La seconde permet d'envoyer une notification


using System;

/// <summary>
/// Provides a message class with a built-in callback and generic value (Content) to a recipient. When the recipient
/// is done processing the message, it can execute the callback to
/// notify the sender that it is done. Use the <see cref="Execute" />
/// method to execute the callback. The callback method has one parameter.
/// <seealso cref="GenericMessageActionGeneric<T, TCallBack>"/>.
/// </summary>
/// <typeparam name="T">The type of the Content property.</typeparam>
/// <typeparam name="TCallBack">The type of the callback method's
/// only parameter.</typeparam>
public class NotificationMessageGenericActionGeneric<T, TCallBack> : GenericMessageActionGeneric<T, TCallBack>
{
    /// <summary>
    /// Gets a string containing any arbitrary message to be
    /// passed to recipient(s).
    /// </summary>
    public string Notification
    {
        get;
        private set;
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="NotificationMessageGenericActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="notification">An arbitrary string that will be
    /// carried by the message.</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public NotificationMessageGenericActionGeneric(T content, string notification, Action<TCallBack> callBack)
        :base(content, callBack)
    {
        Notification = notification;
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="NotificationMessageGenericActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="sender">The message's sender.</param>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="notification">An arbitrary string that will be
    /// carried by the message.</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public NotificationMessageGenericActionGeneric(object sender, T content, string notification, Action<TCallBack> callBack)
        : base(sender, content, callBack)
    {
        Notification = notification;
    }

    /// <summary>
    /// Initializes a new instance of the
    /// <see cref="NotificationMessageGenericActionGeneric<T, TCallBack>" /> class.
    /// </summary>
    /// <param name="sender">The message's sender.</param>
    /// <param name="target">The message's intended target. This parameter can be used
    /// to give an indication as to whom the message was intended for. Of course
    /// this is only an indication, amd may be null.</param>
    /// <param name="content">A value to be passed to recipient(s).</param>
    /// <param name="notification">An arbitrary string that will be
    /// carried by the message.</param>
    /// <param name="callBack">The callback method that can be executed
    /// by the recipient to notify the sender that the message has been
    /// processed.</param>
    public NotificationMessageGenericActionGeneric(object sender, object target, T content, string notification, Action<TCallBack> callBack)
        : base(sender, target, content, callBack)
    {
        Notification = notification;
    }

    /// <summary>
    /// Executes the callback that was provided with the message.
    /// </summary>
    /// <param name="parameter">A parameter requested by the message's
    /// sender and providing additional information on the recipient's
    /// state.</param>
    public void Execute(TCallBack parameter)
    {
        base.Execute(parameter);
    }
}

Les 2 classes sous forme de fichiers, en espérant que cela puisse vous servir.



// coding with fun

Écrire un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec une *

Quelle est la dernière lettre du mot gikx ? :