-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from davewalker5/BSR-46-Fix-Failing-Tests
Add expression builder ; Fix failing unit tests
- Loading branch information
Showing
5 changed files
with
150 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace BaseStationReader.Entities.Expressions | ||
{ | ||
public class TrackerFilter | ||
{ | ||
public string PropertyName { get; set; } = ""; | ||
public TrackerFilterOperator Operator { get; set; } | ||
public object? Value { get; set; } = null; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/BaseStationReader.Entities/Expressions/TrackerFilterOperator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace BaseStationReader.Entities.Expressions | ||
{ | ||
public enum TrackerFilterOperator | ||
{ | ||
Equals, | ||
NotEquals, | ||
GreaterThan, | ||
GreaterThanOrEqual, | ||
LessThan, | ||
LessThanOrEqual | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/BaseStationReader.Entities/Interfaces/IExpressionBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using BaseStationReader.Entities.Expressions; | ||
using System.Linq.Expressions; | ||
|
||
namespace BaseStationReader.Entities.Interfaces | ||
{ | ||
public interface IExpressionBuilder<T> where T : class | ||
{ | ||
IList<TrackerFilter> Filters { get; set; } | ||
|
||
void Add(string propertyName, TrackerFilterOperator @operator, object? value); | ||
Expression<Func<T, bool>>? Build(); | ||
void Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using BaseStationReader.Entities.Expressions; | ||
using BaseStationReader.Entities.Interfaces; | ||
using System.Linq.Expressions; | ||
|
||
namespace BaseStationReader.Logic.Database | ||
{ | ||
public class ExpressionBuilder<T> : IExpressionBuilder<T> where T : class | ||
{ | ||
public IList<TrackerFilter> Filters { get; set; } = new List<TrackerFilter>(); | ||
|
||
/// <summary> | ||
/// Clear the current filters | ||
/// </summary> | ||
public void Clear() | ||
{ | ||
Filters.Clear(); | ||
} | ||
|
||
/// <summary> | ||
/// Add a clause to the current filters | ||
/// </summary> | ||
/// <param name="propertyName"></param> | ||
/// <param name="operator"></param> | ||
/// <param name="value"></param> | ||
public void Add(string propertyName, TrackerFilterOperator @operator, object? value) | ||
{ | ||
Filters.Add(new TrackerFilter | ||
{ | ||
PropertyName = propertyName, | ||
Operator = @operator, | ||
Value = value | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Build a lambda expression from the current filters | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public Expression<Func<T, bool>>? Build() | ||
{ | ||
// If there are no filters, return null | ||
if (Filters.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
// Get an expression representing the object type | ||
var parameter = Expression.Parameter(typeof(T), "p"); | ||
|
||
Expression? body = null; | ||
foreach (var filter in Filters) | ||
{ | ||
// Get an expression representing the class member for this filter and a constant representing | ||
// the value | ||
var member = Expression.Property(parameter, filter.PropertyName); | ||
var constant = Expression.Constant(filter.Value); | ||
|
||
// Construct the body of this clause | ||
Expression expression; | ||
switch (filter.Operator) | ||
{ | ||
case TrackerFilterOperator.Equals: | ||
expression = Expression.Equal(member, constant); | ||
break; | ||
case TrackerFilterOperator.NotEquals: | ||
expression = Expression.NotEqual(member, constant); | ||
break; | ||
case TrackerFilterOperator.GreaterThan: | ||
expression = Expression.GreaterThan(member, constant); | ||
break; | ||
case TrackerFilterOperator.GreaterThanOrEqual: | ||
expression = Expression.GreaterThanOrEqual(member, constant); | ||
break; | ||
case TrackerFilterOperator.LessThan: | ||
expression = Expression.LessThan(member, constant); | ||
break; | ||
case TrackerFilterOperator.LessThanOrEqual: | ||
expression = Expression.LessThanOrEqual(member, constant); | ||
break; | ||
default: | ||
throw new NotImplementedException(); | ||
} | ||
|
||
// Add this clause to the body of the overall expression | ||
body = (body == null) ? expression : Expression.AndAlso(body, expression); | ||
} | ||
|
||
// Construct and return the lambda expression | ||
return Expression.Lambda<Func<T, bool>>(body!, parameter); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters