C#优化新对象的创建(太多,丑陋的代码)

时间:2015-01-15 14:54:41

标签: c# constructor getter-setter

我担心这个问题可能是愚蠢的/我不完全是怎么问这个问题但是我们走了:

我目前正在开展一个需要创建大量新对象的项目。 属性由多个ifs设置,代码看起来非常混乱。

/// <summary>
/// Creates a new <see cref="StockItem"/> based on the <see cref="Article"/> of the given <param name="input"/>.
/// </summary>
/// <param name="input"></param>
public static StockItem CreateStockItem(InputParameterCreateStockItem input)
{
    var returnStockItem = new StockItem();

    //If there is an imob item, start from there and update everything
    if (input.StockItem != null)
    {
        returnStockItem = input.StockItem;
    }
    else
    {
        //When creating a stock item, the status is ALWAYS 1
        returnStockItem.Status = 1;
    }

    #region Properties
    returnStockItem.PartTypeNo = (Int32)input.Article.ArticleNumberPrefix + "-" + input.Article.ArticleNumber.Value.ToString("D8");
    returnStockItem.Kardex = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.KardexT) ?? String.Empty;
    returnStockItem.WeldNumber = input.WeldNumber;
    returnStockItem.ObjectNo = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ObjectNoT) ?? ConvertHelper.GetObjectNo(input.Article);
    returnStockItem.Synonymous = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.SynonymousT) ?? String.Empty;
    returnStockItem.ExtSerialNo = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ExtSerialNoT) ?? String.Empty;
    returnStockItem.ArticleReference = (ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ArticleReferenceT)) ??
                                   ConvertHelper.RemoveColorCodeFromReference(input.Article.Reference, input.ColorList);
    returnStockItem.ArticleName = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ArticleNameT) ??
                              ConvertHelper.GetCorrectTranslation(input.Article.Names, "ENG");
    returnStockItem.Length = ConvertHelper.GetMeasurementInMeters(input.Article, ConvertHelper.ImobLengthT, ConvertHelper.ClsLengthT);
    returnStockItem.Width = ConvertHelper.GetMeasurementInMeters(input.Article, ConvertHelper.ImobWidthT, ConvertHelper.ClsWidthT);
    returnStockItem.Height = ConvertHelper.GetMeasurementInMeters(input.Article, ConvertHelper.ImobHeightT, ConvertHelper.ClsHeightT);
    returnStockItem.Weight =
        Decimal.Parse(!String.IsNullOrEmpty(ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ImobWeightT))
            ? ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ImobWeightT)
            : ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.ClsWeightT) ?? "0");
    returnStockItem.DepreciationCode = ConvertHelper.GetTechSpecValue(input.Article.TechnicalSpecifications, ConvertHelper.DepreciationCodeT) ??
                                   (input.Article.DepreciationCode != null ? input.Article.DepreciationCode.InsuranceValueCode.ToString() +
                                    input.Article.DepreciationCode.ActualValueCode.ToString() +
                                    input.Article.DepreciationCode.RevaluationCode.ToString() : String.Empty);
    returnStockItem.Value = ConvertHelper.GetValue(input.Article);
    returnStockItem.ValueDate = ConvertHelper.GetValueDate(input.Article);
    returnStockItem.Brand = ConvertHelper.GetBrand(input.Article);
    returnStockItem.Remarks = ConvertHelper.GetRemarks(input.Article);
    returnStockItem.InsuranceValue = ConvertHelper.GetInsuranceValue(input.Article);
    returnStockItem.StockItemGroupID = ConvertHelper.GetStockItemGroupId(input.Article, input.ClsImobArticleGroups, input.StockItemGroups);
    returnStockItem.StockAllocations = StockAllocationDAO.GetStockAllocationsForArticleGroupIDs(input.Article, input.ClsAssignedArticleGroups, input.ClsUnAssignedArticleGroups, input.ClsImobArticleGroups, input.StockAllocationsDictionaryByCode, input.StockAllocationsDictionaryByName);
    #endregion

    //AddMotherStockItemColorCode(input.Article, stockItem);

    return returnStockItem;
}

我想知道是否有更好(更易读)的方法来做到这一点。

我已经完成的事情:

  • 将更复杂的代码移至Helper Class(ConvertHelper
  • 创建了一个输入参数对象(我需要很多列表来检查某些内容)
  • 将整个方法移到工厂(感觉不必要?)

但整件事让我觉得麻烦。

谢谢你, 托马斯

1 个答案:

答案 0 :(得分:4)

你试过AutoMapper吗? https://github.com/AutoMapper/AutoMapper 花一些时间查看文档,你会发现你会很快回来的。