GithubHelp home page GithubHelp logo

Comments (17)

ModernRonin avatar ModernRonin commented on May 14, 2024 4

I know this has been closed, I just wanted to add you can get around the refactoring issue with magical strings by doing something like this:

$"{nameof(PriceList.Revisions)}.{nameof(PriceListRevision.VehicleClassPrices)}.{nameof(VehicleClassPricing.Product)}",

That's what I do. Anyway, even so, an expression based solution would be nicer, ofc.

from eshoponweb.

ardalis avatar ardalis commented on May 14, 2024 3

I like the nameof solution. I've just added it via the PR noted above.

from eshoponweb.

ardalis avatar ardalis commented on May 14, 2024 1

For now this is implemented using string-based includes, to maximize flexibility. The downside is, they're magic strings and could become out of sync with entity renames.

Example usage:

public CustomerOrdersWithItemsSpecification(string buyerId)
{
    _buyerId = buyerId;
    AddInclude(o => o.OrderItems); // lambda syntax works for immediate children
    AddInclude("OrderItems.ItemOrdered"); // string syntax works for any level of depth
}

from eshoponweb.

mrukas avatar mrukas commented on May 14, 2024 1

In case someone still wants to have this feature. I added a pull request that adds the possibility to chain includes with .Include() and .ThenInclude().
c3193b1

from eshoponweb.

trevor-hackett avatar trevor-hackett commented on May 14, 2024

I was wondering the same the thing. I had to change the specifications to use strings instead of expressions to do nested includes. Not entirely ideal because refactoring can easily break the includes.

Sent from my Google Nexus 6P using FastHub

from eshoponweb.

ardalis avatar ardalis commented on May 14, 2024

I'm not sure if there's a supported way to do this (hence why it's not implemented yet). I'm open to suggestions, though. This is somewhat related (TL;DR: you only need ThenInclude for collection navigation properties):
dotnet/efcore#4716

from eshoponweb.

ardalis avatar ardalis commented on May 14, 2024

Related: dotnet/efcore#9523

from eshoponweb.

hanslai avatar hanslai commented on May 14, 2024

@ModernRonin I cannot quite follow with your example, can you use @ardalis 's example with
OrderItems.ItemOrdered and give an example using nameof() Thanks

from eshoponweb.

hanslai avatar hanslai commented on May 14, 2024

@ardalis have you find any other alternative approach to avoid using"magic string" with Specification pattern. Thanks

from eshoponweb.

hanslai avatar hanslai commented on May 14, 2024

Thanks.

from eshoponweb.

fingers10 avatar fingers10 commented on May 14, 2024

For some reason this multiple nesting is not working for me. Can anyone please assist me on where I'm wrong?

Here are my Classes:

Branch:

public class Branch : BaseEntity<Guid>, IAggregateRoot
{
    private Branch()
    {
        // required by EF
    }

    public Guid ClientId { get; set; }
    public Client Client { get; set; }
}

Client:

public class Client : BaseEntity<Guid>, IAggregateRoot
{
    private Client()
    {
        // required by EF
    }

    public Guid BusinessId { get; set; }
    public Business Business { get; set; }
}

Business:

public class Business : BaseEntity<Guid>, IAggregateRoot
{
    private Business()
    {
        // required by EF
    }

    public string Name { get; set; }
}

I'm trying to load Branch with Client and Business. Here is my specification:

public class BranchWithClientSpecification : BaseSpecification<Branch>
{
    public BranchWithClientSpecification() : base(b => b.Active)
    {
        AddInclude(b => b.Client);
        AddInclude($"{nameof(Branch.Client)}.{nameof(Client.Business)}");
    }
}

AddInclude($"{nameof(Branch.Client)}.{nameof(Client.Business)}"); line is not working. I always get null for public Business Business { get; set; } property in Client class. Can anyone please assist on where I'm wrong?

from eshoponweb.

JonathanLoscalzo avatar JonathanLoscalzo commented on May 14, 2024

I think that @mrukas is an interesting solution.

We are trying to use Include & .ThenInclude but also use filtering includes. With strings magic that is not possible.

ref: https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include

from eshoponweb.

JonathanLoscalzo avatar JonathanLoscalzo commented on May 14, 2024

I think that @mrukas is an interesting solution.

We are trying to use Include & .ThenInclude but also use filtering includes. With strings magic that is not possible.

ref: https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include

Actually, I found the Ardalis.Specification package, I think that is the solution, but if we want to build a simpler solution, is not as easy as I expected

from eshoponweb.

ardalis avatar ardalis commented on May 14, 2024

Yes, this is solved already in Ardalis.Specification, if you're able to just consume that package.

from eshoponweb.

JonathanLoscalzo avatar JonathanLoscalzo commented on May 14, 2024

We have a simplistic solution, but we need to use include and then-include with filtering. It is not easy to add without break all

from eshoponweb.

fiseni avatar fiseni commented on May 14, 2024

If you use the latest version of Ardalis.Specification, you'll get chaining capabilities for Include-TheInclude.
The filtered includes, e.g. Include(x => x.Where(something)), are introduced starting with EF Core 5. So you have to use a minimum of that version of EF Core to get that capability.

from eshoponweb.

JonathanLoscalzo avatar JonathanLoscalzo commented on May 14, 2024

@fiseni Yes, we are using EF core 5.
Regarding using Ardalis.Specification, We have just implemented an Specification, so move to another implementation is not a good idea. (Next project will be mandatory use that package)

Anyway, I have implemented some pieces similar to Ardalis.Specification solution, but more simplistic (not as good as Ardalis, of course)

We have an issue with Nested ThenIncludes and IncludeExtension.cs, instead of returing IQueryable, our code returns IIncludableQueryable (for chaining of chaining), also we have changed the generic implementation of that method:

public static IIncludableQueryable<T, TProperty> Include<T, TProperty>(this IQueryable<T> source, IncludeExpressionInfo info)
    {
            _ = info ?? throw new ArgumentNullException(nameof(info));

            var types = new Type[] {
                info.EntityType,
                info.PropertyType
            };

            var methodInfo = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(m => m.Name.Equals("Include")).First();

            var genericMethod = methodInfo.MakeGenericMethod(types);
            var result = genericMethod.Invoke(null, new object[] { source, info.LambdaExpression });
            return (IIncludableQueryable<T, TProperty>)result;
   }

In the end, we have nested .ThenInclude. Thanks all for building these projects.

(Nota: I don't know if Ardalis.Specification allows nested ThenIncludes[?])

from eshoponweb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.