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 ThenInclude #247

Closed
dsoltesz opened this issue Dec 9, 2021 · 8 comments
Closed

support ThenInclude #247

dsoltesz opened this issue Dec 9, 2021 · 8 comments

Comments

@dsoltesz
Copy link

dsoltesz commented Dec 9, 2021

Is your feature request related to a problem? Please describe.
BaseSpecification only supports Include

Describe the solution you'd like
Should support Include.ThenInclude

@akema-trebla
Copy link
Contributor

Actually "ThenInclude" is supported if you add the child to the expression using the Includes or IncludeStrings.

My issue though is with the use of magic strings especially when including the children of a collection navigation property.

I'll submit a PR to use typed entities as opposed to magic strings.

@dsoltesz
Copy link
Author

Can you provide an example?

@akema-trebla
Copy link
Contributor

akema-trebla commented Dec 10, 2021

var spec = new BaseSpecification<Product>();
spec.Includes.Add(a => a.Brand);
var product = await _repository.GetByIdAsync<Product, ProductDetailsDto>(id, spec);

Suppose Brand has a Manufacturer and Manufacturer has an Address, you could include them by chaining them

spec.Includes.Add(a => a.Brand.Manufacturer.Address);

Or you could use the IncludeStrings property on the BaseSpecification

spec.IncludeStrings.Add("Brand.Manufacturer.Address");

But like I said I'm doing some refactoring and I'll submit a PR soon.

@dsoltesz
Copy link
Author

dsoltesz commented Dec 10, 2021 via email

@dsoltesz
Copy link
Author

dsoltesz commented Dec 13, 2021

that works for single entity but what if brand had a collection of Manufacturers... you need to be able to say Brand.Manufacturers.ThenInclude(man => man.Address)

in EF 6 you can do this spec.Includes.Add(o => o.EpisodeConsumerAdvices.Select(e => e.ConsumerAdvice)); but in EF core you need to use ThenInclude

@akema-trebla
Copy link
Contributor

akema-trebla commented Dec 13, 2021

Yes, I dunno which version of the API you're using but before I submitted #250, you could achieve this using magic strings.

Something like
spec.IncludeStrings.Add("Brand.Manufacturers.ConsumerAdvice")

Personally, I'm not a fan of magic strings. So I wanted the repository code to use typed entities as much as possible and especially with Includes.

Now because of the use of repository pattern we can't have a dependency of EFCore in the Application project to make use of IIncludableQueryable to leverage ThenInclude directly. So I tried to bring back the EF 6 approach where you could still include collection navigation properties and their children using Select method.

So from #250 onwards you can include children of collection navigation properties using the Select method like you have above.

@dsoltesz
Copy link
Author

Using the select approach wasn't working for me in ef core..that's how I ended up learning about the thenInclude

@akema-trebla
Copy link
Contributor

Using the select approach wasn't working for me in ef core..that's how I ended up learning about the thenInclude

Yes, Select no longer works in EFCore out of the box - You need to use ThenInclude.

But because I wanted to use typed entities and couldn't use the dependency on EFCore, I had to adopt the EF 6 approach in this project.

Hope you get me

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

No branches or pull requests

3 participants