koenbeuk / EntityFrameworkCore.Projectables

Project over properties and functions in your linq queries
MIT License
260 stars 17 forks source link

Protected/private property/method support #76

Open vflame opened 1 year ago

vflame commented 1 year ago

Is supporting the use of protected/private methods/members in a projected expression something that can be added?

koenbeuk commented 1 year ago

Expression trees are generated for projectable methods as part of an external class. A projectable expression is simply a rewrite of the original implementation. If this refers to a private member then that is not accessible from the external class. There are 2 workarounds possible:

  1. We generate expressions as part of a partial class. e.g., when you declare the container class of your projectables as Partial, we can generate the projectable methods within the same class and hence those generated projectable methods will have access to the internals of that class.

  2. We rewrite the generated expression to basically inline the logic of the internal member that it's referring to, e.g.

class Foo {
  [Projectable] private int Sum(int a, int b) => a + b;
  [Projectable] public int SumAll(int[] numbers) => numbers.Aggregate((next, total) => Sum(total, next), 0);
 }

 // Generated code:
class Generated_SumAll {
  // Before
  public Expression<Func<int[], int>> Expression = (int[] numbers) => numbers.Aggregate((next, total) => Foo.Sum(total, next), 0)

  // After
  public Expression<Func<int[], int>> Expression = (int[] numbers) => numbers.Aggregate((next, total) => total +  next, 0);
}

The downside of the second approach is that it would not be able to generate code for projectables that are part of a type that lives in a different assembly as it would not have access to the syntax tree for that method.

Either way, this could be implemented. Happy to accept a PR here.