c# - 代码分析警告CA1506 : "Avoid excessive class coupling"如何解决

标签 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 中给出的建议解决此警告,但没有成功。 谁能帮我解决这个警告??

谢谢和问候, 鲁德雷什

最佳答案

修改代码如下。

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

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

关于c# - 代码分析警告CA1506 : "Avoid excessive class coupling"如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239806/

相关文章:

c# - 自动将思维导图转换为另一个应用程序(在 MS Project 中)

wpf - F# WPF MouseMove 参数

c# - 动态和 System.Object 之间的区别

c# - Enum.HasFlag 中的意外结果

c# - 用户报告具有锁定值的 WPF 控件;这怎么会发生?

c# - 具有设计模式统计信息的示例项目

c# - 取消数据流管道中的特定项目

c# - 为什么 32 位首选标志甚至存在?

wpf - WPF 如何处理对空对象属性的绑定(bind)?

c# - TextBlock 不更新内容