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

Proposal: Add a TreatAsString field to QueryAttribute #1943

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mark-pro
Copy link

The proposal arises from the need to pass custom types or structs which might represent types with rules and conversions. Most of these types can override ToString() which can then be called when constructing query parameters.

For example:

record readonly struct ZipCode(ushort ZipCode) {
    public override string ToString() =>
        ZipCode.ToString();
}

To solve the problem the proposal seeks to add a field to the QueryAttribute called TreatAsString.

The new field is used when the query string is created and simply calls the ToString() function on a type.

There is a workaround that is not documented. The current workaround is to implement IFormattable on the type, which feels redundant if ToString() is already implemented on a custom type.

@mark-pro
Copy link
Author

I am open to suggestions on how to implement this functionality. In many of my project, both professional and personal, I find myself adding IFormattable to those types which feels a little clunky when I really need that complex type to be a string.

The solution could also work on types not owned by the implementer such as passing a GUID as a query parameter.

GetUser([Query(TreatAsString = true)] Guid userId).

The proposal arises from the need to pass custom types or structs which might represent types with rules and conversions.

For example:

```csharp
record readonly struct ZipCode(ushort ZipCode) {
    public override string ToString() =>
        ZipCode.ToString();
}
```

To solve the problem the proposal seeks to add a field to the QueryAttribute called `TreatAsString`.

The new field is used when the query string is created and simply calls the `ToString()` function on a type.

There is a workaround that is not documented. The current workaround is to implement `IFormattable` on the type, which feels redundant if `ToString()` is already implemented on a custom type.
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

Successfully merging this pull request may close these issues.

1 participant