Odonno / ReduxSimple

Simple Stupid Redux Store using Reactive Extensions
http://www.nuget.org/packages/ReduxSimple/
MIT License
143 stars 19 forks source link

Is there a simple way to test Equality on POCO object? #21

Closed Odonno closed 6 years ago

Odonno commented 6 years ago

I added some unit tests in order to test the DistinctUntilChanged observable but one thing I notice is that if we observe the object directly, the distinct is made on the reference and not the properties so even if the state have unchanged property values, the observable considers the object as new and not distinct.

I solved it by overriding the Equals method. https://github.com/Odonno/ReduxSimple/blob/0a3167636e3383a4ce7fda9d628ea0740cc2886f/ReduxSimple.UnitTests/Models.cs#L27

But it feels odd to override the method since the object can be used in other part of dev apps. So, my questions are :

  1. Is there an easy way to test POCO equality? Like an external library or with a simple function?
  2. How can we apply it in the DistinctUntilChanged method?

Maybe @mhusainisurge have an idea?

Odonno commented 6 years ago

At least, there is this topic https://stackoverflow.com/questions/1652897/how-do-i-compare-the-fields-properties-between-pocos

But I hope there is a library for it.

mhusainisurge commented 6 years ago

Ah, good catch! I'm not aware of a solution to this that covers all cases and is performant enough to be used across the board. Having said that, I'll try to do some research when I get a chance.

Odonno commented 6 years ago

If nothing has ever been done before. Let's just take the solution from stackoverflow and see if it fits our needs.

Odonno commented 6 years ago

@mhusainisurge I made a PR for this. I was inspired by the stackoverflow solution and made some changes to fit our needs. But the solution looks pretty simple : use reflexion to get properties of the State and compare them each one against the other to find a difference.