将一个集合与另一个集合进行比较(从架构上来说)的更好方法是什么?
以下是我目前最喜欢的架构方法的示例:
public abstract class CollectionComparer {
public virtual SetEqual(IEnumerable enum1, IEnumerable enum2) {
if(enum1== null && enum2== null)
return true;
if(enum1== null && !(enum2== null))
return false;
if(!(enum1== null) && enum2== null)
return false;
if(enum1.GetType().DeclaringType != enum2.GetType().DeclaringType)
return false;
var col1 = (from e in enum1 select e).ToList()
var col2 = (from e in enum2 select e).ToList()
if(col1.Count != col2.Count)
return false;
foreach (element in col1)
if(!col2.Contains(element))
return false;
foreach (element in col2)
if(!col1.Contains(element))
return false;
return true;
}
}
public interface IProduct {
...
}
public interface IOrder {
...
ICustomer Customer { get; }
ICollection<IProduct> Products { get; }
}
public interface ICustomer {
...
ICollection<IOrder> Orders { get; }
}
public internal Order : CollectionComparer, IOrder {
#region IOrder interface implementation
...
#endregion
}
public internal Customer : CollectionComparer, ICustomer {
#region ICustomer interface implementation
...
#endregion
}
对于应用于集合的与 object.Equals() 等效的集合比较,此 CollectionComparer 抽象类方法是否会被视为良好实践?
Here's an example of the architecture approach I favorited as for now:
public abstract class CollectionComparer {
public virtual SetEqual(IEnumerable enum1, IEnumerable enum2) {
if(enum1== null && enum2== null)
return true;
if(enum1== null && !(enum2== null))
return false;
if(!(enum1== null) && enum2== null)
return false;
if(enum1.GetType().DeclaringType != enum2.GetType().DeclaringType)
return false;
var col1 = (from e in enum1 select e).ToList()
var col2 = (from e in enum2 select e).ToList()
if(col1.Count != col2.Count)
return false;
foreach (element in col1)
if(!col2.Contains(element))
return false;
foreach (element in col2)
if(!col1.Contains(element))
return false;
return true;
}
}
public interface IProduct {
...
}
public interface IOrder {
...
ICustomer Customer { get; }
ICollection<IProduct> Products { get; }
}
public interface ICustomer {
...
ICollection<IOrder> Orders { get; }
}
public internal Order : CollectionComparer, IOrder {
#region IOrder interface implementation
...
#endregion
}
public internal Customer : CollectionComparer, ICustomer {
#region ICustomer interface implementation
...
#endregion
}
Would this CollectionComparer abstract class approach be considered a good practice for collection comparison for an equivalent to object.Equals() applied to a collections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否看到过:
SequenceEqual
http://msdn .microsoft.com/en-us/library/bb348567.aspx
Have you seen this:
SequenceEqual
http://msdn.microsoft.com/en-us/library/bb348567.aspx
如何实现 IComparable 并为每个项目和每个项目属性递归调用它?已经有一个定义良好的接口来比较对象。有什么原因您不想/不能使用它吗?
How about implementing IComparable and calling it recursively for each item and each item property? There's already a well-defined interface to compare objects. Is there some reason you don't want to/can't use it?