Reputation: 447
I am writing a web app that has an admin dashboard and that has a summary of all the data in the db.
MSSql is being used here.
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;
}
}
Upvotes: 0
Views: 968
Reputation: 342
You did not mention, which type of database you use with EF (SQLServer, MySql, Oracle, ...).
Retrieving aggregated data from a relational database is quite an easy job with SQL. You could define a view and use subselects like those here: https://www.essentialsql.com/get-ready-to-learn-sql-server-19-introduction-to-sub-queries/
Upvotes: 2
Reputation: 169
Traditionally, if you want lots of data from one round trip, the answer for SQL Server is a stored procedure. Entity framework probably has a way of directly accessing stored procedures, but keeping to your format, a view could be built on the database from such a stored procedure that pulls data from multiple queries, so each query gets executed server-side, then the results are pulled back in one go.
Directly accessing a stored procedure:
http://www.entityframeworktutorial.net/stored-procedure-in-entity-framework.aspx
How to call Stored Procedure in Entity Framework 6 (Code-First)?
This requires that the stored procedure manipulate the data so it comes back in one table. Entity Framework really isn't the best way to do this, the real purpose is to make it harder to perform rookie mistakes such as UPDATEs and DELETEs without WHERE clauses while adding auto-complete.
Upvotes: 1
Reputation: 24535
If you need to make multiple calls as data is coming from different places and cannot be requested in a single database query, then this is not a problem. Optimise and cache where it makes sense.
Upvotes: 1