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

Specification pattern with Include and ThenInclude #9523

Closed
ardalis opened this issue Aug 22, 2017 · 6 comments
Closed

Specification pattern with Include and ThenInclude #9523

ardalis opened this issue Aug 22, 2017 · 6 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ardalis
Copy link

ardalis commented Aug 22, 2017

I'm using the Specification pattern with EF Core, and am specifying what I want to include as part of the specification (along with query criteria).

See here:
https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/Infrastructure/Data/EfRepository.cs

Sample specification:
https://github.com/dotnet-architecture/eShopOnWeb/blob/master/src/ApplicationCore/Specifications/BasketWithItemsSpecification.cs

This all works great for .Include(), but I cannot figure out how to support .ThenInclude behavior within a Specification. Can you tell me if you think this is possible (and point me in the right direction), and if not, can you let me know if there are any plans to update EF Core's API to support this (i.e. perhaps supporting a different style of expression for .Include()?).

Thanks!

@ajcvickers
Copy link
Member

@ardalis Have you tried using the string-based overloads of Include? When using strings, you don't need to have ThenInclude calls because you can instead use a concatenated string of properties. For example:

context.Products
    .Include("NavA.NavB")
    .ToList();

is the same as:

context.Products
    .Include(e => e.NavA)
    .ThenInclude(e => e.NavB)
    .ToList();

@ardalis
Copy link
Author

ardalis commented Aug 25, 2017

That works, but has the usual disadvantages of magic strings (e.g. not picked up by automatic refactoring tools). Was hoping for something better, but this can be a workaround/stopgap approach.

@ajcvickers
Copy link
Member

@ardalis I wasn't actually trying to suggest that you use strings in your repository API. Instead, I was thinking you could implement whatever lambda-based (or otherwise) API that makes sense for your patterns, not constrained in any way by ThenInclude or the other parts of the EF API. Once you have your API to your liking, then it should be fairly easy to generate the navigation property names from it and pass those to the Include string API without having to mess with EF's lambdas, etc.

@ardalis
Copy link
Author

ardalis commented Aug 25, 2017

Yep, thanks. I'll give that a shot. More work on my end, but shouldn't be awful.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Aug 25, 2017
@Shrembo
Copy link

Shrembo commented May 11, 2022

@ardalis
Copy link
Author

ardalis commented May 12, 2022

We got it working in Ardalis.Specification a while ago, too. Thanks!
https://ardalis.github.io/Specification/features/orm-features.html

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants