作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在做一个项目,我注意到有很多重复的代码,我想将重复的代码合并到一个方法中。
这是重复代码的示例:
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<> 和 Delegates,欢迎提出任何建议。
最佳答案
为滑翔机、座垫和 linkingdevices 对象实现类似于下面的接口(interface):
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)并放置产品特定的任何内容(如果此处的“产品”不完全正确,请更改名称)。
可选地,我会更改 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.
}
关于c# - 避免有细微差别的重复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756483/
我是一名优秀的程序员,十分优秀!