不同属性名称的C#重用逻辑

时间:2020-07-25 03:55:12

标签: c#

我有以下保险课程:

public class Coverages
{
    public double? Deductible1 { get; set; }
    public double? Deductible2 { get; set; }
    public double? Deductible3 { get; set; }
    public bool IsCoverage { get; set; }
    public int? Limit1 { get; set; }
    public int? Limit2 { get; set; }
    public int? Limit3 { get; set; }
    public string DeductType1 { get; set; }
    public string DeductType2 { get; set; }
    public string DeductType3 { get; set; }
}

我也有以下逻辑:

if (coverage.Deductible1.HasValue)
{
    var coverageValue = !string.IsNullOrEmpty(coverage.DeductType1)
                                ? string.Format("{0}/{1}", coverage.DeductType1, coverage.Deductible1)
                                : coverage.Deductible1.Value.ToString();

    coverages.Add(new CoverageDto
    {
        Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
        Value = coverageValue
    });
}

我想将此逻辑重新用于Deductible2Deductible3,它们当然将使用其各自的DeductTypes(即DeductType1Deductible1)。如何参数化类的属性以重用此逻辑,还是有更好的方法呢?

谢谢。

2 个答案:

答案 0 :(得分:3)

DeductibleLimitDeductType似乎非常相关,应将其分组为类/结构:

public class DeductibleInfo {
    public double? Deductible { get; set; }
    public int? Limit { get; set; }
    public string DeductType { get; set; }

    // Constructors...
    // ...
}

现在Coverage可以包含DeductibleInfo个数组:

public DeductibleInfo[] Deductibles { get; } = new DeductibleInfo[] {
    new DeductibleInfo(),
    new DeductibleInfo(),
    new DeductibleInfo()
};

现在,您可以轻松提取逻辑方法:

public void SomeMethod(Coverage coverage, int deductibleIndex) {
    var deductibleInfo = coverage.Deductibles[deductibleIndex];
    if (deductibleInfo.Deductible.HasValue)
    {
        var coverageValue = !string.IsNullOrEmpty(deductibleInfo.DeductType)
                                    ? string.Format("{0}/{1}", deductibleInfo.DeductType, deductibleInfo.Deductible)
                                    : deductibleInfo.Deductible.Value.ToString();
    
        coverages.Add(new CoverageDto
        {
            Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
            Value = coverageValue
        });
    }
}

答案 1 :(得分:1)

如果您遵循一条简单的规则,则可以考虑“任何时候只要在属性名的末尾都带有数字,它就可以通过数组支持它,以便可以通过编程方式对其进行寻址”

public class Coverages
{
    private double?[3] deductibles = new double?[3];
    public double? Deductible1 { get => deductibles[0]; set => deductibles[0] = value; }
    public double? Deductible2 { get => deductibles[1]; set => deductibles[1] = value; }
    
    ...

然后您可以以编程方式引用它们;例如,

for(int x = 0; x<deductibles.Length; x++)
{
  if (coverage.deductibles[x].HasValue)
  {
    var coverageValue = !string.IsNullOrEmpty(coverage.deductTypes[x])
                                ? string.Format("{0}/{1}", coverage.deductTypes[x], coverage.deductibles[x])
                                : coverage.deductibles[x].Value.ToString();

    coverages.Add(new CoverageDto
    {
        Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
        Value = coverageValue
    });
  }
}

因此,如果您希望自己可以引用一些具有相似名称模式的相似命名属性,则可能需要它们的集合