Handler discovery #991
Unanswered
sharpzilla
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi guys, is it possible to call handlers from specific assembly only if mediator.Send() called inside that assembly?
I Have simple synthetic solution:
API
^ Infrastructure
^ Application
^ Domain
API assembly is asp net 6 project. There are controllers with injected mediator. Which send commands to to handlers in application assembly .
Application assembly holds all app use cases.
Infrastructure has ef core db context and etc.
Domain assembly holds domain models, domain events etc.
In example I have domain model with method MyDomainModel.ChangeStatus(). Which change enum status of model and add domain event to collection of domain events inside domain model, which should be published to database outbox table in one transaction when DbContext.SaveChanges() called.
So I'm injecting mediator to DbContext and inside overrided method SaveChanges() getting entities where I has domain events and send or publish that domain events to handlers. Actually it is not a problem I just need register my two assemblies: Application and Infrastructure in mediator to find handlers.
How can I achieve that behavior? Handlers located inside the Infrastructure assembly must be discovered only when the mediator sends a command from the Infrastructure assembly. So, a call inside the API or Application assembly must be invalid and should throw an exception:
Otherwise, this code must be valid:
I tried marking the infrastructure handler class as internal, but the mediator can discover it in calls from other assemblies. Also, I thought I could map domain event DTO to infrastructure internal DTO and send it to the infrastructure internal handler. But maybe there is a simpler way?
Beta Was this translation helpful? Give feedback.
All reactions