andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.52k stars 79 forks source link

Exception when compare default value with StronglyTypedIdBackingType.String #32

Closed nhuthan closed 2 years ago

nhuthan commented 3 years ago

Example code

[StronglyTypedId(backingType: StronglyTypedIdBackingType.String)]
public partial struct UserId{ }

public class User{
    public UserId Id {get; set;}
}

...
var user = new User();
if(user.Id == UserId.Empty) --> throw exception (using Value.CompareTo when Value is null)
andrewlock commented 3 years ago

Yeah, that's an inherent problem with using a reference type in the struct. The question is, how would you expect this to behave? We could throw if you try to create a UserId with a null string. We could use ?. in the CompareTo. What are your thoughts?

nhuthan commented 3 years ago

@andrewlock I think use ?. is good idea. It will working well for EF.

nhuthan commented 3 years ago

I created a pull request here https://github.com/andrewlock/StronglyTypedId/pull/33

andrewlock commented 3 years ago

I've released a beta version of a major update to the library converts to using Source generators. There's quite a few breaking changes in the release, so please make sure to check the release notes for how to update, but I'd appreciate any feedback you have before I do a final release! It fixes this issue too.

I've also written a blog post announcing the update that goes into a bit more details: https://andrewlock.net/rebuilding-stongly-typed-id-as-a-source-generator-1-0-0-beta-release

Thanks! 🙂

andrewlock commented 2 years ago

I believe this issue is solved in the 1.0.0 beta release, so closing this for now. Feel free to re-open if it doesn't work for you