jbogard / MediatR

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

Filtering Registration on Handlers #956

Closed Arcalise08 closed 10 months ago

Arcalise08 commented 10 months ago

I would like to know if there is anyway to be a little more explicit about what handlers are registered when using AddMediatr

Just to explain my use case here. I have a microservice api system which shares a business logic layer. I would like for each microservice to be able to selectively register the handlers which are relevant to each project instead of an all or nothing approach. I'm aware you can add them manually but I'd rather avoid that.

If its not possible would a PR be accepted to do such a thing? I would probably expect something like an attribute which could be used to detect them.

jbogard commented 10 months ago

In short, no, I don't really want to enable this behavior. I think you should re-examine your strategy for sharing business logic because it's really no different than any other class library.

Arcalise08 commented 10 months ago

My architecture follows the Clean Architecture pattern. As for now we have 7 api services which consume 7 individual UseCase projects. It could make sense to keep it that way in a simple system with no cross cutting concerns. But in a more robust system with a lot of bounded context. I think it makes a lot more sense to keep them in a single project scoped to a folder, Or even multiple business logic projects with a single bounded context project which references most of them. Either way that prevents a rather large amount of projects for the various different bounded context.

Not to get too off topic here but various other mediator libraries are implementing a similar feature for this, Although that wont be available for awhile. We also are long time users of this project and would love to see it implemented here as well.

I think there are other benefits to the added ability to filter handlers more explicitly but if you choose to not implement or accept such a PR i respect that as well.

Looking deeper into your code, it seems there is a TypeEvaluator already implemented. If that works as it sounds like it should, That would already allow us to filter by attributes with a little work.

jbogard commented 10 months ago

I don't really want to enable the funniness that happens in Clean architecture, I am...not a fan of that. It doesn't sound like you have microservices either, because you don't really share business logic like this amongst microservices. But if something's there that you can use, go for it.