StronglyTypedId icon indicating copy to clipboard operation
StronglyTypedId copied to clipboard

Add the Parse and TryParse static methods

Open lucasteles opened this issue 2 years ago • 7 comments

Add the .Parse and .TryParse static methods on all base templates.

They are common and expected methods for value types.

.TryParse is especially good because enables the use of custom types on ASP.NET Minimal Api routes

fix https://github.com/andrewlock/StronglyTypedId/issues/30

lucasteles avatar Oct 21 '22 00:10 lucasteles

Looks great. Are you sure that Parse and TryParse for string-backed and nullable-string-backed ids should trim the passed string?

hankovich avatar Oct 22 '22 09:10 hankovich

@hankovich no I don't 😅,

It looked correct to me at first, usually it's undesired spaces at edges of the strings

Wdyt?

lucasteles avatar Oct 22 '22 20:10 lucasteles

@lucasteles I think Parse/TryParse should not do any additional manipulations, they should act as a shorthand for new TId(TBackedType.Parse(input))

If trimming will be added, it makes sense to trim in constructors, not inside parsing methods. And I'm not sure everyone needs they input to be trimmed

hankovich avatar Oct 22 '22 23:10 hankovich

Makes sense to me, I will update the the PR

lucasteles avatar Oct 22 '22 23:10 lucasteles

@hankovich updated

lucasteles avatar Oct 24 '22 13:10 lucasteles

Thank you, @lucasteles ! I thought I must be using this library wrong since I have to manually add the static .TryParse to each of my Ids to get minimal API routing to work properly.

@andrewlock , any chance of getting this or something similar merged into the release?

gunnbr avatar Oct 12 '23 00:10 gunnbr

Thank you, @lucasteles ! I thought I must be using this library wrong since I have to manually add the static .TryParse to each of my Ids to get minimal API routing to work properly.

I implemented it in my fork

https://github.com/lucasteles/Strongly

lucasteles avatar Oct 13 '23 03:10 lucasteles