我正在研究一个项目,我注意到了很多重复的代码,我想将重复的代码整合到一个方法中。
这是重复代码的示例:
foreach (var glider in gliders)
{
List<PriceDataModel_New> bestPrices = PriceService.GetBestPrices(prices, glider.Value.No, string.Empty, string.Empty, string.Empty, 1);
var priceGroups = bestPrices.GroupBy(p => p.SalesCode);
var salesCodePrice = priceGroups.ToDictionary(k => k.Key, v => v.First());
AddEmptyines(fieldMapping, lines);
var last = lines.Last();
foreach (var keyValuePair in fieldMapping.Postions)
{
int index = keyValuePair.Key;
var key = keyValuePair.Value.InternalHeading;
InsertInLines(last, key, index, "CODE_Id", modelNo + "_" + glider.Value.No);
InsertInLines(last, key, index, "ItemId", glider.Value.No);
InsertInLines(last, key, index, "CODE_OptionalName", (glider.Value.ComponentType + " " + glider.Value.ProductFamily).ToLower());
InsertInLines(last, key, index, "Attr_Family name", family);
InsertInLines(last, key, index, "CODE_IsOptional", "true");
InsertInLines(last, key, index, "Model", modelNo);
InsertInLines(last, key, index, "CODE_OptionalInfo", glider.Value.Size.ToLower());
if (AddToLinePrice(salesCodePrice, keyValuePair.Value.InternalHeading, index, last))
continue;
}
}
//AppendLines(seatPads, prices, lines, fieldMapping, "", modelNo, family, "linking.Value.SimpleMaterial", "");
foreach (var seatPad in seatPads)
{
List<PriceDataModel_New> bestPrices = PriceService.GetBestPrices(prices, seatPad.Value.No, seatPad.Value.Variant.Substring(0, 3), string.Empty, string.Empty, 1);
var priceGroups = bestPrices.GroupBy(p => p.SalesCode);
var salesCodePrice = priceGroups.ToDictionary(k => k.Key, v => v.First());
AddEmptyines(fieldMapping, lines);
var last = lines.Last();
foreach (var keyValuePair in fieldMapping.Postions)
{
int index = keyValuePair.Key;
var key = keyValuePair.Value.InternalHeading;
InsertInLines(last, key, index, "CODE_Id", modelNo + "_" + seatPad.Value.No);
InsertInLines(last, key, index, "ItemId", seatPad.Value.No);
InsertInLines(last, key, index, "CODE_OptionalName", seatPad.Value.ModelNo.ToLower());
InsertInLines(last, key, index, "Attr_Family name", family);
InsertInLines(last, key, index, "CODE_IsOptional", "true");
InsertInLines(last, key, index, "Model", modelNo);
InsertInLines(last, key, index, "CODE_OptionalInfo", seatPad.Value.UpholsteryFabric.ToLower() + " black");
if (AddToLinePrice(salesCodePrice, keyValuePair.Value.InternalHeading, index, last))
continue;
}
}
//AppendLines(linkingDevices, prices, lines, fieldMapping, "", modelNo, family, "linking.Value.SimpleMaterial", "");
foreach (var linking in linkingDevices)
{
List<PriceDataModel_New> bestPrices = PriceService.GetBestPrices(prices, linking.Value.No, string.Empty, string.Empty, string.Empty, 1);
var priceGroups = bestPrices.GroupBy(p => p.SalesCode);
var salesCodePrice = priceGroups.ToDictionary(k => k.Key, v => v.First());
AddEmptyines(fieldMapping, lines);
var last = lines.Last();
foreach (var keyValuePair in fieldMapping.Postions)
{
int index = keyValuePair.Key;
var key = keyValuePair.Value.InternalHeading;
InsertInLines(last, key, index, "CODE_Id", modelNo + "_" + linking.Value.No);
InsertInLines(last, key, index, "ItemId", linking.Value.No);
InsertInLines(last, key, index, "CODE_OptionalName", linking.Value.ComponentType.ToLower());
InsertInLines(last, key, index, "Attr_Family name", family);
InsertInLines(last, key, index, "CODE_IsOptional", "true");
InsertInLines(last, key, index, "Model", modelNo);
InsertInLines(last, key, index, "CODE_OptionalInfo", linking.Value.SimpleMaterial);
if (AddToLinePrice(salesCodePrice, keyValuePair.Value.InternalHeading, index, last))
continue;
}
}
上面的foreach循环只有几行不同。我无法弄清楚如何使这个通用。我尝试过使用Reflection,Func&lt;&gt;和代表们,非常欢迎任何建议。
答案 0 :(得分:3)
为滑翔机,座垫和链接装置对象实现类似于以下的界面:
public interface IProduct
{
string No { get; }
string CodeName { get; }
string Family { get; }
string ModelNo { get; }
string CodeInfo { get; }
IDictionary<string, string> FieldMapping { get; }
}
然后创建一个通用函数:
private void Generic<T>(IEnumerable<T> products, string modelNo)
where T: IProduct
接受产品之外的任何内容(例如输入中的modelNo)并将特定产品放入产品中(如果&#39;产品&#39;不在此处,则更改名称)。
可选地,我会将Fieldmapping字典更改为扁平化属性,以便在有意义的情况下(虽然我不确定底层的复杂性):
public interface IProduct
{
string No { get; }
string CodeName { get; }
string Family { get; }
string ModelNo { get; }
string SalesCode { get; }
string CodeInfo { get; }
IEnumerable<IProductAttribute> Attributes { get; }
}
public interface IProductAttribute
{
string InternalHeading { get; }
int Index { get; } //not sure what this is for.
}
答案 1 :(得分:0)
对于初学者来说,有一个基类用于滑翔机,seatPad和链接子类。
提取内部foreach的主体并将其移动到自己的方法中。传递的参数应该是基类类型,因此代码适用于所有子类型。
其余的应该是前进的。
答案 2 :(得分:0)
嗯,很难提出最佳解决方案,因为它非常主观...... Func<>
可能是合适的,有些情况下差异是未成年人,或者你只有一些差异。
这里使用一个通用界面(如果你不想改变原始对象,可能还有一些适配器/外观/桥接器)可能更合适。
或者,您也可以创建另一个具有所需字段的类,并将原始数据转换为该新格式。
哪种解决方案更有意义?好吧,由你来评估。
你也可以看看其他相关的设计模式......