Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tratamento para eventos (Apenas para rastreamento) #48

Closed
alesimoes opened this issue Feb 3, 2020 · 7 comments
Closed

Tratamento para eventos (Apenas para rastreamento) #48

alesimoes opened this issue Feb 3, 2020 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@alesimoes
Copy link

Ivan, blz?

Conforme conversamos.
Estou usando o FluentMediator para publicar os eventos de domínio.
No meu BUS eu uso o fluentMediator e ele publica o evento no mediator para o mediator executar a function associada.
Até ai ok.
Porém se o tratamento do evento não for registrado no FluentMediator ele levanta null Exception.

@ivanpaulovich ivanpaulovich self-assigned this Feb 3, 2020
@ivanpaulovich ivanpaulovich added the bug Something isn't working label Feb 3, 2020
@ivanpaulovich
Copy link
Owner

Olá @alesimoes,

Gostaria de confirmar o bug e a sugestão sugerida:

Eu entendi que ao não configurar um Handler para um evento o mediator lança uma exception. Essa exception seria NullReferenceException ou PipelineNotFoundException?

A sugestão seria:

  1. Silenciar a exceção.
  2. Criar um handler para OnException no mediator e chamar esse handler para essas mensagens mal configuradas.

Seria isso?

@alesimoes
Copy link
Author

Olá @ivanpaulovich ,
Desculpe a exception correta é PipelineNotFoundException;

Acredito que as duas formas que você apresentou são válidas.
Ou talvez uma forma de validar no mediator se o pipeline está configurado. Sendo assim o Bus (que no caso estou usando como infraestrutura) ele poderia validar se tem alguém que quer receber o evento antes de publicar. (Apenas uma outra ideia)

ivanpaulovich added a commit that referenced this issue Feb 4, 2020
@ivanpaulovich
Copy link
Owner

Hey @alesimoes,

O que voce acha dessa implementação?

c3c58aa#diff-14cb423500372d924cabf8d8f8cc87abR159-R182

mediator.Publish(ping); não ira mais lançar PipelineNotFoundException e para o desenvolvedor não perder essa mensagem, ele precisa adicionar o seu delegate no no mediator.PipelineNotFound.

Essa abordagem resolveria a sua implementação?

@alesimoes
Copy link
Author

@ivanpaulovich , Excelente solução Ivan.
Resolveria sim.

ivanpaulovich added a commit that referenced this issue Feb 4, 2020
@ivanpaulovich
Copy link
Owner

Olá @alesimoes,

Estou chegando num design final, implementei duas formas de identificar que uma mensagem não chegou em uma pipeline:

Opção 01:

[Fact]
public void Publish_CallsPipelineNotFound_WhenHandlerIsNotSetup()
{
    var services = new ServiceCollection();
    // Mediator Without the Handler for PingRequest.
    services.AddFluentMediator(builder => { });

    var provider = services.BuildServiceProvider();
    var mediator = provider.GetRequiredService<IMediator>();

    bool myCustomPipelineNotFoundHandlerWasCalled = false;

    // This is handler is called for every message without a destination pipeline.
    mediator.PipelineNotFound += (object sender, PipelineNotFoundEventArgs e) => {
        myCustomPipelineNotFoundHandlerWasCalled = true;
    };

    var cts = new CancellationTokenSource();
    var ping = new PingRequest("Cancellable Async Ping");

    // Should run without throwing exceptions
    mediator.Publish(ping);

    // The method was called :)
    Assert.True(myCustomPipelineNotFoundHandlerWasCalled);
}

Opção 02:

// Add a Mediator derivative class

public class MyCustomMediator : Mediator
{
    public bool MyCustomPipelineNotFoundHandlerWasCalled = false;
    
    public MyCustomMediator(GetService getService, IPipelineProvider pipelines) : base(getService, pipelines)
    {
    }

    protected override void OnPipelineNotFound(PipelineNotFoundEventArgs e)
    {
        MyCustomPipelineNotFoundHandlerWasCalled = true;

        //Do something before raising the event
        base.OnPipelineNotFound(e);
        //Do something after raising the event 
    }
}

// Then you can run this:

[Fact]
public void Publish_CallsCustomPipelineNotFound_WhenHandlerIsNotSetup()
{
    var services = new ServiceCollection();
    
    // Mediator Without the Handler for PingRequest.
    services.AddFluentMediator<MyCustomMediator>(builder => { });

    var provider = services.BuildServiceProvider();
    var mediator = (MyCustomMediator)provider.GetRequiredService<IMediator>();

    var cts = new CancellationTokenSource();
    var ping = new PingRequest("Cancellable Async Ping");

    // Should run without throwing exceptions
    mediator.Publish(ping);

    // The method was called :)
    Assert.True(mediator.MyCustomPipelineNotFoundHandlerWasCalled);
}

Essas mudanças vão para a versão 0.4.7.

ivanpaulovich added a commit that referenced this issue Feb 4, 2020
- #48 Changed `PipelineNotFoundException` to internal.
- #48 Removed sealed modified from Mediator to allow derived implementations.
- #48 Added `OnPipelineNotFound` event to `IMediator` interface.
- #48 Added `void OnPipelineNotFound(PipelineNotFoundEventArgs e)` handler to `Mediator` class.
@ivanpaulovich
Copy link
Owner

Em primeira mão, publicado aqui:

https://www.nuget.org/packages/FluentMediator/0.4.7

@alesimoes
Copy link
Author

Opa, valeu @ivanpaulovich !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants