andrewlock / StronglyTypedId

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

Limits #23

Closed dzmitry-lahoda closed 10 months ago

dzmitry-lahoda commented 4 years ago

Allow to limit string to 50 chars and ushort to 14 bits. I want to use this in games.

andrewlock commented 4 years ago

I'm not entirely sure if that's possible in this library. We could not scaffold the constructor, which would allow you to create your own, but if I remember correctly you can't enforce that a non-default constructor is called in structs. A better option is probably to use a factory function to guard creation, and create your own JSON converters to check for constraints I guess

dzmitry-lahoda commented 4 years ago
dzmitry-lahoda commented 4 years ago

another example - id of guest account is 0. non guest is > 0. I want id which is 1+.

andrewlock commented 4 years ago

I understand the desire, it just does not seem feasible currently with structs. Plus I see this library become pretty much obsolete with C#9

The only option I see in the short term is to support class as well as struct, and allow making the constructor private, so that you can write your own public constructor (while still allowing to create from JSON, if they type convert options are enabled)

dzmitry-lahoda commented 4 years ago

pretty much obsolete with C#9 with public data class Person? it is class (GC) with no automatic operators to cast to and from to internal state (for id it is single value). Also StronglyTypedId may generate records in future.

SteveDunn commented 2 years ago

Hi @dzmitry-lahoda - I have something similar to @andrewlock 's library, called Vogen. It's similar, but different insofar as it's more focused on 'Value Objects', in particular validation. It has analyzers that break the build if certain situations arise where validation could be bypassed

https://github.com/SteveDunn/Vogen