如何解决代码分析警告CA1506:“避免过多的类耦合”

时间:2013-10-08 05:32:11

标签: c# wpf .net-4.0 .net-4.5 code-analysis

我正面临以下代码的CA1506代码分析警告

private void ShowProductStatistics(object obj)
    {
        this.currentProduct = obj as Products;
        Task.Factory.StartNew(() =>
        {
            var topOrderQuery = (from orderDetail in new XPQuery<OrderDetails>(new Session())
                                 where
                                     orderDetail.ProductID.ProductID == currentProduct.ProductID
                                 orderby
                                     (orderDetail.UnitPrice * orderDetail.Quantity) descending
                                 select new TopOrder
                                 {
                                     OrderId = orderDetail.OrderID.OrderID,
                                     TotalSales = orderDetail.UnitPrice * orderDetail.Quantity
                                 }).ToList().Take(10);

            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.TopProduct = topOrderQuery; }));

            var orderPerYearQuery = (from order in new XPQuery<OrderDetails>(new Session())
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new OrderPYear
                                         {
                                             TotalOrder = g.Count(),
                                             OrderYear = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.OrderPerYear = orderPerYearQuery; }));

            var salesPerYearQuery = (from order in new XPQuery<OrderDetails>(new Session())
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new SalesPYear
                                         {
                                             Sales = g.Sum(p => p.UnitPrice * p.Quantity),
                                             Year = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.SalesPerYear = salesPerYearQuery; }));
        });
    }

我尝试通过遵循msdn中给出但未成功的建议来解决此警告。 任何人都可以帮我解决这个警告吗?

谢谢&amp;问候, Rudresh

1 个答案:

答案 0 :(得分:4)

按如下方式更改代码。

this.currentProduct = obj as Products;
        List<OrderDetails> orderDetailLIst = new XPQuery<OrderDetails>(new Session()).ToList();
        Task.Factory.StartNew(() =>
        {
            var topOrderQuery = (from orderDetail in orderDetailLIst
                                 where
                                     orderDetail.ProductID.ProductID == currentProduct.ProductID
                                 orderby
                                     (orderDetail.UnitPrice * orderDetail.Quantity) descending
                                 select new TopOrder
                                 {
                                     OrderId = orderDetail.OrderID.OrderID,
                                     TotalSales = orderDetail.UnitPrice * orderDetail.Quantity
                                 }).ToList().Take(10);

            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.TopProduct = topOrderQuery; }));

            var orderPerYearQuery = (from order in orderDetailLIst
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new OrderPYear
                                         {
                                             TotalOrder = g.Count(),
                                             OrderYear = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.OrderPerYear = orderPerYearQuery; }));

            var salesPerYearQuery = (from order in orderDetailLIst
                                     where order.ProductID.ProductID == currentProduct.ProductID
                                     group order by new { order.OrderID.OrderDate.Year }
                                         into g
                                         select new SalesPYear
                                         {
                                             Sales = g.Sum(p => p.UnitPrice * p.Quantity),
                                             Year = g.Key.Year
                                         }).ToList();
            DispatcherExt.CurrentDispatcher.BeginInvoke(new Action(() => { this.SalesPerYear = salesPerYearQuery; }));
        });

警告的原因是“来自新的XPQuery(新的Session())中的orderDetail”声明,我使用了3次,因此它发出了警告。我试图减少耦合。

相关问题