2017-05-03 74 views
3

下面是方法的两个实施例中,第一个使用foreach语句来填充数据模型使用存储过程作为返回对象而不是使用foreach填充数据模型的后果?

public List<TheOrderSummary> GetTheOrderSummaryByID(int id) 
    { 
     ExoEntities = new ExoEntities(); 
     List<TheOrderSummary> lst = new List<TheOrderSummary>(); 

     foreach(var a in query) 
     { 
      lst.Add(new TheOrderSummary 
      { 

       OrderDetailID = a.OrderDetailID, 
       OrderHeaderID = a.OrderHeaderID, 
       ItemSeq = a.ItemSeq, 
       MaterialID = a.MaterialID, 
       Description = a.Description, 
       DWidth = a.DWidth, 
       DLength = a.DLength, 
       MaterialArea = a.MaterialArea.ToString(), 
       MaterialDetail = a.MaterialDetail, 
       DHeight = a.DHeight, 
       PurchUnitOfMeasure = a.PurchUnitOfMeasure, 
       SellUnitOfMeasure = a.SellUnitOfMeasure, 
       MaterialCategory = a.MaterialCategory, 
       MaterialType = a.MaterialType, 
       MaterialSubType = a.MaterialSubType, 
       ColorID = (int)a.ColorID, 
       Color = a.Color, 
       MaterialPrice = a.MaterialPrice, 
       MaterialCost = a.MaterialCost, 
       MaterialLocationID = (int)a.MaterialLocationID, 
       MaterialLocation = a.MaterialLocation, 
       LaborPrice = a.LaborPrice, 
       LaborCost = a.LaborCost, 
       VendorID = (int)a.VendorID, 
       VendorName = a.VendorName, 
       Size = a.Size, 
       Height = (decimal)a.Height, 
       Length = (decimal)a.Length, 
       Width = (decimal)a.Width, 
       PurchaseQuantity = (decimal)a.PurchaseQuantity, 
       SellQuantity = (decimal)a.SellQuantity, 
       TotalFootage = (decimal)a.TotalFootage, 
       GeneratedItemInd = (int)a.GeneratedItemInd, 
       ExtendedMaterialPrice = (decimal)a.ExtendedMaterialPrice, 
       ExtendedLaborCost = (decimal)a.ExtendedLaborCost, 
       ExtendedMaterialCost = (decimal)a.ExtendedMaterialCost 
      }); 
     } 

     return lst; 
    } 

而这一次使用存储过程返回一个对象

public List<usp_GetTheOrderDetails_Result> GetTheOrderSummaryByID(int id) 
    { 
     ExoEntities = new ExoEntities(); 

     var query = ExoEntities.usp_GetTheOrderDetails(id); 

     return query.ToList(); 
    } 

这两者都是在DAL中,可以调用其中之一的方法是JSONResult,它们都可用于填充网格。使用第二种类型会产生什么后果而不是第一种?他们从性能水平上看起来完全一样,没有做数字,第二个会更快

回答

0

通过直接返回结果引入的痛苦是,你得到一个“硬有线“依赖于消费者和底层数据提供者之间的信息契约。如果您对基础数据结构进行了更改,则必须同时更新客户端(可能会带来不同程度的痛苦)。

如果您改为使用第一个选项,则将基础信息结构的知识封装到此层中,并可能使用它来映射新旧契约,从而将客户端和数据之间的直接依赖关系解耦供应商。

所以,是的,第二个选项可能会稍微快一点(即使它真的应该是边际),但第一个选项在长期维护代码和部署方面可能会让您少得多。

+0

优秀的解释,所以有时候更少的代码并不总是最好的东西,在这种情况下反正 – Chris

相关问题