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

Add support for "normalizing" ValueObjects #80

Closed
kirides opened this issue Feb 11, 2022 · 3 comments · Fixed by #127
Closed

Add support for "normalizing" ValueObjects #80

kirides opened this issue Feb 11, 2022 · 3 comments · Fixed by #127
Labels
enhancement New feature or request

Comments

@kirides
Copy link

kirides commented Feb 11, 2022

Describe the feature

There are cases where " air " == " AIR " but with current code, the caller has to ensure that Value.From(...) is always called with .ToUpperInvariant(), .Trim(), ... or similar to ensure equality and proper ToString representation

Either a Flag, an Attribute or even "magically" understood method to provide the "normalized" Value would be great. Latter would allow us to do whatever we want with the value object on creation.

the generated code would then look somewhat like this

public static Value From(string value)
{
    value = Value.Normalize(value);
    Value instance = new Value(value);

    var validation = Value.Validate(value);
    if (validation != Vogen.Validation.Ok)
    {
        throw new Vogen.ValueObjectValidationException(validation.ErrorMessage);
    }


    return instance;
}

It would also allow for clamping of numeric values for example, where a "larger" value isn't technically a cause for an exception but allowed to be silently "fixed"

@kirides kirides added the enhancement New feature or request label Feb 11, 2022
@kirides kirides changed the title Add ToUpperInvariant/ToLowerInvariant for ValueObjects from string Add support for "normalizing" ValueObjects from string Feb 11, 2022
@kirides kirides changed the title Add support for "normalizing" ValueObjects from string Add support for "normalizing" ValueObjects Feb 11, 2022
@SteveDunn
Copy link
Owner

Good idea! I had a similar concept in a previous implementation of this library; it was a delegate passed that did translation (and as I recall, it was exactly for this kind of this - to sanitize data)

@SteveDunn
Copy link
Owner

@kirides - a new version containing this feature was release today.
https://github.com/SteveDunn/Vogen/blob/main/samples/Vogen.Examples/TypicalScenarios/NormalizationExample.cs

Would be great to see if this is what you wanted.

@kirides
Copy link
Author

kirides commented May 6, 2022

works just like we need ;)

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

Successfully merging a pull request may close this issue.

2 participants