-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Example of generic IRequestHandler #521
Comments
Is this a reasonable way to use Mediatr without any foreseeable issues? App DI:
GenericRequest and GenericResponse:
Generic Handler attached to MainViewModel:
Example request:
|
I would say, making it more generic isn't an ideal use case. The idea is that you have 1 request that maps to 1 handler because this follows SRP. It becomes easier to understand what the code is trying to achieve. I would say that if you just want There maybe a design issue with what you're doing which has made this pattern emerge. The pipeline handlers show how to "pass through" these requests and sort of do something with them. |
@cirrusone do you have this figured out. I am trying to do the same thing with asp.net core DI container, have not find the solution yet. Thanks |
I actually changed to CommunityToolkit/WindowsCommunityToolkit#3428 CommunityToolkit/WindowsCommunityToolkit#3230 https://github.com/windows-toolkit/MVVM-Samples/blob/master/docs/mvvm/Messenger.md |
@cirrusone Thanks a lot, I will check it out also. |
I have a similar issue.. I was able to resolve it by using AutoFac container. But wished I could have solved it using the default container. Here is what I did... Request... public class AddNoteCommand<TEntity> : IRequest
where TEntity : class, INoteEntity
{
public int ParentId { get; set; }
public int CurrentUserProfileId { get; set; }
public string NoteContent { get; set; }
} Handler public class AddNoteCommandHandler<TEntity> : IRequestHandler<AddNoteCommand<TEntity>>
where TEntity : class, INoteEntity, new()
{
private readonly INoteRepository<TEntity> _repo;
private readonly ITime _time;
public AddNoteCommandHandler(INoteRepository<TEntity> repo, ITime time)
{
_repo = repo;
_time = time;
}
public async Task<Unit> Handle(AddNoteCommand<TEntity> request, CancellationToken cancellationToken)
{
await _repo.Insert(new TEntity
{
CreatedByUserId = request.CurrentUserProfileId,
CreatedDate = _time.Current,
NoteContent = request.NoteContent,
ParentId = request.ParentId
});
await _repo.SaveChanges();
return Unit.Value;
}
} Registration with AutoFac public static ContainerBuilder AddGenericHandlers(this ContainerBuilder builder)
{
builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterGeneric(typeof(AddNoteCommandHandler<>)).AsImplementedInterfaces();
return builder;
} If anybody knows of a way to do this in the default asp.net core DI container then please show me! |
Are there any examples to show how to make a generic IRequestHandler?
I can obviously add lots of interfaces such as
but cannot find any examples of a generic handler to handle different requests and responses in the same handler. Is something like the following possible?
which would handle something like the following
The text was updated successfully, but these errors were encountered: