Skip to content

Commit 65e1943

Browse files
author
Jay Oursler
committed
Fix for Serialization and Deserialization issues.
1 parent ee14829 commit 65e1943

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

MicroRuleEngine.Core.Tests/ExampleUsage.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
66
using MicroRuleEngine.Core.Tests.Models;
7+
using Newtonsoft.Json;
78

89
namespace MicroRuleEngine.Tests
910
{
@@ -392,5 +393,46 @@ public void EnumerableAggregationOnChild()
392393

393394
Assert.IsTrue(passes);
394395
}
396+
397+
[TestMethod]
398+
public void SerializeThenDeserialize()
399+
{
400+
401+
402+
403+
Order order = GetOrder();
404+
405+
var orderParent = new OrderParent() {PlacedOrder = order};
406+
407+
408+
Rule rule = new Rule
409+
{
410+
MemberName = "PlacedOrder.Items",
411+
EnumerableValueExpression = new Selector
412+
{
413+
Operator = "Count"
414+
},
415+
Operator = "GreaterThan",
416+
TargetValue = 3
417+
};
418+
419+
var jsonString = JsonConvert.SerializeObject(rule);
420+
421+
var deserializedRule = JsonConvert.DeserializeObject<Rule>(jsonString);
422+
423+
424+
425+
MRE engine = new MRE();
426+
var compiledRule = engine.CompileRule<OrderParent>(deserializedRule);
427+
bool passes = compiledRule(orderParent);
428+
Assert.IsFalse(passes);
429+
430+
order.Items.Add(new Item());
431+
order.Items.Add(new Item());
432+
433+
passes = compiledRule(orderParent);
434+
435+
Assert.IsTrue(passes);
436+
}
395437
}
396438
}

MicroRuleEngine/MRE.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,22 @@ private static Expression BuildExpr(Type type, Rule rule, Expression param, bool
328328
2,
329329
property.PropertyType);
330330

331+
332+
331333
if(generationMethod == null)
332334
{
333335
generationMethod = GetLinqMethod(rule.EnumerableValueExpression.Operator, rule.TargetValue.GetType());
334336
}
337+
if(generationMethod == null)
338+
{
339+
generationMethod = GetLinqMethod(rule.EnumerableValueExpression.Operator);
340+
}
341+
342+
if (generationMethod == null)
343+
{
344+
throw new
345+
RulesException($"Unable to find Linq method for {rule.EnumerableValueExpression.Operator}");
346+
}
335347

336348
var m = generationMethod.MakeGenericMethod(elementType);
337349

@@ -492,6 +504,12 @@ private static MethodInfo GetLinqMethod(string name, Type returnType)
492504
.FirstOrDefault((m => m.Name == name && m.ReturnType == returnType));
493505
}
494506

507+
private static MethodInfo GetLinqMethod(string name)
508+
{
509+
return typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public)
510+
.FirstOrDefault((m => m.Name == name));
511+
}
512+
495513

496514

497515
private static Expression GetDataRowField(Expression prm, string member, string typeName)

0 commit comments

Comments
 (0)