1

I need to have possibility of comparison of Product, AdvancedProduct (and other classes that inherit from class Product) How it is better to realize hierarchical check of fields? For example, i want to check two AdvancedProduct classes, first i check fields the basic class Product then check additional fields of AdvancedProduct and return in some form (???) changes between them (maybe class PChanges???). Whether there is a suitable template? How make all this, but to make rather flexibly for the subsequent use?

        public class Product
    {
        public string ID;
        public string Name;

        public Product(string id, string name)
        {
            this.ID = id;
            this.Name = name;
        }


    }

    public class AdvancedProduct : Product
    {
        public string CurrentVersion;

        public AdvancedProduct(string id, string name, string version)
            : base(id, name) { }
    }

    public class PChanges
    {
        public bool NameChanged = false;
        public bool VersionChanged = false;

        public PChanges() { }

    }

    public class ProductComparer
    {
        PChanges changes = new PChanges();

        public ProductComparer() { }

        public PChanges Compare(AdvancedProduct p1, AdvancedProduct p2)
        {
            if (p1.Name != p2.Name)
                changes.NameChanged = true;
            if (p1.CurrentVersion != p2.CurrentVersion)
                changes.VersionChanged = true;

            return changes;

        }
    }
4

3 回答 3

1

有一个不错的 .NET 库,名为 Compare .NET Objects。它可以用来比较复杂的对象,而无需编写比较代码。它也非常可定制 - 您可以告诉它排除某些属性,包括其他属性等。它可以比较平面对象和对象层次结构。您可以从 CodePlex - http://comparenetobjects.codeplex.com/下载它。

于 2013-05-04T07:22:18.293 回答
0

根据 Uzzy 的回答,看起来它可以扩展以跟踪更改。这是不好的做法,是的,但对于小型应用程序来说应该足够了。例子:

public class ProductComparer : IEqualityComparer<Product>{
    private PChanges change;
    public PChanges Changes{ get { return change; } }

    public bool Equals(Product p1, Product p2){
        PChanges newChange = new PChanges();
        bool equal = true;
        if(p1.Name != p2.Name){
            newChange.NameChange = true;
            equal = false;
        }
        this.change = newChange;
        return equal;
    }
}

编辑:

我误读了可扩展字段比较的要求。如果是这种情况,那么装饰器模式最适合您。假设每个其他 Product 类都应该从 Product 类继承。

public class ProductComparer{
    public virtual void TrackChange(Product p1, Product p2, ref PChange change){
        if(p1.Name != p2.Name){
            change.NameChange = true;
        }
        // other base validation
    }
}

public class AdvancedProductComparer : ProductComparer{
    public AdvancedProductComparer(ProductComparer baseComparer){
        this.baseComparer = baseComparer;
    }
    ProductComparer baseComparer;

    public override void TrackChange(Product p1, Product p2, ref PChange change){
        baseComparer.Compare(p1, p2, ref change);
        if( ((AdvancedProduct)p1).CurrentVersion != ((AdvancedProduct)p2).CurrentVersion){
            change.CurrentVersion = true;
        }
    }
}

public class ProductComparerService{
    public ProductComparerService(ProductComparer comparer){
        this.comparer = comparer;
    }

    ProductComparer comparer;
    private PChanges change;
    public PChanges Changes{ get { return change; } }

    public bool Equals(Product p1, Product p2){
        PChanges newChange = new PChanges();
        comparer.Compare(p1,p2, ref newChange);            

        this.change = newChange;
        return (newChange.CurrentVersion || newChange.NameChange);
    }
 }

用法:

ProductComparer pCompare = new ProductComparer();
AdvancedProductComparer advCompare = new AdvancedProductComparer(pCompare);
ProductComparerService service = new ProductComparerService(advCompare);
if( service.Equals(p1,p2) ){
    PChange change = service.Change;
}
于 2013-05-04T07:38:46.217 回答
0

ProductComparer 最好实现IEqulaityComparer

有关更多详细信息,请参阅给定链接中的示例。

于 2013-05-04T07:18:39.153 回答