低效的MVC ViewModel向数据库进行多次调用?[英] Inefficient MVC ViewModel Making Multiple Calls to the Database?

本文是小编为大家收集整理的关于低效的MVC ViewModel向数据库进行多次调用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我显然不知道自己在做什么.这个 MVC 东西真的让我在试图保持这种模式时大吃一惊.我一直在关注 MVC 教程和大型谷歌搜索,这是我自己画的一个角落.

我有多个相似的数据,我正试图获取一个视图.我能够让我的代码正常工作,但对我来说,由于对数据库的多次调用,我们开始从数据库中提取大型记录集,因此它看起来效率非常低.所以,我有一个 OrderSummary 类,类里面是这样的:

public IEnumerable<Order> GetOrders()
{
   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s);

   return orders.ToList();
}

然后这个:

public decimal GetGrossProfitTotal()
{
   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s);
   decimal? grossprofittotal = orders.Sum(s => s.Profit);

   return grossprofittotal ?? decimal.Zero;
}

所以,如果我们将最后一段代码复制到总佣金和净利润总额中,这基本上就是我的布局.我猜想对 db 进行四次调用?

然后在控制器中:

        var ordersummary = new OrdersSummary();
        var viewModel = new OrderSummary
        {
            Orders = ordersummary.GetOrders(),
            GrossProfitTotal = ordersummary.GetGrossProfitTotal(),
            CommissionTotal = ordersummary.GetCommissionTotal(),
            NetProfitTotal = ordersummary.GetNetProfitTotal(),
        };
        return View(viewModel);

这会在视图中获取我需要的所有数据,以便我可以使用它.对我来说,这似乎是不必要的多余,我猜效率低下?如果你说我也在做排序和搜索参数,那也是很多重复的 linq 代码.看来我应该能够像这样整合数据:

   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s).ToList();

   decimal grossprofittotal = orders.Sum(s => s.Profit);
   decimal commissiontotal = orders.Sum(s => s.Commission);
   decimal netprofittotal = orders.Sum(s => s.Profit + s.Commission);

然后将这四个数据(订单列表和三个十进制值)很好地包装在一个数组(或其他)中,并将它们发送到控制器/视图.在视图中,我需要能够遍历订单列表.我离这儿很远吗?或者,MVC 的标准程序是什么?谢谢.

推荐答案

是的,四次获取相同的数据确实效率低下,完全没有必要.您可以很好地只获取一次,然后对您拥有的数据执行其他操作.

如果您愿意,您可以保留 GetOrders 方法,但这就是您需要获取的所有数据.如果您在控制器或模型构造函数中获取数据,则主要是个人喜好问题.就我个人而言,我倾向于在模型中放入比控制器更多的逻辑.

只要您使用 ToList 确保您实际获取数据(或将结果实现为集合的任何其他方法),您就可以根据内存中的内容计算总和.(没有它,您仍然需要对数据库进行四次查询.)

不用将所有项目的利润和佣金相加得出净利润总额,您可以从其他总和中计算出来:

decimal netprofittotal = grossprofittotal + netprofittotal;

本文地址:https://www.itbaoku.cn/post/1557177.html

问题描述

I clearly don't know what I'm doing. This MVC stuff is really blowing my mind in trying to keep with the pattern. I've been following the MVC tutorials as well as mega-googling and this is the corner I've painted myself into.

I have multiple similar pieces of data I'm trying to get to a view. I'm able to get my code to work, but to me it just looks like it's going to be highly inefficient as we start pulling large recordsets from the db due to multiple calls to the db. So, I have a OrderSummary class, inside the class is this:

public IEnumerable<Order> GetOrders()
{
   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s);

   return orders.ToList();
}

Then this:

public decimal GetGrossProfitTotal()
{
   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s);
   decimal? grossprofittotal = orders.Sum(s => s.Profit);

   return grossprofittotal ?? decimal.Zero;
}

So, if we take that last chunk of code and copy it for totalcommission and netprofittotal that's basically how I have things layed out. I would guess four calls to the db?

Then in the controller:

        var ordersummary = new OrdersSummary();
        var viewModel = new OrderSummary
        {
            Orders = ordersummary.GetOrders(),
            GrossProfitTotal = ordersummary.GetGrossProfitTotal(),
            CommissionTotal = ordersummary.GetCommissionTotal(),
            NetProfitTotal = ordersummary.GetNetProfitTotal(),
        };
        return View(viewModel);

This gets me all the data I need in the view so I can work with it. To me, it just seems unnecessarily redundant and I'm guessing inefficient? If you throw in that I'm also doing sort and search parms, it's a lot of duplicate linq code as well. It seems like I should be able to do something to consolidate the data like this:

   var orders = (from s in db.Orders
                 where s.UserId == uId
                 select s).ToList();

   decimal grossprofittotal = orders.Sum(s => s.Profit);
   decimal commissiontotal = orders.Sum(s => s.Commission);
   decimal netprofittotal = orders.Sum(s => s.Profit + s.Commission);

and then wrap those four pieces of data (orders list, and three decimal values) up nicely in an array (or whatever) and send them to the controller/view. In the view I need to be able to loop through the orders list. Am I way off here? Or, what is standard procedure here with MVC? Thanks.

推荐答案

Yes, fetching the same data four times is indeed inefficient, and completely unneccesary. You can very well fetch it only once and then do the other operations on the data that you have.

You can keep the GetOrders method as it is if you like, but that's all the data that you need to fetch. If you fetch the data in the controller or in the model constructor is mostly a matter of taste. Personally I tend to put more logic in the model than the controller.

As long as you use ToList to make sure that you actually fetch the data (or any other method that realises the result as a collection), you can calculate the sums from what you have in memory. (Without it, you would still be doing four queries to the database.)

Instead of summing up the profit and commision from all items to get the net profit total, you can just calculate it from the other sums:

decimal netprofittotal = grossprofittotal + netprofittotal;