如果我最常使用Lists中的类Range,那么继承类List并将解析器算法移动到构造函数是否是一个好习惯?
public class Range<T>
{
public T From { get; set; }
public T To { get; set; }
}
没有继承:
public class RangeParser
{
public static IList<Range<int>> Parser(string range)
{
// parse the string and return a new list
}
}
public class RestrictionsFoo
{
public static ICriterion BetweenRanges<T>(IList<Range<T>> ranges, string propertyName)
{
var disjunction = Restrictions.Disjunction();
foreach (Range<T> range in ranges)
disjunction.Add(Restrictions.Between(propertyName, range.De, range.Ate));
return disjunction;
}
}
继承:
public class RangeCollection<T> : List<Range<T>>
{
public RangeCollection<T>(string range)
{
// parse the string and load this collection
}
}
public class RestrictionsFoo
{
public static ICriterion BetweenRanges<T>(RangeCollection<T> ranges, string propertyName)
{
var disjunction = Restrictions.Disjunction();
foreach (var range in ranges)
disjunction.Add(Restrictions.Between(propertyName, range.De, range.Ate));
return disjunction;
}
}
答案 0 :(得分:1)
似乎最简单的解决方案是在Range&lt;中使用静态Parse方法。 T&gt ;,只要您可以从字符串中解析T本身。为此我会使用TypeConverter
的设施