Skip to content

Commit

Permalink
Update to v3.0.5 direct select Ids type because Expression Constant c…
Browse files Browse the repository at this point in the history
…annot set List<object> as specific type
  • Loading branch information
quyvu01 committed Dec 30, 2024
1 parent ba357e7 commit 169666f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
32 changes: 14 additions & 18 deletions src/OfX.EntityFrameworkCore/EfQueryOfHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using OfX.Abstractions;
using OfX.ApplicationModels;
using OfX.Attributes;
using OfX.Cached;
using OfX.EntityFrameworkCore.Delegates;
using OfX.Exceptions;
using OfX.Responses;

namespace OfX.EntityFrameworkCore;
Expand Down Expand Up @@ -46,18 +45,25 @@ public async Task<ItemsResponse<OfXDataResponse>> GetDataAsync(RequestContext<TA
// May I should cache the containsMethod, idAsString first!
private Expression<Func<TModel, bool>> BuildFilter(RequestOf<TAttribute> query)
{
var modelIdData = GetModelData();
var selectorsConstant = Expression.Constant(query.SelectorIds);
var containsCall = Expression.Call(selectorsConstant, OfXCached.IdsContainsMethodLazy.Value,
modelIdData.MethodCallExpression);
return Expression.Lambda<Func<TModel, bool>>(containsCall, modelIdData.ParameterExpression);
var parameter = Expression.Parameter(typeof(TModel), "x");
var idProperty = Expression.Property(parameter, idPropertyName);
var idType = idProperty.Type;
var containsMethod = typeof(List<>).MakeGenericType(idType).GetMethod("Contains");
var selectorsConstant = Helpers.GeneralHelpers.ConstantExpression(query.SelectorIds, idType);
var containsCall = Expression.Call(selectorsConstant, containsMethod!, idProperty);
return Expression.Lambda<Func<TModel, bool>>(containsCall, parameter);
}

private Expression<Func<TModel, OfXDataResponse>> BuildResponse(RequestOf<TAttribute> request)
{
return ExpressionMapModelStorage.Value.GetOrAdd(request.Expression, expression =>
{
var (parameter, idAsString) = GetModelData();
var parameter = Expression.Parameter(typeof(TModel), "x");
// Access the Id property on the model
var idProperty = Expression.Property(parameter, idPropertyName);
var toStringMethod = typeof(object).GetMethod(nameof(ToString), Type.EmptyTypes);
var idAsString = Expression.Call(idProperty, toStringMethod!);

var expressionParts = expression.Split('.');
Expression currentExpression = parameter;
var currentType = typeof(TModel);
Expand Down Expand Up @@ -123,14 +129,4 @@ private Expression<Func<TModel, OfXDataResponse>> BuildResponse(RequestOf<TAttri
return Expression.Lambda<Func<TModel, OfXDataResponse>>(newExpression, parameter);
});
}

private ModelIdData GetModelData() => OfXCached.ModelIdDataCachedLazy
.Value.GetOrAdd(typeof(TModel), modelType =>
{
var parameter = Expression.Parameter(modelType, "x");
var idProperty = Expression.Property(parameter, idPropertyName);
var toStringMethod = idProperty.Type.GetMethod(nameof(ToString), Type.EmptyTypes);
var idAsString = Expression.Call(idProperty, toStringMethod!);
return new ModelIdData(parameter, idAsString);
});
}
2 changes: 1 addition & 1 deletion src/OfX.EntityFrameworkCore/OfX.EntityFrameworkCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<LangVersion>default</LangVersion>
<Version>3.0.4</Version>
<Version>3.0.5</Version>
<Authors>Quy Vu</Authors>
<PackageId>OfX-EFCore</PackageId>
<Description>OfX extension. Use EntityFramework as Data Querying</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/OfX.Grpc/OfX.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<LangVersion>default</LangVersion>
<Version>3.0.3</Version>
<Version>3.0.5</Version>
<Authors>Quy Vu</Authors>
<PackageId>OfX-gRPC</PackageId>
<Description>OfX extension. Use gRPC as Data transporting</Description>
Expand Down
5 changes: 0 additions & 5 deletions src/OfX/Cached/OfXCached.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public static class OfXCached

private static readonly Lazy<ConcurrentDictionary<Type, Func<object[], object>>> ConstructorCache = new(() => []);

public static readonly Lazy<MethodInfo> IdsContainsMethodLazy =
new(() => typeof(List<string>).GetMethod("Contains", [typeof(string)]));

public static readonly Lazy<ConcurrentDictionary<Type, ModelIdData>> ModelIdDataCachedLazy = new(() => []);

public static object CreateInstanceWithCache(Type type, params object[] args)
{
if (ConstructorCache.Value.TryGetValue(type, out var factory)) return factory(args);
Expand Down
3 changes: 3 additions & 0 deletions src/OfX/Exceptions/OfXException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ public sealed class RequestMustNotBeAddMoreThanOneTimes()

public sealed class AttributesFromNamespaceShouldBeAdded() :
Exception("Attributes from namespaces should be added!");

public sealed class CurrentIdTypeWasNotSupported() :
Exception("Current Id type was not supported. Please create a join us to contribute more!");
}
28 changes: 28 additions & 0 deletions src/OfX/Helpers/GeneralHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Linq.Expressions;
using OfX.Exceptions;

namespace OfX.Helpers;

public static class GeneralHelpers
{
public static string GetAssemblyName(this Type type) => $"{type.FullName},{type.Assembly.GetName().Name}";

public static ConstantExpression ConstantExpression(List<string> selectorIds, Type idType)
{
if (idType == typeof(string)) return Expression.Constant(selectorIds);
if (idType == typeof(Guid))
return Expression.Constant(selectorIds
.Where(a => Guid.TryParse(a, out _)).Select(Guid.Parse).ToList());
if (idType == typeof(int))
return Expression.Constant(selectorIds
.Where(a => int.TryParse(a, out _)).Select(int.Parse).ToList());
if (idType == typeof(long))
return Expression.Constant(selectorIds
.Where(a => long.TryParse(a, out _)).Select(long.Parse).ToList());
if (idType == typeof(short))
return Expression.Constant(selectorIds
.Where(a => short.TryParse(a, out _)).Select(short.Parse).ToList());

throw new OfXException.CurrentIdTypeWasNotSupported();
}
}
6 changes: 0 additions & 6 deletions src/OfX/Helpers/Helpers.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/OfX/OfX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<LangVersion>default</LangVersion>
<Version>3.0.3</Version>
<Version>3.0.5</Version>
<Authors>Quy Vu</Authors>
<PackageId>OfX</PackageId>
<Description>The high performance and easiest way to play with microservices for .NET</Description>
Expand Down

0 comments on commit 169666f

Please sign in to comment.