Skip to content

Commit

Permalink
Merge pull request #392 from LuccaSA/fix_hierarchy_problem
Browse files Browse the repository at this point in the history
FIX : issue on order by on property on base class
  • Loading branch information
rducom authored Feb 13, 2020
2 parents 45cc3a5 + e194570 commit d8e6eca
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Rdd.Domain/Helpers/Expressions/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ private PropertyInfo GetPropertyInfo(Type type, string name)
}
else
{
return type.GetProperty(name, BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var property = type.GetProperty(name, BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

//Ef needs the property coming from the declaring type
if (property != null && property.ReflectedType != property.DeclaringType)
{
property = property.DeclaringType.GetProperty(name, BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
}
return property;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/Rdd.Domain.Tests/Models/Hierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public abstract class Hierarchy : IEntityBase<int>

public string Type { get; }

public long Value { get; set; }

public int Id { get; set; }

public Hierarchy Clone() => this;
Expand All @@ -25,6 +27,7 @@ public abstract class Hierarchy : IEntityBase<int>
public class Super : Hierarchy
{
public string SuperProperty { get; set; }
public long Value2 { get; set; }
}

public class SuperSuper : Super
Expand Down
97 changes: 97 additions & 0 deletions test/Rdd.Infra.Tests/HierarchyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Rdd.Domain.Helpers.Expressions;
using Rdd.Domain.Models.Querying;
using Rdd.Domain.Tests.Models;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Xunit;

namespace Rdd.Infra.Tests
{
public class GenericKey
{
public long Sum { get; set; }
public string BaseProperty { get; set; }
}

public class TypeOneKey : GenericKey
{
public string SuperProperty { get; set; }
}

public class Metric<TKey>
where TKey : GenericKey
{
public long Sum { get; set; }
public TKey Key { get; set; }
}

public class TypeOneMetric : Metric<TypeOneKey>
{
public long TypeOneProperty { get; set; }
}

public class HierarchyTests : DatabaseTest
{
[Fact]
public async Task OrderByTest()
{
await RunCodeInsideIsolatedDatabaseAsync((context) =>
{
using (var newContext = GetContext())
{
newContext.Set<Super>().Add(new Super
{
BaseProperty = "a",
Value = 1
});
newContext.Set<Super>().Add(new Super
{
BaseProperty = "a",
Value = 2
});
newContext.Set<Super>().Add(new Super
{
BaseProperty = "b",
Value = 2
});
newContext.SaveChanges();

var query = newContext.Set<Super>().GroupBy(e => new
{
e.BaseProperty,
e.SuperProperty
})
.Select(g => new TypeOneMetric
{
Sum = g.Sum(e => e.Value),
Key = new TypeOneKey
{
Sum = g.Sum(e => e.Value2),
BaseProperty = g.Key.BaseProperty,
SuperProperty = g.Key.SuperProperty
}
});

var set = new Service<TypeOneMetric, TypeOneKey>().ApplyOrderBys(query);

var response = set.ToList();
}

return Task.CompletedTask;
});
}
}

public class Service<TMetric, TKey> where TMetric : Metric<TKey>
where TKey : GenericKey
{
public IQueryable<TMetric> ApplyOrderBys(IQueryable<TMetric> entities)
{
var expression = new ExpressionParser().Parse<TMetric>("Sum");
var orderBy = new OrderBy<TMetric>(expression.ToLambdaExpression());
return orderBy.ApplyOrderBy(entities);
}
}
}

0 comments on commit d8e6eca

Please sign in to comment.