2

構造函數難題,我有這兩個構造函數。一個是製作該類的副本,另一個是標準構造函數。我需要調用第一個,以便我可以在其中使用規則。我不應該重複代碼,所以我正在尋找一種避免覆蓋這兩個維度的方法。複製構造函數去基礎構造函數並覆蓋複製值

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

我會抽出一個初始化方法和電話會議上說,從兩個構造方法。

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)); 
} 
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)); 
} 

的好處是,你必須在一個地方implemntation。

/Tibi