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

查看:124
本文介绍了低效的MVC ViewModel多次调用数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然不知道自己在做什么. MVC的这些东西真的让我很想尝试与模式保持一致.我一直在关注MVC教程以及大型搜索,而这正是我向自己描绘的角落.

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.

我要尝试查看多个类似的数据.我能够使我的代码正常工作,但是对我来说,由于多次调用数据库,我们开始从数据库中提取大型记录集时,效率似乎非常低下.所以,我有一个OrderSummary类,该类里面是这样:

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();
}

然后这样:

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?

然后在控制器中:

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

这将为我获取视图中所需的所有数据,以便我可以使用它.对我来说,这似乎不必要地多余,我猜效率低下吗?如果您还说我也在进行排序和搜索Parms,那么也有很多重复的linq代码.看来我应该能够做一些整合这样的数据的事情:

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);

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

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.

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

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.

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

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;

这篇关于低效的MVC ViewModel多次调用数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆