CleanArchitecture icon indicating copy to clipboard operation
CleanArchitecture copied to clipboard

refactor: use generic Created/Deleted event handlers

Open bacongobbler opened this issue 2 years ago • 11 comments

While in this example there is only one Created/Deleted event and one Created/Deleted event handler, in a project with multiple entities emitting these events it makes sense to implement a more generic Created/Deleted event handler.

I also moved the event handlers to common as it can be re-used to log any created/deleted events.

Signed-off-by: Matthew Fisher [email protected]

bacongobbler avatar Apr 05 '22 15:04 bacongobbler

thank you! If we have multiple entities, how do you distinguish between them, use switch?

neozhu avatar Apr 06 '22 04:04 neozhu

I’m not quite sure I understand the question. Could you please elaborate more on that thought? What are you trying to do with those entities?

bacongobbler avatar Apr 06 '22 05:04 bacongobbler

If you’re referring to entity-specific event handlers, you could use the following function signature:

public class TodoItemCreatedEventHandler : INotificationHandler<DomainEventNotification<CreatedEvent<TodoItem>>>

You could then put that under the CleanArchitechture.Application.TodoItem.EventHandlers namespace as before and implement your business logic there.

Is that what you are referring to, or were you referring to something else?

bacongobbler avatar Apr 06 '22 05:04 bacongobbler

Nice

neozhu avatar Apr 06 '22 05:04 neozhu

I'd hold off on merging this one FYI... I just discovered an issue with this design pattern due to the way that aspnetcore resolves generic services. A fix is planned for .NET 7.

https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection/issues/118 https://github.com/dotnet/runtime/issues/65145

bacongobbler avatar Apr 06 '22 20:04 bacongobbler

I have a new problem NotificationHandler gets called twice #718

neozhu avatar Apr 23 '22 13:04 neozhu

image Createdeventhanler not working

neozhu avatar Apr 23 '22 13:04 neozhu

image this code will be invoke twice why

neozhu avatar Apr 23 '22 13:04 neozhu

there’s a bug in .NET causing this behaviour. Please go read through the issues in my last comment.

https://github.com/jasontaylordev/CleanArchitecture/pull/603#issuecomment-1090795153

bacongobbler avatar Apr 23 '22 14:04 bacongobbler

Thank you for your prompt reply, I hope it can be repaired soon

neozhu avatar Apr 23 '22 14:04 neozhu

Thanks @bacongobbler, this will be a great improvement once the bug is fixed.

jasontaylordev avatar May 23 '22 09:05 jasontaylordev

Hey @bacongobbler. Thanks for your contribution however I've decided not to merge this PR in the interest of keeping the solution as simple as possible.

jasontaylordev avatar Feb 23 '23 20:02 jasontaylordev