You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
namespace Api
...
var response = await mediatorInsideApiAssembly.Send(new InfrastructureDto());
// Throw an exception because the handler can't be discovered
Otherwise, this code must be valid:
namespace Infrastructure
public class MyDbContext : DbContext
{
private readonly IMediator mediatorInsideInfrastructureAssembly;
...
public override int SaveChanges()
{
var response = await mediatorInsideInfrastructureAssembly.Send(new InfrastructureDto());
// Goes to the handler
return base.SaveChanges();
}
}
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?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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