检查两个对象之间是否所有属性(两个属性除外)匹配的干净方法



我有一个包含大约 20 个属性的组件的数据库。要了解是否需要更新,我想检查两个对象的所有属性(日期创建和 Id 除外)是否匹配。如果全部匹配没有更新,如果没有,则更新数据库。

Component comp_InApp = new Component()
{
    Id = null,
    Description = "Commponent",
    Price = 100,
    DateCreated = "2019-01-30",
    // Twenty more prop
};
Component comp_InDb = new Component()
{
    Id = 1,
    Description = "Component",
    Price = 100,
    DateCreated = "2019-01-01",
    // Twenty more prop
};
// Check if all properties match, except DateCreated and Id.
if (comp_InApp.Description == comp_InDb.Description &&
    comp_InApp.Price == comp_InDb.Price
    // Twenty more prop
    )
{
    // Everything up to date.
}
else
{
    // Update db.
}

这有效,但对于 20 个属性,这不是一种非常干净的方式。有没有更好的方法以更干净的方式获得相同的结果?

一种方法是创建一个实现IEqualityComparer<Component>的类来封装此逻辑并避免修改类Comparer本身(如果您不希望此Equals逻辑一直存在)。然后,您可以将其用于两个Component实例的简单Equals,甚至可以用于接受它作为附加参数的所有 LINQ 方法。

class ComponentComparer : IEqualityComparer<Component>
{
    public bool Equals(Component x, Component y)
    {
        if (object.ReferenceEquals(x, y)) return true;
        if (x == null || y == null) return false;
        return x.Price == y.Price && x.Description == y.Description;
    }
    public int GetHashCode(Component obj)
    {
        unchecked 
        {
            int hash = 17;
            hash = hash * 23 + obj.Price.GetHashCode();
            hash = hash * 23 + obj.Description?.GetHashCode() ?? 0;
            // ...
            return hash;
        }
    }
}

您的简单用例:

var comparer = new ComponentComparer();
bool equal = comparer.Equals(comp_InApp, comp_InDb);

如果您有两个集合并想知道差异,它也可以工作,例如:

IEnumerable<Component> missingInDb = inAppList.Except( inDbList, comparer );
当我不想

/没有时间给自己写EqualsGetHashCode方法时,我正在使用DeepEqual。

你可以简单地从 NuGet 安装它:

Install-Package DeepEqual

并像这样使用它:

    if (comp_InApp.IsDeepEqual(comp_InDb))
    {
        // Everything up to date.
    }
    else
    {
        // Update db.
    }

但请记住,这仅适用于您想要显式比较对象的情况,而不适用于要从List中删除对象的情况或类似情况,当调用EqualsGetHashCode时。

以下是反射的解决方案:

    static bool AreTwoEqual(Component inApp, Component inDb)
    {
        string[] propertiesToExclude = new string[] { "DateCreated", "Id" };
        PropertyInfo[] propertyInfos = typeof(Component).GetProperties()
                                                 .Where(x => !propertiesToExclude.Contains(x.Name))
                                                 .ToArray();
        foreach (PropertyInfo propertyInfo in propertyInfos)
        {
            bool areSame = inApp.GetType().GetProperty(propertyInfo.Name).GetValue(inApp, null).Equals(inDb.GetType().GetProperty(propertyInfo.Name).GetValue(inDb, null));
            if (!areSame)
            {
                return false;
            }
        }
        return true;
    }

您可以使用反射,但它可能会降低应用程序的速度。创建该比较器的另一种方法是使用 Linq 表达式生成它。试试这个代码:

public static Expression<Func<T, T, bool>> CreateAreEqualExpression<T>(params string[] toExclude)
{
    var type = typeof(T);
    var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(p => !toExclude.Contains(p.Name))
        .ToArray();
    var p1 = Expression.Parameter(type, "p1");
    var p2 = Expression.Parameter(type, "p2");
    Expression body = null;
    foreach (var property in props)
    {
        var pare = Expression.Equal(
            Expression.PropertyOrField(p1, property.Name),
            Expression.PropertyOrField(p2, property.Name)
        );
        body = body == null ? pare : Expression.AndAlso(body, pare);
    }
    if (body == null) // all properties are excluded
        body = Expression.Constant(true);
    var lambda = Expression.Lambda<Func<T, T, bool>>(body, p1, p2);
    return lambda;
}

它将生成一个看起来像

(Component p1, Component p2) => ((p1.Description == p2.Description) && (p1.Price == p2.Price))

用法简单

var comporator = CreateAreEqualExpression<Component>("Id", "DateCreated")
    .Compile(); // save compiled comparator somewhere to use it again later
var areEqual = comporator(comp_InApp, comp_InDb);

编辑:为了使其更加类型安全,您可以使用lambda排除属性

public static Expression<Func<T, T, bool>> CreateAreEqualExpression<T>(
  params Expression<Func<T, object>>[] toExclude)
{
    var exclude = toExclude
        .Select(e =>
        {
            // for properties that is value types (int, DateTime and so on)
            var name = ((e.Body as UnaryExpression)?.Operand as MemberExpression)?.Member.Name;
            if (name != null)
                return name;
            // for properties that is reference type
            return (e.Body as MemberExpression)?.Member.Name;
        })
        .Where(n => n != null)
        .Distinct()            
        .ToArray();
    var type = typeof(T);
    var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(p => !exclude.Contains(p.Name))
        .ToArray();
    /* rest of code is unchanged */
}

现在,在使用它时,我们有一个智能感知支持:

var comparator = CreateAreEqualExpression<Component>(
        c => c.Id,
        c => c.DateCreated)
    .Compile();

最新更新