簡體   English   中英

將這兩種方法合二為一

[英]combining these two methods into single one

大家好,我有兩個類,我有兩個方法,我將這兩個類分別傳遞給方法並檢查nameNarrativeHTML並呈現項目符號列表。

public class LibraryHydronicEquipment : AEIMasterBase
{
    public string Name { get; set; }
    public HydronicEquipmentType? Type { get; set; }
    [Column(TypeName = "jsonb")]
    public List<HydronicSystemType> Systems { get; set; }
    [Column(TypeName = "jsonb")]
    public List<EquipmentProperty> Properties { get; set; }
    public string NarrativeHTML { get; set; }
}

public class LibrarySteamEquipment : AEIMasterBase
{
    public string Name { get; set; }
    public SteamEquipmentType? Type { get; set; }
    [Column(TypeName = "jsonb")]
    public List<SteamSystemType> Systems { get; set; }
    [Column(TypeName = "jsonb")]
    public List<EquipmentProperty> Properties { get; set; }
    public string NarrativeHTML { get; set; }
  }

方法一:

  private void BuildBulletedList(List<LibraryHydronicEquipment> libraryHydronicEquipments)
  {
        List<Run> labels = new List<Run>();
        foreach (var equipmentItem in libraryHydronicEquipments)
        {
            if (!string.IsNullOrEmpty(equipmentItem.NarrativeHTML))
            {
                var htmlRuns = this.DocumentHtmlConverter.Parse(equipmentItem.NarrativeHTML)
                                                         .First()
                                                         .ChildElements
                                                         .Where(c => c is Run)
                                                         .Cast<Run>()
                                                         .Select(n => n.CloneNode(true));
                labels.Add(new Run(htmlRuns));
            }
            else if (!string.IsNullOrEmpty(equipmentItem.Name))
            {
                labels.Add(new Run(new Text(equipmentItem.Name)));
            }
        }
        BuildList(labels);
  }

方法二

  private void BuildBulletedList(List<LibrarySteamEquipment> librarySteamEquipments)
  {
        List<Run> labels = new List<Run>();
        foreach (var equipmentItem in librarySteamEquipments)
        {
            if (!string.IsNullOrEmpty(equipmentItem.NarrativeHTML))
            {
                var htmlRuns = this.DocumentHtmlConverter.Parse(equipmentItem.NarrativeHTML)
                                                         .First()
                                                         .ChildElements
                                                         .Where(c => c is Run)
                                                         .Cast<Run>()
                                                         .Select(n => n.CloneNode(true));
                labels.Add(new Run(htmlRuns));
            }
            else if (!string.IsNullOrEmpty(equipmentItem.Name))
            {
                labels.Add(new Run(new Text(equipmentItem.Name)));
            }
        }
        BuildList(labels);
  }

我正在調用這些方法,如下所示

if (hydronicSystem.Equipment.Source.Count != 0)
{                       
      BuildBulletedList(hydronicSystem.Equipment.Source);
}
   
if (steamSystem.Equipment.Source.Count != 0)
{
     BuildBulletedList(steamSystem.Equipment.Source);
}

更新:

 if (hydronicSystem.Equipment.Source.Count != 0)
 {
      BuildBulletedList(hydronicSystem.Equipment.Source);
 }
 if (hydronicSystem.Equipment.Distribution.Count != 0)
 {
      BuildBulletedList(hydronicSystem.Equipment.Distribution);
 }
 if (hydronicSystem.Equipment.Terminals.Count != 0)
 {
      BuildBulletedList(hydronicSystem.Equipment.Terminals);
 }
        

有什么方法可以將這兩種方法組合成一個通用方法?

提前致謝!!

將共享成員移動到基本類型

public interface IRender {
    string Name { get; set; }
    string NarrativeHTML { get; set; }
}

並讓類派生自該類型

public class LibraryHydronicEquipment : AEIMasterBase, IRender {
    public string Name { get; set; }
    public HydronicEquipmentType? Type { get; set; }
    [Column(TypeName = "jsonb")]
    public List<HydronicSystemType> Systems { get; set; }
    [Column(TypeName = "jsonb")]
    public List<EquipmentProperty> Properties { get; set; }
    public string NarrativeHTML { get; set; }
}

public class LibrarySteamEquipment : AEIMasterBase, IRender {
    public string Name { get; set; }
    public SteamEquipmentType? Type { get; set; }
    [Column(TypeName = "jsonb")]
    public List<SteamSystemType> Systems { get; set; }
    [Column(TypeName = "jsonb")]
    public List<EquipmentProperty> Properties { get; set; }
    public string NarrativeHTML { get; set; }
}

然后通過 generics 重構方法以依賴於該類型

private void BuildBulletedList<T>(IEnumerable<T> items) where T : IRender {
    List<Run> labels = new List<Run>();
    foreach (T item in items) {
        if (!string.IsNullOrEmpty(item.NarrativeHTML)) {
            var htmlRuns = DocumentHtmlConverter
                .Parse(item.NarrativeHTML)
                .First()
                .ChildElements
                .Where(c => c is Run)
                .Cast<Run>()
                .Select(n => n.CloneNode(true));
            labels.Add(new Run(htmlRuns));
        } else if (!string.IsNullOrEmpty(item.Name)) {
            labels.Add(new Run(new Text(item.Name)));
        }
    }
    BuildList(labels);
}

現在進行一次調用,連接列表,然后檢查是否可以構建項目符號列表。

var source = hydronicSystem.Equipment.Source.Cast<IRender>();
var distribution = hydronicSystem.Equipment.Distribution.Cast<IRender>();
var terminals = hydronicSystem.Equipment.Terminals.Cast<IRender>();

var bullets = source.Concat(distribution).Concat(terminals);
if (bullets.Any()) {
    BuildBulletedList(bullets);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM