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

Compiler picks the wrong overload of Translate #13

Closed
jmathew opened this issue Oct 18, 2017 · 2 comments
Closed

Compiler picks the wrong overload of Translate #13

jmathew opened this issue Oct 18, 2017 · 2 comments

Comments

@jmathew
Copy link
Contributor

jmathew commented Oct 18, 2017

Related to https://stackoverflow.com/q/46819033/730326

Steps to Reproduce

Given a model like so:

public class DataModel {
    public List<Person> Persons { get; set; }
}

public class Person {
    public Guid Id { get; set; }
    public List<Pet> Pets {get; set;}

    public List<Pet> GetLivingPets() {
       // Do some computation to get the pets that are alive
    }
}

public class Pet {
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool Alive { get; set; }
}

public PetProjection {
    public Guid? Id { get; set; }
    public string Name { get; set; } 
}


public PersonProjection {
    public Guid? Id { get; set; }
    public List<PetProjection> LivingPets {get; set;}
}

And a mapping like so:

popcornConfig
.Map<Person, PersonProjection>(config: (personConfig) =>
{

     personConfig.Translate(fp => fp.LivingPets, f => f.GetLivingPets());

})
.MapEntityFramework<Pet, PetProjection, DataModel>(dbContextOptionsBuilder);

Expected Behavior

No error. Compiles and provides expansion for the Pets. (ie persons?include=[Name, LivingPets[Name]] returns only the pets names not the whole object)

Actual Behavior

On the .Translate line.

'Dictionary<string, object>' does not contain a definition for 'GetLivingPets' and no extension method 'GetLivingPets' accepting a first argument of type 'Dictionary<string, object>' could be found (are you missing a using directive or an assembly reference?)   

Work around

If I change the lambda signature to personConfig.Translate(fp => fp.LivingPets, (f, dict) => f.GetLivingPets()); it works just fine.

Alternatively, if I rename GetLivingPets() to LivingPets() I can remove the translation and it all works.

@undiwahn
Copy link

I think the fix for this will unfortunately be to rebrand a subset of the 'Translate' overloads to 'TranslateWithContext' or otherwise differentiating the different versions by name. We're relying on the compiler to be smarter than it is possible to be in this situation, and realistically not a lot is gained by simply overloading the same method so many times.

@Eightgate
Copy link

Claimed

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

4 participants