-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Nested includes in ISpecification #40
Comments
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 |
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): |
Related: dotnet/efcore#9523 |
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:
|
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:
That's what I do. Anyway, even so, an expression based solution would be nicer, ofc. |
@ModernRonin I cannot quite follow with your example, can you use @ardalis 's example with |
@ardalis have you find any other alternative approach to avoid using"magic string" with Specification pattern. Thanks |
I like the |
Thanks. |
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(). |
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 public class BranchWithClientSpecification : BaseSpecification<Branch>
{
public BranchWithClientSpecification() : base(b => b.Active)
{
AddInclude(b => b.Client);
AddInclude($"{nameof(Branch.Client)}.{nameof(Client.Business)}");
}
}
|
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 |
Yes, this is solved already in Ardalis.Specification, if you're able to just consume that package. |
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 |
If you use the latest version of |
@fiseni Yes, we are using Anyway, I have implemented some pieces similar to We have an issue with Nested ThenIncludes and IncludeExtension.cs, instead of returing 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
|
Hi, I have been looking at ISpecification and the way the includes are added. Could something similiar be implemented for nested includes, that later are translated to ThenInclude in IQueryable?
The text was updated successfully, but these errors were encountered: