andrewlock / StronglyTypedId

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

[Proposal] Add Parse and TryParse methods for Guid/long/int-backed ids #30

Closed hankovich closed 10 months ago

hankovich commented 3 years ago

Hi, thanks for the handy library. I think it would be rather nice to have Parse method to do

UserId.Parse("some guid")

instead of

new UserId(Guid.Parse("some guid"))

And I'd like to also see TryParse to make my parsing safer.

arex388 commented 3 years ago

I just tried out 1.0.0-beta1 and thought the same thing. It would help a lot not to have to write new WhateverId(Guid.Parse("???")) and just do WhateverId.Parse("???"). For now you can do something along the lines of, so at least it's in one place:

[StronglyTypedId]
public partial struct WhateverId {
    public static WhateverId Parse(
        string value) => new(Guid.Parse(value));
}
lucasteles commented 2 years ago

This wold help to use the Id in aspnet minimal api route parameter

lucasteles commented 1 year ago

I opened a PR with an implementation proposal for this https://github.com/andrewlock/StronglyTypedId/pull/85

andrewlock commented 10 months ago

I'm very late to this, but I've included it in the 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.