[英]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/Check
和Equals
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.