Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a static wrapper type (or value object) similar to Guid for strong typing #37

Open
BlackBearFTW opened this issue Jun 27, 2024 · 5 comments

Comments

@BlackBearFTW
Copy link

It would be great if this implementation could be wrapped in a static class similar to Guid so strong typing is allowed inside the domain.

@aradalvand
Copy link
Collaborator

aradalvand commented Jun 27, 2024

This can already be done in userland by declaring your own custom types, or better yet, using things like Vogen (which is a great library).

[ValueObject<int>]
public readonly partial struct SqidsId; // you could also add methods like `Validate`, static factories, etc.

Could you elaborate on what need you specifically have and how it's not already doable by defining your own types?

@BlackBearFTW
Copy link
Author

This can already be done in userland by declaring your own custom types, or better yet, using things like Vogen (which is a great library).

[ValueObject<int>]
public readonly partial struct SqidsId; // you could also add methods like `Validate`, static factories, etc.

Could you elaborate on what need you specifically have and how it's not already doable by defining your own types?

I am familiar with Vogen and was originally trying to use that, but that would require a whole lot of additional code to handle model binding, json conversion and other stuff since your primitive will be an integer, while publicly facing (and in ToString) it should be displayed as string.

@aradalvand
Copy link
Collaborator

I have to ask again: "How is this not already doable by defining your own types"?

@BlackBearFTW
Copy link
Author

Yes of course I can implement it myself, but that is with anything, I could write my own implementation of sqid, I could write my own implementation of value objects etc, the ability for me being able to do it myself isn't the point.

I think this feature would improve the developer experience because they don't have to do it themselves. Besides strongly typed is something that should be strived for anyway, it makes it easier to recognise if a property is just a string or if its supposed to represent a sqid.

@kpietraszko
Copy link

I think it's a good point. For example, a library that's a bit similar: https://github.com/Cysharp/Ulid
provides a type representing the id, which stores the underlying data (raw bytes in their case, but in sqids it would be numbers), along with methods to encode to string.
This also allows them to provide integrations with System.Text.Json etc, see https://github.com/Cysharp/Ulid#integrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants