1

We scaffolded database from Entity Framework to create class models. We have over 1000 classes. Now we're implementing unit tests, to compare classes inserting an Actual class with Expected class. The following website recommends method below to compare all Members.

Do I have to write this for all my 1000+ classes? Or is there a way to use auto code generate in Visual Studio to create all these IEquatable? Maybe with T4?

https://grantwinney.com/how-to-compare-two-objects-testing-for-equality-in-c/

public class Person : IEquatable<Person> 
{
    public string Name { get; set; }
    public int Age { get; set; }

    public bool Equals(Person other)
    {
        if (ReferenceEquals(other, null))
            return false;

        if (ReferenceEquals(this, other))
            return true;

        return Name.Equals(other.Name) && Age.Equals(other.Age);
    }

https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019

Teamwork Guy answer below is nice, need to way to implement this kind of method for 1000+ classes.

  • Do your classes have identity properties like `ID`? My understanding is that identity is a preferable way to check for equality between entities. In other words, they're equal if they have the same IDs. In that case inheritance would work. They could inherit from a base identity and equality would be implemented there. – Scott Hannen Aug 08 '19 at 18:38
  • Likely it will take you longer to write the code gen or t4 template than to do 1000 once so if you don’t plan to regenerate the models it might not be worth. Also your interns probably need something to do anyway. – Vidmantas Blazevicius Aug 08 '19 at 19:53
  • hi @VidmantasBlazevicius well our database architect is changing some tables every week, as client requirements are changing –  Aug 08 '19 at 20:09

2 Answers2

4

If you are only implementing Equals for test assertions, I would use a library like FluentAssertions to do assertions. The various BeEquivalentTo methods compare object graphs and give detailed error messages (e.g. expected Age to be x but was y) which are often better than what you will get out of implementing your own Equals methods. It integrates with all the major test frameworks out of the box (mstest, nunit, xunit.net).

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • ok, I will check this out, and it will compare classes with many members? sounds good ! –  Aug 08 '19 at 17:25
  • AFAIK yes it will work for many members and collections like lists and dictionaries. – Mike Zboray Aug 08 '19 at 17:33
  • ok, I will check, I wanted to Exclude couple columns in Comparing, eg identityid and system getdate audittime, thanks again ! –  Aug 08 '19 at 17:34
  • Ah I see, there are ways to exclude members on a per assertion basis, but there is also some global configuration. It might be worth another question about this library to learn if there is a way to set a global option to exclude certain properties. – Mike Zboray Aug 08 '19 at 17:46
1

Yes, it appears possible in Visual Studio >=2017 or using ReSharper:

https://stackoverflow.com/a/48441971

https://learn.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods?view=vs-2017

This would still be time consuming for 1000+ classes. You might look into Visual Studio's APIs for plugin development to see if you could programmatically trigger this option for a whole list of files.

TeamworkGuy2
  • 159
  • 6