dimanche 27 mars 2022

How to apply Command Design pattern with Dependency Injection using Generic Class?

i want to apply command Design pattern with dependency Injection in the following situation: i have three types of reports in my system (SubscriptionReport, SalesReport, SpechialistReport) so i created one interface IReportService

public interface IReportService<T> where T: class
    {
        public Task<GenericResponse<List<T>>> GetReport(string searchKey, DateTime from, DateTime to);

    }

and to apply OCP i have implemented the GetReport function tree times for (SubscriptionReport, SalesReport, SpechialistReport)

public class SpechialistReportService : IReportService<SpechialistReportDTO>
    {
        public Task<GenericResponse<List<SpechialistReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
        {
            throw new NotImplementedException(); // to be implemented later
        }
    }

 public class SubscriptionReportService : IReportService<SubscriptionReportDTO>
    {
        public Task<GenericResponse<List<SubscriptionReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
        {
            throw new NotImplementedException(); // to be implemented later
        }
    }
 public class SalesReportService : IReportService<SalesReportDTO>
    {
        public Task<GenericResponse<List<SalesReportDTO>>> Report(string searchKey, DateTime from, DateTime to)
        {
            throw new NotImplementedException(); // to be implemented later
        }
    }

after that i have added the dependency

    services.AddScoped(typeof(IReportService<SpechialistReportDTO>), typeof(SpechialistReportService));
    services.AddScoped(typeof(IReportService<SubscriptionReportDTO>), typeof(SubscriptionReportService));
    services.AddScoped(typeof(IReportService<SalesReportDTO>), typeof(SalesReportService));

the problem is in calling the dependency in the controller constructor

 private readonly IEnumerable<IReportService> _reportService; // Should be IReportService<dont know what class should i specify here>

        public ReportController(IReportService<T> reportService)
        {
            this._reportService = reportService;
        }

Any help would be appreciated thanks in advance,

Aucun commentaire:

Enregistrer un commentaire