Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated code doesn't implement some comparison operators #56

Closed
bbrysh opened this issue Feb 28, 2022 · 10 comments · Fixed by #117
Closed

Generated code doesn't implement some comparison operators #56

bbrysh opened this issue Feb 28, 2022 · 10 comments · Fixed by #117

Comments

@bbrysh
Copy link

bbrysh commented Feb 28, 2022

The id structs generated don't implement some comparison operators (<,>, <=, or >=.) The CLR cannot automatically call the CompareTo implementation from Equals(object) or from the base comparison operator implementations.

@andrewlock
Copy link
Owner

Thanks, I specifically didn't add those previously, as I didn't want them. For IDs, those comparisons don't make any sense. It seems reasonable to make optionally add them though I think

@bbrysh
Copy link
Author

bbrysh commented Feb 28, 2022

The optional route sounds good to me. This would support a scenario like someone is searching for an integer id using a binary search of a sorted list. It also helps me get rid of some SonarQube warnings :)

@SteveDunn
Copy link
Contributor

@bbrysh - you could also implement IComparable yourself, as the classes are partial.

@bbrysh
Copy link
Author

bbrysh commented Mar 8, 2022

I could, but to me that defeats the purpose of using the library - not writing boilerplate code. Also, it's a good idea to treat IComparable like IEqualable. If you implement the IEqualable interface, you should override the equality operators. For IComparable, you should override the comparison operators. The templates already override the equality operators, so a precedent has been set.

@SteveDunn
Copy link
Contributor

SteveDunn commented Mar 8, 2022 via email

@bbrysh
Copy link
Author

bbrysh commented Mar 8, 2022

I don't believe that implicit operators would be needed as the Value property is the underlying typed value. I think having the overloaded comparison operators is a good choice as nobody knows all the cases where this library might be used. Since IComparable is an optional part of the ids generated, when the interface is generated, the operators could also be generated.

@MovGP0
Copy link

MovGP0 commented Jun 13, 2022

For IDs, those comparisons don't make any sense.

I disagree. Ids often need be sorted for some reason. Especially with ids of type byte, int, or long.

@timbze
Copy link

timbze commented Nov 10, 2022

== and != is not mentioned in OP, but that's what I mostly need for ID. I'm doing it manually for now but sure would be great to have the option to generate those!

@cyril265
Copy link

I'd like to add that > and < operators for ids are needed if you want to implement cursor based pagination. Your query would be something like that: db.Entity.Where(e => e.Id > previousId).OrderBy(e => e.Id)

@andrewlock
Copy link
Owner

I'm slow to get to this, but the fundamental point of "if you implement IComparable you should implement the operators" is sound.

I've done a redesign of the library in this PR, and added the operators in that PR:

The main idea is to make the library much more maintainable while also giving people a mechanism to customise the generated IDs as much as they like. Which will make it easy to make changes like this in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants