iQuarc/DataLocalization

Support for many to one relationships

Closed this issue · 3 comments

span commented

When looking at the tests a Product only seems to have a single category. I am trying to set it up to have a list of categories like this:

public class Product
{
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Category> Categories { get; set; }
        public List<ProductTranslation> ProductTranslations { get; set; }
}

public class Category
{
        [Key]
        public int Id { get; set; }
        public int ProductId { get; set; }
        public string Name { get; set; }
        public List<CategoryTranslation> CategoryTranslations {get; set; }
}

I then try to select all product with their categories like this:

var cul = new System.Globalization.CultureInfo("sv-SE");
var products = context.Products
                .Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    Categories = context.Categories
                        .Where(c => c.ProductId == p.Id)
                        .Select(c => new
                        {
                            Id = c.Id,
                            ProductId = p.id,
                            Name = c.Name
                        })
                        .Localize(cul)
                        .ToList()
                })
                .Localize(cul)
                .First();

The snippet above returns a list of translated products but the category name property simply shows up as my default value in the Category.Name field and not the translated name. If I change the nested select statement for the category to use more explicit fetching like this snippet below:

                Categories = context.Categories 
                        .Where(c => c.ProductId == p.Id)
                        .Select(c => new
                        {
                            Id = c.Id,
                            ProductId = p.id,
                            Name = s.CategoryTranslations.Where(st => st.Language.IsoCode == "sv").Select(ct => ct.Name)
                        })
                        .Localize(cul)
                        .ToList()

Then I get translated products and translated categories.

Does this library support localization of lists like this?

span commented

Attaching sample project using Course and Skill instead of Product and Category.

iQuarc.DataLocalization.Poc.zip

Hi @span

I've pushed a new package with a fix. There was a bug, nested projections were not being visited. You can update to iQuarc.DataLocalization 1.0.4 to make your example project work.

As a note:
image

The inner Localize is redundant, only the outer one is needed.

If there are still more scenarios you think the library should support please feel free to open a new ticket. Pull requests are welcome as well.

Thank you for your support,
Cata

span commented

Wow, amazing.

Thank you for the pointer about the redundant Localize call as well. I was using in directly on the db context and with the latest release it gave me the following error:

System.InvalidOperationException: 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.'

When removing the inner Localize it works as a charm though 👍