Issa Saman
Issa Saman

Reputation: 110

dynamic MediatR request handler not found

I am trying to use a MediatR with dynamic Query for that i have to use "T" Generic Classe to not duplicate work

System.AggregateException: 'One or more errors occurred. (Handler was not found for request of type MediatR.IRequestHandler`

i face this error and i have no idea to solve this ,, any idea please

public class DynamicReportingQuery<T> : IRequest<T>
    {
        public class DynamicReportingQueryHandler : IRequestHandler<DynamicReportingQuery<T>, T>
        {
            protected IExitDbContext db;

            public DynamicReportingQueryHandler(IExitDbContext db)
            {
                this.db = db;
            }

            public Task<T> Handle(DynamicReportingQuery<T> request, CancellationToken cancellationToken)
            {
                T response;

                try
                {
                    var parameters = new object[] {
                        new { name = "QuestionnaireId",value=request.Filter.QuestionnaireId },
                        new { name = "SeparationType" ,value=request.Filter.SeparationType},
                        new { name = "FromDate",value=request.Filter.From },
                        new { name = "ToDate" ,value=request.Filter.GetToDate()},
                        new { name = "EntityId",value=request.Filter.EntityId },
                    };
                    response = db.SingleRowExecuteStoredProcedure<T>(request.ProcdureName, parameters);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return Task.FromResult(response);
            }
        }
        public ReportFilterationModel Filter { get; set; }
        public string ProcdureName { get; set; }

    }

Upvotes: 0

Views: 1453

Answers (1)

Issa Saman
Issa Saman

Reputation: 110

I have solved this by:

if you have to use T class you have to register each model for example

services.AddScoped<IRequestHandler<DynamicReportingQuery<EmployeeSeparationOverAllModel>, IEnumerable<EmployeeSeparationOverAllModel>>, DynamicReportingQueryHandler<EmployeeSeparationOverAllModel>>();

and you have to separate query it self

public class DynamicReportingQuery<T> : IRequest<T>
    {
     
        public ReportFilterationModel Filter { get; set; }
        public string ProcdureName { get; set; }

    }

and this section

   public class DynamicReportingQueryHandler<T> : IRequestHandler<DynamicReportingQuery<T>, T>
        {
            protected IExitDbContext db;

            public DynamicReportingQueryHandler(IExitDbContext db)
            {
                this.db = db;
            }

            public Task<T> Handle(DynamicReportingQuery<T> request, CancellationToken cancellationToken)
            {
                T response;

                try
                {
                    var parameters = new object[] {
                        new { name = "QuestionnaireId",value=request.Filter.QuestionnaireId },
                        new { name = "SeparationType" ,value=request.Filter.SeparationType},
                        new { name = "FromDate",value=request.Filter.From },
                        new { name = "ToDate" ,value=request.Filter.GetToDate()},
                        new { name = "EntityId",value=request.Filter.EntityId },
                    };
                    response = db.SingleRowExecuteStoredProcedure<T>(request.ProcdureName, parameters);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return Task.FromResult(response);
            }
        }

so, in this way you have to repeat code over and over again with a different model or replace T class with object type and keep the code above in question it self.

the different between 2 ways is:

the first one:

  • repeat the code with different models
  • support multi data-set
  • changes properties data types

the second one:

  • more simple and less code
  • not supporting multi data-set
  • cannot change properties data types (you have to convert all data from the database side)

Upvotes: 2

Related Questions