andrewlock / StronglyTypedId

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

[Proposal] Add Guid-backed id which disallows Guid.Empty #84

Closed hankovich closed 9 months ago

hankovich commented 1 year ago

We use guid-backed ids a lot, but since they may represent Empty (which it almost never a possible value), we have to do a lot of

if (id == TId.Empty)
{
    throw null!; // well, any stupid handling
}

checks. It looks like adding a new backing type (NotEmptyGuid) will be handy.

Probably we can go even deeper and introduce a new AllowEmpty parameter, because empty strings and zero ints/longs are also usually represent invalid identifiers.

Rain0Ash commented 1 year ago

Not possible. Guid.Empty == default(Guid). StrongId is struct type. So, default(StrongId) where typeof(UNDERLYING) == typeof(Guid) - is also Guid.Empty.

Varveyn commented 10 months ago

Might be covered in the scope of https://github.com/andrewlock/StronglyTypedId/issues/75. Having the default constructor generation disabled, the developer could just define their own constructor that will disallow Guid.Empty (or string.Empty, or 0).

andrewlock commented 9 months ago

As Rain0Ash pointed out, this in nice theory, but doesn't really work for structs, because you can always do new MyId() using the default constructor.

Nevertheless, I'm currently working on a big redesign of the library in this 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. So if you want to do something like throw in the constructor when provided Guid.Empty, you can 🙂