我想将两个功能结合在一起,然后重用,以便遵循干式代码的SOLID原则。我有两个不同的列表,都包含id作为其对象的属性。我想将这种逻辑结合起来并给另一个方法参数,以便它将执行重复的逻辑。
public static bool IsParfumesStyleValid(string style, List<Parfumes> parfumes)
{
foreach (var parfume in parfumes)
{
var matchNumbersInDecimal = Regex.IsMatch(parfume.Id, "^(\\d*\\.)\\d+");
var matchFullNumbers = Regex.IsMatch(parfume.Id, "^\\d+$");
var matchNumbersWithHalfs = Regex.IsMatch(parfume.Id, "^[1-9][0-9]*\\/[1-9][0-9]*");
if ((style == "decimal" && !matchNumbersInDecimal)
|| (style == "full" && !matchFullNumbers)
|| (style == "numbersWithHalfs" && !matchNumbersWithHalfs))
{
return false;
}
}
return true;
}
public static bool IsCosmeticsStyleValid(string style, List<Cosmetics> cosmetics)
{
foreach (var item in cosmetics)
{
var matchNumbersInDecimal = Regex.IsMatch(item.Id, "^(\\d*\\.)\\d+");
var matchFullNumbers = Regex.IsMatch(item.Id, "^\\d+$");
var matchNumbersWithHalfs = Regex.IsMatch(item.Id, "^[1-9][0-9]*\\/[1-9][0-9]*");
if ((style == "decimal" && !matchNumbersInDecimal)
|| (style == "full" && !matchFullNumbers)
|| (style == "numbersWithHalfs" && !matchNumbersWithHalfs))
{
return false;
}
}
return true;
}
我的建议现在不起作用,看起来像这样。
public static bool IsObjectsStyleValid(string style, List<Cosmetics> cosmetics,List<Parfumes> parfumes)
{
var list;
if (parfumes == null)
{
list = cosmetics;
}
else
{
list = parfumes;
}
foreach (var item in list)
{
var matchNumbersInDecimal = Regex.IsMatch(item.Id, "^(\\d*\\.)\\d+");
var matchFullNumbers = Regex.IsMatch(item.Id, "^\\d+$");
var matchNumbersWithHalfs = Regex.IsMatch(item.Id, "^[1-9][0-9]*\\/[1-9][0-9]*");
if ((style == "decimal" && !matchNumbersInDecimal)
|| (style == "full" && !matchFullNumbers)
|| (style == "numbersWithHalfs" && !matchNumbersWithHalfs))
{
return false;
}
}
return true;
}
有人可以给我合适的例子吗?
答案 0 :(得分:5)
如果您可以控制Cosmetics
和Parfumes
类,我会让它们都实现带有公共字符串Id属性的接口:
interface IHaveId
{
string Id {get;}
}
然后,您可以执行以下操作,而无需更改已发布的代码:
public static bool IsStyleValid<T>(string style, List<T> product)
where T : IHaveId
{
foreach (var item in product)
{
var matchNumbersInDecimal = Regex.IsMatch(item.Id, "^(\\d*\\.)\\d+");
var matchFullNumbers = Regex.IsMatch(item.Id, "^\\d+$");
var matchNumbersWithHalfs = Regex.IsMatch(item.Id, "^[1-9][0-9]*\\/[1-9][0-9]*");
if ((style == "decimal" && !matchNumbersInDecimal)
|| (style == "full" && !matchFullNumbers)
|| (style == "numbersWithHalfs" && !matchNumbersWithHalfs))
{
return false;
}
}
return true;
}
答案 1 :(得分:3)
如果您使用选择器功能来指出要用于“ Id”的属性,则可以将它们组合在一起。
public static bool IsObjectsStyleValid<T>(string style, IEnumerable<T> items, Func<T, string> idSelector)
{
foreach (var id in items.Select(idSelector))
{
if ((style == "decimal" && !Regex.IsMatch(id, "^(\\d*\\.)\\d+"))
|| (style == "full" && !Regex.IsMatch(id, "^\\d+$"))
|| (style == "numbersWithHalfs" && !Regex.IsMatch(id, "^[1-9][0-9]*\\/[1-9][0-9]*")))
{
return false;
}
}
return true;
}
用法:
IsObjectsStyleValid("decimal", parfumes, x => x.Id);
IsObjectsStyleValid("decimal", cosmetics, x => x.Id);
答案 2 :(得分:0)
将扩展方法与应用方法约束一起使用即可实现,请检查以下示例
interface IProduct
{
string Id {get;}
}
class Cosmetic : IProduct
{
public string Id {get;}
//Other members
}
class Parfume : IProduct
{
public string Id {get;}
//Other members
}
public static class AppExtension
{
public static bool IsStyleValid<T>(this List<T> instance, string style)
where T : IProduct
{
foreach (var item in instance)
{
var matchNumbersInDecimal = Regex.IsMatch(item.Id, "^(\\d*\\.)\\d+");
var matchFullNumbers = Regex.IsMatch(item.Id, "^\\d+$");
var matchNumbersWithHalfs = Regex.IsMatch(item.Id, "^[1-9][0-9]*\\/[1-9][0-9]*");
if ((style == "decimal" && !matchNumbersInDecimal)
|| (style == "full" && !matchFullNumbers)
|| (style == "numbersWithHalfs" && !matchNumbersWithHalfs))
{
return false;
}
}
return true;
}
}
static void Main(string[] args)
{
List<Cosmetics> cosmetics = GetData();
bool x = cosmetics.IsStyleValid("decimal");
}