构造函数难题,我有这两个构造函数。一个是制作类的副本,另一个是标准构造函数。我需要调用第一个,以便我可以使用其中的规则。我不应该重复代码,所以我正在寻找一种方法来防止覆盖这两个维度。
public SprinklerLineModel()
{
NearCrossMainDimension = new PipeDimensionModel();
FarCrossMainDimension = new PipeDimensionModel();
this.AddValidationRule(Rule.CreateRule(() => BranchLineDiameter, RuleMessage.GREATER_THAN_ZERO, () => BranchLineDiameter > 0));
}
/// <summary>
/// Copy Constructor
/// </summary>
/// <param name="sprinklerLineModel">Original copy of sprinklerLineModel</param>
public SprinklerLineModel(SprinklerLineModel sprinklerLineModel)
:this()
{
this.EstimatedFlow = sprinklerLineModel.EstimatedFlow;
this.EstimatedPressure = sprinklerLineModel.EstimatedPressure;
this.NearCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.NearCrossMainDimension);
this.FarCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.FarCrossMainDimension);
this.BranchLineDiameter = sprinklerLineModel.BranchLineDiameter;
this.LeadLinePipeFittingLength = sprinklerLineModel.LeadLinePipeFittingLength;
this.ExbPipeFittingLength = sprinklerLineModel.ExbPipeFittingLength;
this.IsDirty = sprinklerLineModel.IsDirty;
}
我希望能够在不覆盖两个变量的情况下调用默认构造函数。任何帮助,将不胜感激。
我想我可以查看它们是否为空,但这看起来并不是最好的方法。
由于
答案 0 :(得分:0)
我将提取一个初始化方法并从两个构造函数中调用所述方法。
public SprinklerLineModel()
{
NearCrossMainDimension = new PipeDimensionModel();
FarCrossMainDimension = new PipeDimensionModel();
Init();
}
public SprinklerLineModel(SprinklerLineModel sprinklerLineModel)
{
this.EstimatedFlow = sprinklerLineModel.EstimatedFlow;
this.EstimatedPressure = sprinklerLineModel.EstimatedPressure;
this.NearCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.NearCrossMainDimension);
this.FarCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.FarCrossMainDimension);
this.BranchLineDiameter = sprinklerLineModel.BranchLineDiameter;
this.LeadLinePipeFittingLength = sprinklerLineModel.LeadLinePipeFittingLength;
this.ExbPipeFittingLength = sprinklerLineModel.ExbPipeFittingLength;
this.IsDirty = sprinklerLineModel.IsDirty;
Init();
}
void Init()
{
this.AddValidationRule(Rule.CreateRule(() => BranchLineDiameter, RuleMessage.GREATER_THAN_ZERO, () => BranchLineDiameter > 0));
}
答案 1 :(得分:0)
我会做这样的事情:
public SprinklerLineModel()
: this(null)
{
}
/// <summary>
/// Copy Constructor
/// </summary>
/// <param name="sprinklerLineModel">Original copy of sprinklerLineModel</param>
public SprinklerLineModel(SprinklerLineModel sprinklerLineModel)
{
this.EstimatedFlow = sprinklerLineModel.EstimatedFlow;
this.EstimatedPressure = sprinklerLineModel.EstimatedPressure;
if (sprinklerLineModel != null)
{
this.NearCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.NearCrossMainDimension);
this.FarCrossMainDimension = new PipeDimensionModel(sprinklerLineModel.FarCrossMainDimension);
}
else
{
NearCrossMainDimension = new PipeDimensionModel();
FarCrossMainDimension = new PipeDimensionModel();
}
this.BranchLineDiameter = sprinklerLineModel.BranchLineDiameter;
this.LeadLinePipeFittingLength = sprinklerLineModel.LeadLinePipeFittingLength;
this.ExbPipeFittingLength = sprinklerLineModel.ExbPipeFittingLength;
this.IsDirty = sprinklerLineModel.IsDirty;
this.AddValidationRule(Rule.CreateRule(() => BranchLineDiameter, RuleMessage.GREATER_THAN_ZERO, () => BranchLineDiameter > 0));
}
优势在于您可以在一个地方实施。
/提笔