Skip to content

Add OnlyOnInclude option for the root rewritter #91

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions samples/BasicSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class User
public string FullName { get; set; }
private string _FullName => FirstName + " " + LastName;

[Projectable(UseMemberBody = nameof(_TotalSpent))]
[Projectable(UseMemberBody = nameof(_TotalSpent), OnlyOnInclude = true)]
public double TotalSpent { get; set; }
private double _TotalSpent => Orders.Sum(x => x.PriceSum);

Expand Down Expand Up @@ -154,10 +154,11 @@ public static void Main(string[] args)
}

{
var result = dbContext.Users.FirstOrDefault();
Console.WriteLine($"Unloaded total: {dbContext.Users.First().TotalSpent}");
var result = dbContext.Users.Include(x => x.TotalSpent).FirstOrDefault();
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");

result = dbContext.Users.FirstOrDefault(x => x.TotalSpent > 1);
result = dbContext.Users.Include(x => x.TotalSpent).FirstOrDefault(x => x.TotalSpent > 1);
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");

var spent = dbContext.Users.Sum(x => x.TotalSpent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Projectables
{
Expand All @@ -23,5 +19,12 @@ public sealed class ProjectableAttribute : Attribute
/// or null to get it from the current member.
/// </summary>
public string? UseMemberBody { get; set; }

/// <summary>
/// <c>true</c> will allow you to request for this property by
/// explicitly calling .Include(x => x.Property) on the query,
/// <c>false</c> will always consider this query to be included.
/// </summary>
public bool OnlyOnInclude { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq.Expressions;
using System.Reflection;
using EntityFrameworkCore.Projectables.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query;

Expand All @@ -16,6 +17,7 @@ public sealed class ProjectableExpressionReplacer : ExpressionVisitor
readonly ExpressionArgumentReplacer _expressionArgumentReplacer = new();
readonly Dictionary<MemberInfo, LambdaExpression?> _projectableMemberCache = new();
private bool _disableRootRewrite;
private List<string> _includedProjections = new();
private IEntityType? _entityType;

private readonly MethodInfo _select;
Expand Down Expand Up @@ -60,6 +62,7 @@ bool TryGetReflectedExpression(MemberInfo memberInfo, [NotNullWhen(true)] out La
public Expression? Replace(Expression? node)
{
_disableRootRewrite = false;
_includedProjections.Clear();
var ret = Visit(node);

if (_disableRootRewrite)
Expand Down Expand Up @@ -138,6 +141,28 @@ bool TryGetReflectedExpression(MemberInfo memberInfo, [NotNullWhen(true)] out La

protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.Name == nameof(EntityFrameworkQueryableExtensions.Include))
{
var include = node.Arguments[1] switch {
ConstantExpression { Value: string str } => str,
LambdaExpression { Body: MemberExpression member } => member.Member.Name,
UnaryExpression { Operand: LambdaExpression { Body: MemberExpression member } } => member.Member.Name,
_ => null
};
// Only rewrite the include if it includes a projectable property (or if we don't know what's happening).
var ret = Visit(node.Arguments[0]);
// The visit here is needed because we need the _entityType defined on the query root for the condition below.
if (
include != null
&& _entityType?.ClrType
?.GetProperty(include)
?.GetCustomAttribute<ProjectableAttribute>() != null)
{
_includedProjections.Add(include);
return ret;
}
}

// Replace MethodGroup arguments with their reflected expressions.
// Note that MethodCallExpression.Update returns the original Expression if argument values have not changed.
node = node.Update(node.Object, node.Arguments.Select(arg => arg switch {
Expand Down Expand Up @@ -212,13 +237,13 @@ PropertyInfo property when nodeExpression is not null
var updatedBody = _expressionArgumentReplacer.Visit(reflectedExpression.Body);
_expressionArgumentReplacer.ParameterArgumentMapping.Clear();

return base.Visit(
return Visit(
updatedBody
);
}
else
{
return base.Visit(
return Visit(
reflectedExpression.Body
);
}
Expand All @@ -243,7 +268,14 @@ protected override Expression VisitExtension(Expression node)
private Expression _AddProjectableSelect(Expression node, IEntityType entityType)
{
var projectableProperties = entityType.ClrType.GetProperties()
.Where(x => x.IsDefined(typeof(ProjectableAttribute), false))
.Where(x => {
var attr = x.GetCustomAttribute<ProjectableAttribute>();
if (attr == null)
return false;
if (attr.OnlyOnInclude)
return _includedProjections.Contains(x.Name);
return true;
})
.Where(x => x.CanWrite)
.ToList();

Expand Down Expand Up @@ -291,7 +323,7 @@ private Expression _GetAccessor(PropertyInfo property, ParameterExpression para)
_expressionArgumentReplacer.ParameterArgumentMapping.Add(lambda.Parameters[0], para);
var updatedBody = _expressionArgumentReplacer.Visit(lambda.Body);
_expressionArgumentReplacer.ParameterArgumentMapping.Clear();
return base.Visit(updatedBody);
return Visit(updatedBody);
}
}
}