简体   繁体   English

如何将具有相同主体但不同签名的方法合并在一起?

[英]How to merge methods with the same body but different signatures together?

This is a refactoring question. 这是一个重构问题。

How to merge all these Check() methods into one single Generic Check() method since their method bodies are the same? 如何将所有这些Check()方法合并到一个Generic Check()方法中,因为它们的方法体是相同的?

ppublic class ChangeDetector : IChangeDetector
{
    private readonly IEqualityHelper _equalityHelper;

    public ChangeDetector(IEqualityHelper equalityHelper)
    {
        _equalityHelper = equalityHelper;
    }

    public bool ChangeDetected { get; private set; }

    public void Check<T>(IList<T> existingList, IList<T> newList) where T : IdentifiedActiveRecordBase<T>, new ()
    {
        if (!this._equalityHelper.Equals(existingList, newList))
        {
            NotifyChange();
        }
    }

    public void CheckEntities<T>(IdentifiedActiveRecordBase<T> existingObj, IdentifiedActiveRecordBase<T> newObj) where T : IdentifiedActiveRecordBase<T>, new()
    {
        if (!this._equalityHelper.Equals(existingObj, newObj))
        {
            NotifyChange();
        }
    }

    public void Check(string existing, string newVal)
    {
        if (!this._equalityHelper.Equals(existing, newVal))
        {
            NotifyChange();
        }
    }

    public void Check<T>(T existing, T newVal) where T : struct 
    {
        if (!this._equalityHelper.Equals(existing, newVal))
        {
            NotifyChange();
        }
    }

    public void Check<T>(T? existing, T? newVal) where T : struct 
    {
        if (!this._equalityHelper.Equals(existing, newVal))
        {
            NotifyChange();
        }
    }

    private void NotifyChange()
    {
        ChangeDetected = true;
    }
}

My EqualityHelper class members have different body though which is fine: 我的EqualityHelper类成员有不同的身体虽然很好:

public class EqualityHelper : IEqualityHelper
    {
        public bool Equals<T>(IList<T> existingList, IList<T> newList) where T : IdentifiedActiveRecordBase<T>, new()
        {
            if (existingList == null || existingList.Count == 0)
            {
                if (newList != null && newList.Count > 0)
                {
                    return false;
                }
            }
            else
            {
                if (newList == null
                    || existingList.Count != newList.Count
                    || newList.Any(newListItem => existingList.Any(existingListItem => existingListItem.Id == newListItem.Id)))
                {
                    return false;
                }
            }

            return true;
        }

        public bool Equals<T>(IdentifiedActiveRecordBase<T> existingObj, IdentifiedActiveRecordBase<T> newObj) where T : IdentifiedActiveRecordBase<T>, new()
        {
            if (existingObj == null)
            {
                if (newObj != null)
                {
                    return false;
                }
            }
            else
            {
                if (newObj == null || existingObj.Id != newObj.Id)
                {
                    return false;
                }
            }

            return true;
        }

        public bool Equals(string existing, string newVal)
        {
            return string.Equals(existing, newVal);
        }

        public bool Equals<T>(T existing, T newVal) where T : struct
        {
            return !existing.Equals(newVal);
        }

        public bool Equals<T>(T? existing, T? newVal) where T : struct
        {
            if ((existing.HasValue && !newVal.HasValue)
                || (!existing.HasValue && newVal.HasValue)
                || existing.Equals(newVal))
            {
                return false;
            }

            return true;
        }
    }

Just because the method bodies are looking similar doesn't mean the method signatures can be merged. 仅仅因为方法体看起来相似并不意味着方法签名可以合并。 Each of your five Handle methods calls five different Equals -methods, so unless you can merge the five Equals methods, you can't merge the Handle methods. 您的五个Handle方法中的每一个都调用五个不同的Equals方法,因此除非您可以合并五个Equals方法,否则无法合并Handle方法。 You can't do that of course because the Equals method implementations are different. 你不能这样做,因为Equals方法的实现是不同的。 Remember that which of the Equals-method are to be called is decided compile-time, not runtime. 请记住,要调用哪个Equals方法是由编译时决定的,而不是运行时。

Edit: What you could do is change the signature of both Handle/Check and Equals to Check(object existing, object equals) and Equals(object existing, object equals) . 编辑:可以做的是改变Handle/CheckEquals to Check(object existing, object equals)Equals(object existing, object equals)的签名。 Then in the Equals -method perform a runtime type-check which results in a switch-case to the five Equals-method you already have with the help of type casting. 然后在Equals -method中执行运行时类型检查,这会产生一个switch-case到你在类型转换的帮助下已经拥有的五个Equals方法。 This would make the implementation slower and only arguably more maintainable. 这将使实现更慢,并且只能更可维护。 I'm not sure I would go down that route. 我不确定我会走那条路。

The method bodies aren't really the same, since they're all calling different Equals() methods. 方法体实际上并不相同,因为它们都调用不同的Equals()方法。 What you're intending to do would (if I understand the question correctly) finish up with one Handle<T>() method where T could be any type. 你打算做什么(如果我理解正确的话)完成一个Handle<T>()方法,其中T可以是任何类型。 Thinking about what you're trying to express in the code, it seems fair that if you have one Handle<T>() method, that ought to be able to call one Equals<T>() method. 考虑一下你在代码中试图表达的内容,如果你有一个Handle<T>()方法,它应该能够调用一个Equals<T>()方法似乎是公平的。 That way, you can implement your handling logic once (and potentially this becomes more complex later but you only need to write it once) and you delegate the tricky business of comparing objects to your equality comparer class. 这样,您可以实现一次处理逻辑(以后可能会变得更复杂,但您只需要编写一次),并且委托将对象比较到相等比较器类的棘手业务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何结合签名略有不同的两种方法? - How to combine two methods with slightly different signatures? 继承具有相同方法但签名不同的多个接口的正确方法? - Correct way to Inherit multiple interfaces with same methods but different signatures? 具有不同签名的控制器操作方法 - Controller Action Methods with different signatures 如何命名具有相同签名的2种方法 - What to name 2 methods with same signatures 如何重构具有不同签名但几乎相同的主体的方法? - How do I refactor methods with different signatures but almost identical bodies? 重构方法调用具有不同参数签名的代理 - Refactor methods invoking delgates with different parameter signatures 如何在 .NET 中创建通用方法来捕获应用程序中具有不同签名的不同方法的延迟(经过时间)? - How to create a generic method in .NET to capture latency (elapsed times) of different methods with different signatures in my application? 如何通过LINQ将同一类的对象属性合并在一起 - How to merge object properties of the same class together via LINQ DSA使用相同的数据生成不同的签名 - DSA generates different signatures with the same data 如何使用 C# 和 ClosedXML 将相同的值合并在一起? - How to merge same values together using C# and ClosedXML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM