jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.82k stars 1.16k forks source link

RabbitMQ and MediatR #446

Closed sarthak-k closed 4 years ago

sarthak-k commented 4 years ago

I was wondering if it's possible to hook up RabbitMQ (or any other service bus) with MediatR to make it more usable across cross functional micro-services scenarios?

lilasquared commented 4 years ago

I don't see why it wouldn't be. I do this today by serializing the IRequest and writing to MSMQ.

sarthak-k commented 4 years ago

Did you implement that by implementing the IMediator interface?

jbogard commented 4 years ago

I would use something more suited for that purpose, like Rebus, EasyNetQ, or NServiceBus.

On Sun, Oct 6, 2019 at 3:25 PM Sarthak Killedar notifications@github.com wrote:

Did you implement that by implementing the IMediator interface?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jbogard/MediatR/issues/446?email_source=notifications&email_token=AAAZQMXBIEP2R4BVTQ22HXDQNJCU3A5CNFSM4I55RJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOTHXI#issuecomment-538784733, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZQMTANLKHDTRAP2IAAM3QNJCU3ANCNFSM4I55RJNA .

lilasquared commented 4 years ago

Nope. I have an extension method for IMediator called .SendToQueue() which wraps the request in a QueueRequest. Then a QueueRequestHandler handles that message by sending it to the queue.

// Extension Method
public static void SendToQueue(this IMediator mediator, IRequest request)
{
    mediator.Send(new QueueRequest(request));
}

// QueueRequestHandler
public Task Handle(QueueRequest request, CancellationToken cancellationToken)
{
    _queue.Enqueue(request);
}

// QueueRequest
public class QueueRequest
{
    public IRequest Request { get; set; }

    public QueueRequest(IRequest request)
    {
        Request = request;
    }
}

Then when reading the message from the queue it deserializes it and sends the wrapped IRequest to the mediator pipeline.

sarthak-k commented 4 years ago

I would use something more suited for that purpose, like Rebus, EasyNetQ, or NServiceBus. On Sun, Oct 6, 2019 at 3:25 PM Sarthak Killedar @.***> wrote: Did you implement that by implementing the IMediator interface? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#446?email_source=notifications&email_token=AAAZQMXBIEP2R4BVTQ22HXDQNJCU3A5CNFSM4I55RJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOTHXI#issuecomment-538784733>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZQMTANLKHDTRAP2IAAM3QNJCU3ANCNFSM4I55RJNA .

Yup, makes sense to go for already implemented bus. I did custom implementation for Azure Service Bus, so thought of extending the Mediator using RabbitMQ. NServiceBus is great but the licenses complexity kills the motivation to use in any project.

sarthak-k commented 4 years ago

Nope. I have an extension method for IMediator called .SendToQueue() which wraps the request in a QueueRequest. Then a QueueRequestHandler handles that message by sending it to the queue.

// Extension Method
public static void SendToQueue(this IMediator mediator, IRequest request)
{
    mediator.Send(new QueueRequest(request));
}

// QueueRequestHandler
public Task Handle(QueueRequest request, CancellationToken cancellationToken)
{
    _queue.Enqueue(request);
}

// QueueRequest
public class QueueRequest
{
    public IRequest Request { get; set; }

    public QueueRequest(IRequest request)
    {
        Request = request;
    }
}

Then when reading the message from the queue it deserializes it and sends the wrapped IRequest to the mediator pipeline.

Thanks for the reply. I think I got the answers for my questions.

jbogard commented 4 years ago

Ha well I don’t get paid to write infrastructure code....but MediatR is NOT a replacement for an actual message client or endpoint, and never will be. Just make sure you’ve taken a look at the Enterprise Integration Patterns book for all the things those projects do on client and server that MediatR will never do.

On Sun, Oct 6, 2019 at 4:06 PM Sarthak Killedar notifications@github.com wrote:

I would use something more suited for that purpose, like Rebus, EasyNetQ, or NServiceBus. … <#m-8679126490559795290> On Sun, Oct 6, 2019 at 3:25 PM Sarthak Killedar @.***> wrote: Did you implement that by implementing the IMediator interface? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#446 https://github.com/jbogard/MediatR/issues/446?email_source=notifications&email_token=AAAZQMXBIEP2R4BVTQ22HXDQNJCU3A5CNFSM4I55RJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOTHXI#issuecomment-538784733>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZQMTANLKHDTRAP2IAAM3QNJCU3ANCNFSM4I55RJNA .

Yup, makes sense to go for already implemented bus. I did custom implementation for Azure Service Bus, so thought of extending the Mediator using RabbitMQ. NServiceBus is great but the licenses complexity kills the motivation to use in any project.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/jbogard/MediatR/issues/446?email_source=notifications&email_token=AAAZQMSMAXJAKVA757LD3S3QNJHOTA5CNFSM4I55RJNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOUCJA#issuecomment-538788132, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZQMQBZSR7SQB7PAZ26LLQNJHOTANCNFSM4I55RJNA .

AliBayatGH commented 4 years ago

This NuGet package adds an extension method to IMediator called. Enqueue() Through it you can queue commands.

https://www.nuget.org/packages/CommandScheduler/