简体   繁体   English

可以在一个控制器动作中进行多次数据库往返

[英]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 . 我正在编写一个Web应用程序,该应用程序具有管理仪表板,并且具有db中所有数据摘要

MSSql is being used here. 在这里使用MSSql。

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. 我返回一个IQueryable,因为它不可能有效地将所有数据作为IEnumerable获取到内存中,并且使用扩展方法在中间执行更多过滤/分页操作。

Is there a better way to make my repository? 有没有更好的方法来制作我的存储库?

Here is my ViewComponent action (which can be a controller action as well): 这是我的ViewComponent操作(也可以是控制器操作):

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: 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;
    }
}

You did not mention, which type of database you use with EF (SQLServer, MySql, Oracle, ...). 您没有提到与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/ 您可以定义视图并使用类似于此处的子选择: https : //www.essentialsql.com/get-ready-to-learn-sql-server-19-introduction-to-sub-queries/

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. 在有意义的地方进行优化和缓存。

Traditionally, if you want lots of data from one round trip, the answer for SQL Server is a stored procedure. 传统上,如果您希望一次往返传输大量数据,则SQL Server的答案是存储过程。 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 http://www.entityframeworktutorial.net/stored-procedure-in-entity-framework.aspx

How to call Stored Procedure in Entity Framework 6 (Code-First)? 如何在Entity Framework 6(代码优先)中调用存储过程?

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. Entity Framework确实不是执行此操作的最佳方法,真正的目的是在添加自动完成功能的同时,使执行菜鸟错误(例如不带WHERE子句的UPDATE和DELETE)更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM