0

I have a configuration defined for duplicate check on each property in my model below

public class ConfigurationModel
{
    public GroupValue Value1 { get; set; }
    public GroupValue Value2 { get; set; }
    public GroupValue Value3 { get; set; }
    public GroupValue Value4 { get; set; }
    public GroupValue Value5 { get; set; }
}
public class GroupValue
{
    public bool duplicateCheck { get; set; }
}

I have the real class which has all the properties defined like Value1, Value2 etc, which we will call it as OrderModel

public class OrderModel
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
}

Now I want to include only those properties for duplicate check which have the respective flag in configuration set to true. Example

bool CheckDuplicate(OrderModel newOrder)
{
OrderModel existingOrder = //GetfromDB;
//Compare only those properties in newOrder and existingOrder which have duplicate check set to true in ConfigurationModel
}

Using reflection to getproperties by string name and then compare is what I had in mind, But wanted to check the best way to do it, keeping performance in mind as well. Any help would be greatly appreciated.

Thanks, AK

4

1 回答 1

0

当我理解正确时,这些属性在编译时是未知的?在这种情况下,我认为最好的方法是使用反射。(通过使用 nameof() 函数从反射对象中获取属性,您可以获得一种类型安全性,而不是使用硬编码字符串)。

另一方面,如果您知道在编译时设置的完整属性,则可以根据 ConfigurationModel 中的设置为属性创建受控的重复检查。

编辑:简单地尝试一种对数据库的命中测试:

bool CheckDuplicate(OrderModel newOrder)
{
  OrderModel existingOrder = //GetfromDB WHERE dbOrder.Value1 == newOrder.Value1 AND dbOrder.Value2 == newOrder.Value2 AND_SO_ON;
  if(existingOrder/*.FirstOrDefault() -> if IEnumerble is returned from DB*/ == null) return false;
  return true;
}
于 2020-10-15T08:37:00.150 回答