Coder Social home page Coder Social logo

rsa-dev / membroker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cjbhaines/membroker

0.0 1.0 0.0 712 KB

MemBroker is a lightweight in-memory message broker for .NET

Home Page: http://www.nuget.org/packages/MemBroker/

License: MIT License

C# 100.00%

membroker's Introduction

MemBroker

MemBroker is a lightweight in memory message broker for .NET. It can be used to reduce coupling and to communicate with different components in your system. Full credit must be given to the MVVM light team from whom I took the base concept of their Messenger class. MemBroker is a refactored, cleaner and extended version.

Build status

Features

  • Register for messages via base types, interfaces or concrete types
  • Register for messages with a filter func
  • Deliver sub types to base type registrations
  • Unregister entire receivers or specific registrations
  • WeakReferences are used to allow recipients to be garbage collected

Example

var messageBroker = new MessageBroker();

// Subscribe to messages
messageBroker.Register<TestMessage>(this, message =>
{
// Handle message
});

// Send message
messageBroker.Send(new TestMessage());

Interface

/// <summary>
/// Register to a given message type
/// </summary>
/// <typeparam name="TMessage">The type of the message to register to</typeparam>
/// <param name="recipient">The object to which the message will be delivered. This reference keeps the registration alive</param>
/// <param name="action">The callback to invoke upon receiving a message</param>
void Register<TMessage>(object recipient, Action<TMessage> action);

/// <summary>
/// Register to a given message type with an additional filter
/// </summary>
/// <typeparam name="TMessage">The type of the message to register to</typeparam>
/// <param name="recipient">The object to which the message will be delivered. This reference keeps the registration alive</param>
/// <param name="action">The callback to invoke upon receiving a message</param>
/// <param name="filter">A Func to filter messages</param>
void Register<TMessage>(object recipient, Action<TMessage> action, Func<TMessage, bool> filter);

/// <summary>
/// Send a message to all subscribers
/// </summary>
/// <typeparam name="TMessage">The type of the message to send</typeparam>
/// <param name="message">The message</param>
void Send<TMessage>(TMessage message);

/// <summary>
/// Send a collection of messages to all subscribers
/// </summary>
/// <typeparam name="TMessage">The type of the message to send</typeparam>
/// <param name="messages">The collection of messages</param>
void Send<TMessage>(IEnumerable<TMessage> messages);

/// <summary>
/// Unregister an entire recipient instance
/// </summary>
/// <param name="recipient">The recipient instance to unregister</param>
void Unregister(object recipient);

/// <summary>
/// Unregister a specific registration
/// </summary>
/// <typeparam name="TMessage">The message type to uregister</typeparam>
/// <param name="recipient">The recipient instance to unregister</param>
/// <param name="action">The callback reference</param>
void Unregister<TMessage>(object recipient, Action<TMessage> action);

/// <summary>
/// Remove any registrations for recipients that have been garbage collected
/// </summary>
void Cleanup();

/// <summary>
/// Remove all registrations
/// </summary>
void Clear();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.