C#比较课堂领域的最佳方式

时间:2013-05-04 07:14:52

标签: c#

我需要有可能比较Product,AdvancedProduct(以及继承自Product类的其他类)     如何更好地实现字段的分层检查?例如,我想检查两个AdvancedProduct类,首先我检查字段基本类Product然后检查AdvancedProduct的其他字段并以某种形式返回(???)它们之间的变化(可能是类PChanges ???)。是否有合适的模板?     如何做到这一点,但为了后续的使用而灵活地制作?

        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;

        }
    }

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

ProductComparer实现IEqulaityComparer

时更好

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

答案 2 :(得分: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;
    }
}

编辑:

我误读了可扩展字段比较的要求。如果是这种情况,那么Decorator模式对您来说是最好的。假设每个其他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;
}