Reputation: 17691
Hi all i have two classes and i have two methods where i am passing these two classes into methods individually and checking name
and NarrativeHTML
and rendering bulleted list.
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; }
}
Method 1:
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);
}
Method 2
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);
}
and i am calling these methods like as below
if (hydronicSystem.Equipment.Source.Count != 0)
{
BuildBulletedList(hydronicSystem.Equipment.Source);
}
if (steamSystem.Equipment.Source.Count != 0)
{
BuildBulletedList(steamSystem.Equipment.Source);
}
update :
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);
}
Is there any way we can combine these two methods into single generic method?
thanks in advance!!
Upvotes: 0
Views: 77
Reputation: 247018
Move the shared members into a base type
public interface IRender {
string Name { get; set; }
string NarrativeHTML { get; set; }
}
and have the classes derive from that type
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; }
}
then refactor the method to depend on that type via 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);
}
Now to make a single call, concatenate the lists and then check to see if a bullet list can be built.
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);
}
Upvotes: 1