프로젝트를 진행하면서 중복 된 코드가 많다는 것을 알고 있으므로 중복 된 코드를 단일 메서드로 통합하고 싶습니다.사소한 차이가있는 중복 루프 방지
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 <> 및 대표자들과 함께 시도해 봤지만 어떤 제안이라도 대환영입니다.
. 이것은 특정 리팩토링에 대한 요청이므로, 나는 그것이 너무 광범위하다고 생각하지 않습니다. –
@ Jean-FrançoisFabre [this] (http://meta.codereview.stackexchange.com/q/5777/31562)를 읽으셨습니까? 또한 CR을 이전 대상으로 추가하는 것이 새로운 것은 아닙니다. 그러나 그것은 여러 가지 이유로 문제가되는 것입니다. 필요 이상으로 여기에 의견을 남기고 싶지 않으므로 그것에 대해 이야기하고 싶다면 여기에서 나를 찾을 수 있습니다 : http://chat.stackexchange.com/rooms/8595/the-2nd-monitor –
예, 저는 내가 내 질문에 부딪혔을 때 그것을 읽으십시오. 보시다시피 질문은 끝났습니다. CodeReview에서 나는 심지어 대답하려고 노력했을 것입니다.그래서 SO 또는 CR에 있어야하는지 여부는 기본적으로 의견 : –