Skip to content

CShark-Hub/Mako-IoT.Device.Services.Mediator

Repository files navigation

Mako-IoT.Device.Services.Mediator

Mediator pattern implementation. Provides in-process publisher-subscriber communication while keeping all parties decoupled.

Usage

See Mediator sample

Create classes for your events

public class Event1 : IEvent
{
    public string Data { get; set; }
}

public class Event2 : IEvent
{
    public string Text { get; set; }
}

Your event subscriber must implement IEventHandler interface

public class Service2 : IEventHandler
{
    public void Handle(IEvent @event)
    {
        switch (@event)
        {
            case Event1 event1:
                Debug.WriteLine($"[{nameof(Service2)}] Event1 received. The data is: {event1.Data}");
                break;
            case Event2 event2:
                Debug.WriteLine($"[{nameof(Service2)}] Event2 received The text is: {event2.Text}");
                break;
        }
    }
}

Use IMediator to publish events

public class Service1 : IService1
{
    private readonly IMediator _mediator;

    public Service1(IMediator mediator)
    {
        _mediator = mediator;
    }

    public void DoSomething()
    {
        _mediator.Publish(new Event2 { Text = "Hello from Service1 !" });
    }
}

Register Mediator and singleton subscribers in DeviceBuilder

DeviceBuilder.Create()
  .AddMediator(options =>
  {
      options.AddSubscriber(typeof(Event1), typeof(Service2));
      options.AddSubscriber(typeof(Event2), typeof(Service2));
  })
  .Build()
  .Start()

For transient and scoped services you can use the Subscribe and Unsubscribe overloads that take a specific instance.

public class TransientService : IDisposable
{
    private readonly IMediator _mediator;

    public TransientService(IMediator mediator)
    {
        _mediator = mediator;
        _mediator.Subscribe(typeof(Event1), this);
        _mediator.Subscribe(typeof(Event2), this);
    }

    public void Dispose()
    {
        _mediator.Unsubscribe(typeof(Event1), this);
        _mediator.Unsubscribe(typeof(Event2), this);
    }
}

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages