Click or drag to resize
Mediator Class
Implements the Mediator pattern to be able to send and receive messages and data between objects which doesn't know each other.
Inheritance Hierarchy
SystemObject
  DW.SharpToolsMediator

Namespace: DW.SharpTools
Assembly: DW.SharpTools (in DW.SharpTools.dll) Version: 2.4.0.0 (2.4.0.0)
Syntax
C#
public static class Mediator

The Mediator type exposes the following members.

Methods
  NameDescription
Public methodStatic memberIgnore
Public methodStatic memberIgnoreForMessage
Unregisters a callback from the specific message.
Public methodStatic memberIgnoreForObjectT
Unregisters a callback from the specific object type.
Public methodStatic memberListenForMessage
Registers a callback listener to be invoked when the given message appears.
Public methodStatic memberListenForObjectT
Registers a callback listener to be invoked when the given object type has been sent.
Public methodStatic memberSendMessage
Sends the given message and its parameter to all listeners.
Public methodStatic memberSendObjectT
The object to send to all listeners.
Top
Examples
C#
private class DemoSender
{
    public void Notify(string filePath)
    {
        Mediator.SendMessage("FileCopied", filePath);
    }

    public void Notify(FileCopyData data)
    {
        Mediator.SendObject(data);
    }
}

private class DemoListener : IDisposable
{
    private ListenToken _objectToken;
    private ListenToken _messageToken;

    public DemoListener()
    {
        _messageToken = Mediator.ListenForMessage("FileCopied", HandleFileCopied);
        _objectToken = Mediator.ListenForObject<FileCopyData>(HandleFileCopied);
    }

    private void HandleFileCopied(object obj)
    {
        var copiedFile = obj.ToString();
    }

    private void HandleFileCopied(FileCopyData data)
    {
        var copiedFile = data.FilePath;
    }

    public void Dispose()
    {
        Mediator.Ignore(_messageToken);
        Mediator.Ignore(_objectToken);
    }
}

private class FileCopyData
{
    public string FilePath { get; set; }
}
See Also