我有一个为所有DataAccess使用WCF的应用程序。它返回一些业务对象,它有一些操作。我应该在哪里将Comparers放入使用WCF的应用程序中?
此代码是否应存在于我的客户端或我的服务中?如果在服务中,我应该如何实现它?我可以简单地将它添加为我的业务对象的接口吗?这是通过WCF服务代理代码实现的吗?
(这是从MSDN的样本,我想在我实现我自己能得到一些反馈,但是这将是99%相同)
// Custom comparer for the Product class.
class ProductComparer : IEqualityComparer<Product>
{
// Products are equal if their names and product numbers are equal.
public bool Equals(Product x, Product y)
{
// Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
// Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
// Check whether the products' properties are equal.
return x.Code == y.Code && x.Name == y.Name;
}
// If Equals() returns true for a pair of objects,
// GetHashCode must return the same value for these objects.
public int GetHashCode(Product product)
{
// Check whether the object is null.
if (Object.ReferenceEquals(product, null)) return 0;
// Get the hash code for the Name field if it is not null.
int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();
// Get the hash code for the Code field.
int hashProductCode = product.Code.GetHashCode();
// Calculate the hash code for the product.
return hashProductName^hashProductCode;
}
}
优秀 - 说得好! – 2009-10-07 19:31:38
多数民众赞成我以为 - 所以我需要做一个方法,暴露这个比较从我的WCF服务公开? – Nate 2009-10-07 19:58:41
我的目标是能够在客户端做到这一点,做我自己的比较:productsA.Except(productsB)(其中两个都是IEnumerable)。 – Nate 2009-10-07 20:00:13