<< When it comes to entities, so far I haven't really have a use for overriding Equals().>>
If you're using detached instances/multiple sessions, you should override equals (and GetHashCode) on your entities (entity X/db row X is two different instances in two different sessions). Still talking about entities, if you override == or not I guess is a matter of taste. I prefer not doing it. To me that operator means "same instance" on ref types. -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Oskar Berggren Sent: den 1 september 2009 11:17 To: [email protected] Subject: [nhusers] Re: override == for persistable objects If you are creating a value object, you should absolutely also override operator==(). Your example code however may cause null pointer exceptions. I ususally do it the other way round... Let Equals() call operator==() which is then something like this: public static bool operator ==(MyClass left, MyClass right) { // Same instance or both null. if (object.ReferenceEquals(left, right)) return true; // Either, but not both, is null. if (((object)left == null) || ((object)right == null)) return false; // Insert MyClass specific logic here. } And don't forget IEquatable<>. When it comes to entities, so far I haven't really have a use for overriding Equals(). /Oskar 2009/9/1 reflection <[email protected]>: > > Hello everybody, > > Overwriting Equals on persistable Objects should be standard. I just > had a thought to also use Equals if a User compares to instances with > ==, which normally does something like ReferenceEquals(Object obj). > Has anybody ever tried that? Or is this a no go cause you overwrite > standard .NET behaviour? I'm just unsure if I should do that or not. > > public static bool operator ==(A thisA, A otherA) > { > return thisA.Equals(otherA); > } > > Thanks for your help =) > > Greetings, > Reflection > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
