AspNetCoreHero-Boilerplate icon indicating copy to clipboard operation
AspNetCoreHero-Boilerplate copied to clipboard

Is it possible to use generic handler?

Open sgashua opened this issue 3 years ago • 0 comments

To prevent too much works to do from creating too many handlers classes, is it possible to use generic handler for all entities? Or not recommended to use generic handler?

public partial class CreateBrandCommand : IRequest<Result<int>>
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Tax { get; set; }
    }
public class CreateBrandCommandHandler : IRequestHandler<CreateBrandCommand, Result<int>>
    {
        private readonly IBrandRepository _brandRepository;
        private readonly IMapper _mapper;
...

Generic handler instead

public partial class CreateCommand<T> : T, IRequest<Result<int>> where T : class { } 
public class CreateCommandHandler : IRequestHandler<CreateCommand<T>, Result<int>>
{
      private readonly IBaseRepository<T> _repo;
...

But I cannot get generic handler working.

sgashua avatar Jun 21 '21 02:06 sgashua