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

Support translating value objects that are using a converter #31994

Closed
adampaquette opened this issue Oct 8, 2023 · 1 comment
Closed

Support translating value objects that are using a converter #31994

adampaquette opened this issue Oct 8, 2023 · 1 comment

Comments

@adampaquette
Copy link

Context

We are using immutable value objects that wraps primitives with validations in our entities.

A value object (VO) could be represented as the following:

public record CustomerName
{
    public string Value { get; }

    public CustomerName(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            throw new ValidationException("CustomerName can't be empty.");
        }

        value = Normalize(value);

        if (value.Length > 100)
        {
            throw new ValidationException("CustomerName exceeds the maximum length of 100 characters.");
        }

        Value = value;
    }

    public static CustomerName From(string value)
        => new(value);

    public string Normalize(string value)
        => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value).Trim();
}

99% of our VO are Domain Primitives, they only contain a single property and we don't want to treat them as complex objects, so we use converters in the configuration:

cb.Property(c => c.Name).HasConversion(name => name.Value, s => CustomerName.From(s));

Actual use

To make a search query translate to Sql, we need to cast the VO as a string because the underlying property Value does not exist in the database and sometimes we have to cast as object before casting as string.

db.Customers.Where(c => ((string)c.FirstName).Contains(firstName.Value)));

Expected use

We would like to be able to access the underlying property in the query as valid C# code and still generate the correct Sql.

db.Customers.Where(c => c.FirstName.Value.Contains(firstName.Value)));

I've made a test case here: adampaquette#1
I would need guidance on how to achieve this.
Thank you

@roji
Copy link
Member

roji commented Oct 8, 2023

Duplicate of #10434

@roji roji marked this as a duplicate of #10434 Oct 8, 2023
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants