views:

41

answers:

2

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?

+4  A: 

Have you seen this: SequenceEqual

http://msdn.microsoft.com/en-us/library/bb348567.aspx

Daniel A. White
+1, I did not know about that
ram
I had never seen it before. I will read carefully. Thanks!
Will Marcouiller
Linq is awesome.
Daniel A. White
@Ram: Then perhaps my question has been useful, if it was just to learn about this SequenceEqual() method? ;P I'm kidding!
Will Marcouiller
@Will: OK Will, +1 for your question too !!
ram
@Ram: Thanks for your upvote! This is appreciated, really! Hope I can help you out someday onto one of your future questions! Have a nice day! =)
Will Marcouiller
+2  A: 

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?

No Refunds No Returns
Honestly? Ignorance! =P I have yet never ever used the IComparable interface. I guess I shall take an eye out to it. =)
Will Marcouiller
`IComparable` is used internally by string and numeric types for sorting.
Daniel A. White
Thanks Daniel A. White for this information. I like to learn such information on how it works under the hood.
Will Marcouiller
If you implement `IComparable`, your objects in a collection can easily be sorted, too.
Daniel A. White
This offers a great advantage then! Thanks Daniel!
Will Marcouiller