Skip to content

The mapping expression to retrieve a translation property should be simplified #1

@raymondbrink

Description

@raymondbrink

Right now the expression to retrieve a translation property looks like this:

public static Expression<Func<StoreProduct, string>> ProductName
{
    get
    {
        var cultureName = Thread.CurrentThread.CurrentCulture.Name;
        var languageName = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

        return s => s.Product.Translations.FirstOrDefault(e => matchesCulture(cultureName).Invoke(e)).Name ??
                    s.Product.Translations.FirstOrDefault(e => matchesCulture(languageName).Invoke(e)).Name ??
                    s.Product.Translations.FirstOrDefault().Name;
    }
}

Right now we need to specify the property to retrieve (.Name) three times in this expression.

The end goal would be to split this expression into two expressions:

  1. One generic expression to determine the correct translation to use
  2. Another expression to retrieve te property from the single translation found with 1.

I'm thinking of something along the lines of this:

public static Expression<Func<Product, object>> FromTranslation
{
    get
    {
        var cultureName = Thread.CurrentThread.CurrentCulture.Name;
        var languageName = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

        Expression<Func<Product, ProductTranslation>> translationExpression = s =>
            s.Translations.FirstOrDefault(t => t.Culture == cultureName) ??
            s.Translations.FirstOrDefault(t => t.Culture == languageName) ??
            s.Translations.FirstOrDefault();

        Expression<Func<ProductTranslation, object>> memberExpression = t => t.Name;

        // TODO: Combine expressions somehow?
        throw new NotImplementedException();
    }
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions