dimanche 31 décembre 2017

Is it OK to do many database round trips in one controller action

I am writing a web app that has an admin dashboard and that has a summary of all the data in the db.

Is there a way to get all these data from different tables in one round call ?

I am also concerned about my repository design. I return an IQueryable since it's by no way gonna be efficient to get all the data as IEnumerable in the memory and to perform more filtering/pagin in the middle using extension methods.

Is there a better way to make my repository?

Here is my ViewComponent action (which can be a controller action as well):

public async Task<IViewComponentResult> InvokeAsync()
    {
        var agents = _repository.AgentData.GetAll();
        var applications = _repository.ApplicationData.GetAll();
        var paymentRequests = _repository.PaymentRequestData.GetAll();

        var universityCommissionCalcuator = new CommissionUniversityCalculator(0);
        var commissionCalcuator = new CommissionReferralCalculator(universityCommissionCalcuator);
        var commission = await _repository.AgentData.GetTotalCommissions(applications, commissionCalcuator);

        var result = AdminSummaryMapper.ToViewModel(agents, applications, paymentRequests, commission);

        return View(result);
    }

AdminSummaryMapper:

public static class AdminSummaryMapper
{
    public static AdminSummaryViewModel ToViewModel(IQueryable<Agent> agents, 
                                                    IQueryable<Application> applications, 
                                                    IQueryable<PaymentRequest> paymentRequests,
                                                    Commission commission)
    {
        var result = new AdminSummaryViewModel()
        {
            TotalStudents = applications.Count(),
            ConfirmedStudents = applications.Count(app => app.ApplicationStatus == ApplicationStatus.Confirmed),
            UnderEvaluationStudents = applications.Count(app => app.ApplicationStatus == ApplicationStatus.UnderEvaluation),
            TotalAgents = agents.Count(),
            PaymentRequests = paymentRequests.Count(),
            TotalCommission = commission.TotalComission,
            NetCommission = commission.NetComission,
        };

        return result;
    }
}

Aucun commentaire:

Enregistrer un commentaire