Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="IvAt.CommonFramework" Version="2.1.11" />
<PackageVersion Include="IvAt.CommonFramework.DependencyInjection" Version="2.1.11" />
<PackageVersion Include="IvAt.CommonFramework" Version="2.1.13" />
<PackageVersion Include="IvAt.CommonFramework.DependencyInjection" Version="2.1.13" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.2" />
Expand Down
66 changes: 13 additions & 53 deletions src/GenericQueryable.EntityFramework/EfFetchService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Linq.Expressions;
using System.Reflection;

using CommonFramework;
using CommonFramework.ExpressionEvaluate;

using GenericQueryable.Fetching;

using Microsoft.EntityFrameworkCore;
Expand All @@ -12,67 +11,28 @@

namespace GenericQueryable.EntityFramework;

public class EfFetchService([FromKeyedServices(RootFetchRuleExpander.Key)] IFetchRuleExpander fetchRuleExpander) : IFetchService
public class EfFetchService([FromKeyedServices(RootFetchRuleExpander.Key)] IFetchRuleExpander fetchRuleExpander) : FetchService(fetchRuleExpander)
{
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, object>> rootCache = [];

public virtual IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, FetchRule<TSource> fetchRule)
public override IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, FetchRule<TSource> fetchRule)
where TSource : class
{
return fetchRule switch
if (fetchRule is UntypedFetchRule<TSource> untypedFetchRule)
{
UntypedFetchRule<TSource> untypedFetchRule => source.Include(untypedFetchRule.Path),

_ => this.GetApplyFetchFunc(fetchRule).Invoke(source)
};
}

private Func<IQueryable<TSource>, IQueryable<TSource>> GetApplyFetchFunc<TSource>(FetchRule<TSource> fetchRule)
where TSource : class
{
return this.rootCache
.GetOrAdd(typeof(TSource), _ => new ConcurrentDictionary<Type, object>())
.GetOrAdd(fetchRule.GetType(), _ => new ConcurrentDictionary<FetchRule<TSource>, Func<IQueryable<TSource>, IQueryable<TSource>>>())
.Pipe(v => (ConcurrentDictionary<FetchRule<TSource>, Func<IQueryable<TSource>, IQueryable<TSource>>>)v)
.GetOrAdd(fetchRule, _ =>
{
var fetchExpr = this.GetApplyFetchExpression(fetchRuleExpander.Expand(fetchRule));

return fetchExpr.Compile();
});
}

private Expression<Func<IQueryable<TSource>, IQueryable<TSource>>> GetApplyFetchExpression<TSource>(PropertyFetchRule<TSource> fetchRule)
where TSource : class
{
var startState = ExpressionHelper.GetIdentity<IQueryable<TSource>>();

return fetchRule.Paths.Aggregate(startState, (state, path) =>
return source.Include(untypedFetchRule.Path);
}
else
{
var nextApplyFunc = GetApplyFetchExpression<TSource>(path);

return ExpressionEvaluateHelper.InlineEvaluate<Func<IQueryable<TSource>, IQueryable<TSource>>>(ee =>

q => ee.Evaluate(nextApplyFunc, ee.Evaluate(state, q)));
});
return base.ApplyFetch(source, fetchRule);
}
}

private static Expression<Func<IQueryable<TSource>, IQueryable<TSource>>> GetApplyFetchExpression<TSource>(LambdaExpressionPath fetchPath)
protected override IEnumerable<MethodInfo> GetFetchMethods<TSource>(LambdaExpressionPath fetchPath)
where TSource : class
{
LambdaExpression startState = ExpressionHelper.GetIdentity<IQueryable<TSource>>();

var resultBody = fetchPath
return fetchPath
.Properties
.ZipStrong(new LambdaExpression?[] { null }.Concat(fetchPath.Properties.SkipLast(1)), (prop, prevProp) => new { prop, prevProp })
.Aggregate(startState.Body, (state, pair) =>
{
var fetchMethod = GetFetchMethod<TSource>(pair.prop, pair.prevProp);

return Expression.Call(fetchMethod, state, pair.prop);
});

return Expression.Lambda<Func<IQueryable<TSource>, IQueryable<TSource>>>(resultBody, startState.Parameters);
.Select(pair => GetFetchMethod<TSource>(pair.prop, pair.prevProp));
}

private static MethodInfo GetFetchMethod<TSource>(LambdaExpression prop, LambdaExpression? prevProp)
Expand Down
63 changes: 63 additions & 0 deletions src/GenericQueryable.Runtime/Fetching/FetchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;

using CommonFramework;
using CommonFramework.ExpressionEvaluate;

using Microsoft.Extensions.DependencyInjection;

namespace GenericQueryable.Fetching;

public abstract class FetchService([FromKeyedServices(RootFetchRuleExpander.Key)] IFetchRuleExpander fetchRuleExpander) : IFetchService
{
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, object>> rootCache = [];

public virtual IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, FetchRule<TSource> fetchRule)
where TSource : class => this.GetApplyFetchFunc(fetchRule).Invoke(source);

private Func<IQueryable<TSource>, IQueryable<TSource>> GetApplyFetchFunc<TSource>(FetchRule<TSource> fetchRule)
where TSource : class
{
return this.rootCache
.GetOrAdd(typeof(TSource), _ => new ConcurrentDictionary<Type, object>())
.GetOrAdd(fetchRule.GetType(), _ => new ConcurrentDictionary<FetchRule<TSource>, Func<IQueryable<TSource>, IQueryable<TSource>>>())
.Pipe(v => (ConcurrentDictionary<FetchRule<TSource>, Func<IQueryable<TSource>, IQueryable<TSource>>>)v)
.GetOrAdd(fetchRule, _ =>
{
var fetchExpr = this.GetApplyFetchExpression(fetchRuleExpander.Expand(fetchRule));

return fetchExpr.Compile();
});
}

private Expression<Func<IQueryable<TSource>, IQueryable<TSource>>> GetApplyFetchExpression<TSource>(PropertyFetchRule<TSource> fetchRule)
where TSource : class
{
var startState = ExpressionHelper.GetIdentity<IQueryable<TSource>>();

return fetchRule.Paths.Aggregate(startState, (state, path) =>
{
var nextApplyFunc = this.GetApplyFetchExpression<TSource>(path);

return ExpressionEvaluateHelper.InlineEvaluate<Func<IQueryable<TSource>, IQueryable<TSource>>>(ee =>

q => ee.Evaluate(nextApplyFunc, ee.Evaluate(state, q)));
});
}

private Expression<Func<IQueryable<TSource>, IQueryable<TSource>>> GetApplyFetchExpression<TSource>(LambdaExpressionPath fetchPath)
where TSource : class
{
LambdaExpression startState = ExpressionHelper.GetIdentity<IQueryable<TSource>>();

var resultBody = this
.GetFetchMethods<TSource>(fetchPath).ZipStrong(fetchPath.Properties, (method, prop) => new { method, prop })
.Aggregate(startState.Body, (state, pair) => Expression.Call(pair.method, state, pair.prop));

return Expression.Lambda<Func<IQueryable<TSource>, IQueryable<TSource>>>(resultBody, startState.Parameters);
}

protected abstract IEnumerable<MethodInfo> GetFetchMethods<TSource>(LambdaExpressionPath fetchPath)
where TSource : class;
}
29 changes: 29 additions & 0 deletions src/GenericQueryable.Runtime/Fetching/UntypedFetchExpander.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Concurrent;

using CommonFramework;

namespace GenericQueryable.Fetching;

public class UntypedFetchExpander : IFetchRuleExpander
{
private readonly ConcurrentDictionary<Type, object> cache = new();

public PropertyFetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule)
{
if (fetchRule is UntypedFetchRule<TSource> untypedFetchRule)
{
return this.cache.GetOrAdd(typeof(TSource), _ => new ConcurrentDictionary<UntypedFetchRule<TSource>, PropertyFetchRule<TSource>>())
.Pipe(innerCache => (ConcurrentDictionary<UntypedFetchRule<TSource>, PropertyFetchRule<TSource>>)innerCache)
.Pipe(innerCache => innerCache.GetOrAdd(
untypedFetchRule,
_ =>
{
var fetchPath = LambdaExpressionPath.Create(typeof(TSource), untypedFetchRule.Path.Split('.'));

return new PropertyFetchRule<TSource>([fetchPath]);
}));
}

return null;
}
}
4 changes: 4 additions & 0 deletions src/GenericQueryable.Runtime/GenericQueryable.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<RootNamespace>GenericQueryable</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GenericQueryable.Abstractions\GenericQueryable.Abstractions.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void Initialize(IServiceCollection services)
services.TryAddSingleton<IMethodRedirector, MethodRedirector>();

services.AddSingleton<IFetchRuleExpander, FetchRuleHeaderExpander>();
services.AddSingleton<IFetchRuleExpander, UntypedFetchExpander>();

services.AddKeyedSingleton<IFetchRuleExpander, RootFetchRuleExpander>(RootFetchRuleExpander.Key);
}

Expand Down
2 changes: 1 addition & 1 deletion src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[assembly: AssemblyProduct("GenericQueryable")]
[assembly: AssemblyCompany("IvAt")]

[assembly: AssemblyVersion("2.1.11.0")]
[assembly: AssemblyVersion("2.1.13.0")]
[assembly: AssemblyInformationalVersion("changes at build")]

#if DEBUG
Expand Down
Loading