https://github.com/alexander-scott/mediatorsharp
A basic implementation of the Mediator design pattern that is used to promote decoupling between different systems
https://github.com/alexander-scott/mediatorsharp
csharp mediator mediator-pattern net
Last synced: about 1 month ago
JSON representation
A basic implementation of the Mediator design pattern that is used to promote decoupling between different systems
- Host: GitHub
- URL: https://github.com/alexander-scott/mediatorsharp
- Owner: alexander-scott
- License: mit
- Created: 2018-12-20T17:46:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-11T18:22:40.000Z (about 7 years ago)
- Last Synced: 2025-03-06T13:30:55.491Z (over 1 year ago)
- Topics: csharp, mediator, mediator-pattern, net
- Language: C#
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MediatorSharp
A basic implementation of the Mediator design pattern that is used to promote decoupling between different systems, as described [here](https://sourcemaking.com/design_patterns/mediator).
## Usage
Create an instance of the MessageService class when your application first starts. Then, when creating your systems, inject the MessageService instance as a dependency. Systems can then subscribe to a specific message type by providing a function callback and the message type:
```csharp
_messageService.Subscribe(ApplicationStarted, MessageType.ApplicationStarted);
```
To send messages between systems you simply need to send an IMessage to the MessageService:
```csharp
_messageService.SendMessage(new EmptyMessage(MessageType.ApplicationStarted));
```
In the callback function of a subscribed system, the IMessage can be cast into whatever data type your system requires:
```csharp
private void ReceiveStringMessage(IMessage obj)
{
StringMessage msg = (StringMessage) obj;
Debug.Log("Receive message: " + msg.String)
}
```
Additional message types can be constructing by defining more values in the MessageType enum and by also creating classes that inherit from IMessage.