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
23 changes: 12 additions & 11 deletions src/GenericQueryable.EntityFramework/EfFetchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.DependencyInjection;

namespace GenericQueryable.EntityFramework;

public class EfFetchService(IEnumerable<IFetchRuleExpander> expanders) : IFetchService
public class EfFetchService([FromKeyedServices(RootFetchRuleExpander.Key)] IFetchRuleExpander fetchRuleExpander) : IFetchService
{
public virtual IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, FetchRule<TSource> fetchRule)
where TSource : class
public virtual IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, FetchRule<TSource> fetchRule)
where TSource : class
{
var expandedFetchRule = expanders.Aggregate(fetchRule, (state, expander) => expander.TryExpand(state) ?? state);
var expandedFetchRule = fetchRuleExpander.TryExpand(fetchRule) ?? fetchRule;

return expandedFetchRule switch
{
UntypedFetchRule<TSource> untypedFetchRule => source.Include(untypedFetchRule.Path),
{
UntypedFetchRule<TSource> untypedFetchRule => source.Include(untypedFetchRule.Path),

PropertyFetchRule<TSource> propertyFetchRule => this.ApplyFetch(source, propertyFetchRule),
PropertyFetchRule<TSource> propertyFetchRule => this.ApplyFetch(source, propertyFetchRule),

_ => throw new ArgumentOutOfRangeException(nameof(fetchRule))
};
}
_ => throw new ArgumentOutOfRangeException(nameof(fetchRule))
};
}

protected IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, PropertyFetchRule<TSource> fetchRule)
protected IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, PropertyFetchRule<TSource> fetchRule)
where TSource : class
{
return fetchRule.Paths.Aggregate(source, this.ApplyFetch);
Expand All @@ -45,7 +46,7 @@
private IQueryable<TSource> ApplyFetch<TSource>(IQueryable<TSource> source, LambdaExpression prop, LambdaExpression? prevProp)
where TSource : class
{
return this.GetFetchMethod<TSource>(prop, prevProp).Invoke<IQueryable<TSource>>(this, source, prop);

Check warning on line 49 in src/GenericQueryable.EntityFramework/EfFetchService.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LambdaExpression' cannot be used for parameter 'args' of type 'object?[]' in 'IQueryable<TSource> extension(MethodInfo).Invoke<IQueryable<TSource>>(object? source, object? arg1, params object?[] args)' due to differences in the nullability of reference types.

Check warning on line 49 in src/GenericQueryable.EntityFramework/EfFetchService.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LambdaExpression' cannot be used for parameter 'args' of type 'object?[]' in 'IQueryable<TSource> extension(MethodInfo).Invoke<IQueryable<TSource>>(object? source, object? arg1, params object?[] args)' due to differences in the nullability of reference types.

Check warning on line 49 in src/GenericQueryable.EntityFramework/EfFetchService.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LambdaExpression' cannot be used for parameter 'args' of type 'object?[]' in 'IQueryable<TSource> extension(MethodInfo).Invoke<IQueryable<TSource>>(object? source, object? arg1, params object?[] args)' due to differences in the nullability of reference types.

Check warning on line 49 in src/GenericQueryable.EntityFramework/EfFetchService.cs

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LambdaExpression' cannot be used for parameter 'args' of type 'object?[]' in 'IQueryable<TSource> extension(MethodInfo).Invoke<IQueryable<TSource>>(object? source, object? arg1, params object?[] args)' due to differences in the nullability of reference types.
}

private MethodInfo GetFetchMethod<TSource>(LambdaExpression prop, LambdaExpression? prevProp)
Expand Down
2 changes: 1 addition & 1 deletion src/GenericQueryable.IntegrationTests/AppFetchRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace GenericQueryable.IntegrationTests;

public static class AppFetchRule
{
public static FetchRule<TestObject> TestFetchRule { get; } = new FetchRuleHeader<TestObject>(nameof(TestFetchRule));
public static FetchRuleHeader<TestObject> TestFetchRule { get; } = new FetchRuleHeader<TestObject, string>(nameof(TestFetchRule));
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public void Initialize(IServiceCollection services)
services.AddSingleton(typeof(IFetchService), this.fetchServiceType);
services.AddSingleton(typeof(ITargetMethodExtractor), this.targetMethodExtractorType);

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

foreach (var fetchRuleExpanderType in this.fetchRuleExpanderTypeList)
{
services.AddSingleton(typeof(IFetchRuleExpander), fetchRuleExpanderType);
Expand All @@ -42,7 +44,7 @@ public IGenericQueryableSetup SetFetchService<TFetchService>()
return this;
}

public IGenericQueryableSetup AddFetchRule<TSource>(FetchRule<TSource> header, FetchRule<TSource> implementation)
public IGenericQueryableSetup AddFetchRule<TSource>(FetchRuleHeader<TSource> header, PropertyFetchRule<TSource> implementation)
{
this.fetchRuleHeaderInfoList.Add(new FetchRuleHeaderInfo<TSource>(header, implementation));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ IGenericQueryableSetup SetFetchService<TFetchService>()
IGenericQueryableSetup AddFetchRuleExpander<TFetchRuleExpander>()
where TFetchRuleExpander : IFetchRuleExpander;

IGenericQueryableSetup AddFetchRule<TSource>(FetchRule<TSource> header, FetchRule<TSource> implementation);
IGenericQueryableSetup AddFetchRule<TSource>(FetchRuleHeader<TSource> header, PropertyFetchRule<TSource> implementation);

IGenericQueryableSetup SetTargetMethodExtractor<TTargetMethodExtractor>()
where TTargetMethodExtractor : ITargetMethodExtractor;
Expand Down
4 changes: 3 additions & 1 deletion src/GenericQueryable/Fetching/FetchRuleHeader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace GenericQueryable.Fetching;

public record FetchRuleHeader<TSource>(string Name) : FetchRule<TSource>;
public abstract record FetchRuleHeader<TSource> : FetchRule<TSource>;

public record FetchRuleHeader<TSource, TValue>(TValue Value) : FetchRuleHeader<TSource>;
25 changes: 16 additions & 9 deletions src/GenericQueryable/Fetching/FetchRuleHeaderExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ public class FetchRuleHeaderExpander(IEnumerable<FetchRuleHeaderInfo> fetchRuleH

private readonly ConcurrentDictionary<Type, object> cache = new();

public FetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule)
public PropertyFetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule)
{
return cache.GetOrAdd(typeof(TSource),
_ => headersDict
.GetValueOrDefault(typeof(TSource))
.EmptyIfNull()
.Cast<FetchRuleHeaderInfo<TSource>>()
.ToDictionary(info => info.Header, info => info.Implementation))
if (fetchRule is FetchRuleHeader<TSource> fetchRuleHeader)
{
return cache.GetOrAdd(typeof(TSource),
_ => headersDict
.GetValueOrDefault(typeof(TSource))
.EmptyIfNull()
.Cast<FetchRuleHeaderInfo<TSource>>()
.ToDictionary(info => info.Header, info => info.Implementation))

.Pipe(innerCache => (IReadOnlyDictionary<FetchRule<TSource>, FetchRule<TSource>>)innerCache)
.Pipe(innerCache => (IReadOnlyDictionary<FetchRuleHeader<TSource>, PropertyFetchRule<TSource>>)innerCache)

.Pipe(innerCache => innerCache.GetValueOrDefault(fetchRule));
.Pipe(innerCache => innerCache.GetValueOrDefault(fetchRuleHeader));
}
else
{
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/GenericQueryable/Fetching/FetchRuleHeaderInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public abstract record FetchRuleHeaderInfo
public abstract Type SourceType { get; }
}

public record FetchRuleHeaderInfo<TSource>(FetchRule<TSource> Header, FetchRule<TSource> Implementation) : FetchRuleHeaderInfo
public record FetchRuleHeaderInfo<TSource>(FetchRuleHeader<TSource> Header, PropertyFetchRule<TSource> Implementation) : FetchRuleHeaderInfo
{
public override Type SourceType { get; } = typeof(TSource);
}
2 changes: 1 addition & 1 deletion src/GenericQueryable/Fetching/IFetchRuleExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IFetchRuleExpander
{
FetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule);
PropertyFetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule);
}
40 changes: 40 additions & 0 deletions src/GenericQueryable/Fetching/RootFetchRuleExpander.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Concurrent;

using CommonFramework;

namespace GenericQueryable.Fetching;

public class RootFetchRuleExpander(IEnumerable<IFetchRuleExpander> expanders) : IFetchRuleExpander
{
public const string Key = "Root";

private readonly ConcurrentDictionary<Type, object> cache = new();

public PropertyFetchRule<TSource>? TryExpand<TSource>(FetchRule<TSource> fetchRule)
{
if (fetchRule is PropertyFetchRule<TSource> propertyFetchRule)
{
return propertyFetchRule;
}
else
{
return cache
.GetOrAdd(typeof(TSource), _ => new ConcurrentDictionary<FetchRule<TSource>, PropertyFetchRule<TSource>?>())
.Pipe(innerCache => (ConcurrentDictionary<FetchRule<TSource>, PropertyFetchRule<TSource>?>)innerCache)
.GetOrAdd(fetchRule, _ =>
{
var request =

from expander in expanders

let expandedFetchRule = expander.TryExpand(fetchRule)

where expandedFetchRule != null

select expandedFetchRule;

return request.FirstOrDefault();
});
}
}
}
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.0.5.0")]
[assembly: AssemblyVersion("2.0.6.0")]
[assembly: AssemblyInformationalVersion("changes at build")]

#if DEBUG
Expand Down
Loading