aspnetcorehero / Boilerplate

Clean Architecture Solution Template for ASP.NET Core 5.0. Built with Onion/Hexagonal Architecture and incorporates the most essential Packages your projects will ever need. Includes both WebApi and Web(MVC) Projects.
https://codewithmukesh.com/project/aspnet-core-hero-boilerplate/
MIT License
582 stars 148 forks source link

Possible to use generic handler? #29

Open sgashua opened 3 years ago

sgashua commented 3 years ago

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?

From

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;
...

To this generic handler

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. How to get it working?