Skip to content

Commit d85f397

Browse files
authored
Set the Condition parameterless constructor obsolete (#146)
* refactor: Modify calls to use the new Condition ctor * refactor: Turn Condition parameterless obsolete * refactor: Renamed method in condition builders that accept a condition to Condition()
1 parent cc5559b commit d85f397

27 files changed

Lines changed: 225 additions & 580 deletions

File tree

samples/Rules.Framework.InMemory.Sample/Engine/RulesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task<T> MatchOneAsync<T>(
2222
IDictionary<ConditionTypes, object> conditions)
2323
{
2424
var rulesConditions = (conditions is null) ? new Condition<ConditionTypes>[] { } :
25-
conditions.Select(x => new Condition<ConditionTypes> { Type = x.Key, Value = x.Value })
25+
conditions.Select(x => new Condition<ConditionTypes>(x.Key, x.Value))
2626
.ToArray();
2727

2828
var rulesEngine = await

src/Rules.Framework/Builder/FluentComposedConditionNodeBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ public IConditionNode<TConditionType> Build()
3131
return new ComposedConditionNode<TConditionType>(this.logicalOperator, this.conditions);
3232
}
3333

34+
public IFluentComposedConditionNodeBuilder<TConditionType> Condition(IConditionNode<TConditionType> conditionNode)
35+
{
36+
this.conditions.Add(conditionNode);
37+
38+
return this;
39+
}
40+
3441
public IFluentComposedConditionNodeBuilder<TConditionType> Or(
35-
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc)
42+
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc)
3643
{
3744
var composedConditionNode = ConditionNodeFactory.CreateComposedNode(LogicalOperators.Or, conditionFunc);
3845

@@ -49,12 +56,5 @@ public IFluentComposedConditionNodeBuilder<TConditionType> Value<TDataType>(TCon
4956

5057
return this;
5158
}
52-
53-
public IFluentComposedConditionNodeBuilder<TConditionType> Value(IConditionNode<TConditionType> valueConditionNode)
54-
{
55-
this.conditions.Add(valueConditionNode);
56-
57-
return this;
58-
}
5959
}
6060
}

src/Rules.Framework/Builder/IFluentComposedConditionNodeBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Rules.Framework.Builder
1010
public interface IFluentComposedConditionNodeBuilder<TConditionType>
1111
{
1212
/// <summary>
13-
/// Adds a composed And condition to the fluent condition node builder.
13+
/// Adds a And composed condition to the fluent condition node builder.
1414
/// </summary>
1515
/// <param name="conditionFunc">The function containing the logic for the new condition.</param>
1616
/// <returns></returns>
@@ -24,26 +24,26 @@ IFluentComposedConditionNodeBuilder<TConditionType> And(
2424
IConditionNode<TConditionType> Build();
2525

2626
/// <summary>
27-
/// Adds a composed Or condition to the fluent condition node builder.
27+
/// Adds a Condition to the fluent condition node builder.
28+
/// </summary>
29+
/// <param name="conditionNode">The condition node.</param>
30+
IFluentComposedConditionNodeBuilder<TConditionType> Condition(IConditionNode<TConditionType> conditionNode);
31+
32+
/// <summary>
33+
/// Adds a Or composed condition to the fluent condition node builder.
2834
/// </summary>
2935
/// <param name="conditionFunc">The function containing the logic for the new condition.</param>
3036
/// <returns></returns>
3137
IFluentComposedConditionNodeBuilder<TConditionType> Or(
3238
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc);
3339

3440
/// <summary>
35-
/// Adds a value condition to the fluent condition node builder.
41+
/// Adds a Value condition to the fluent condition node builder.
3642
/// </summary>
3743
/// <param name="conditionType">The condition type.</param>
3844
/// <param name="condOperator">The condition operator.</param>
3945
/// <param name="operand">The condition operand.</param>
4046
/// <returns></returns>
4147
IFluentComposedConditionNodeBuilder<TConditionType> Value<TDataType>(TConditionType conditionType, Operators condOperator, TDataType operand);
42-
43-
/// <summary>
44-
/// Adds a value condition to the fluent condition node builder.
45-
/// </summary>
46-
/// <param name="valueConditionNode">The value condition node.</param>
47-
IFluentComposedConditionNodeBuilder<TConditionType> Value(IConditionNode<TConditionType> valueConditionNode);
4848
}
4949
}

src/Rules.Framework/Builder/IRootConditionNodeBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@ namespace Rules.Framework.Builder
1010
public interface IRootConditionNodeBuilder<TConditionType>
1111
{
1212
/// <summary>
13-
/// Sets a root And condition for the condition node builder.
13+
/// Sets a And composed condition to the root condition node builder.
1414
/// </summary>
1515
/// <param name="conditionFunc">The function containing the logic for the root condition.</param>
1616
/// <returns></returns>
1717
IConditionNode<TConditionType> And(
1818
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc);
1919

2020
/// <summary>
21-
/// Sets a root Or condition for the condition node builder.
21+
/// Sets a Condition to the root condition node builder.
22+
/// </summary>
23+
/// <param name="conditionNode">The condition node.</param>
24+
IConditionNode<TConditionType> Condition(IConditionNode<TConditionType> conditionNode);
25+
26+
/// <summary>
27+
/// Sets a Or composed condition to the root condition node builder.
2228
/// </summary>
2329
/// <param name="conditionFunc">The function containing the logic for the root condition.</param>
2430
/// <returns></returns>
2531
IConditionNode<TConditionType> Or(
2632
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc);
2733

2834
/// <summary>
29-
/// Sets a value condition for the root condition node builder.
35+
/// Sets a Value condition to the root condition node builder.
3036
/// </summary>
3137
/// <param name="conditionType">The condition type.</param>
3238
/// <param name="condOperator">The condition operator.</param>
3339
/// <param name="operand">The condition operand.</param>
3440
/// <returns></returns>
3541
IConditionNode<TConditionType> Value<TDataType>(TConditionType conditionType, Operators condOperator, TDataType operand);
36-
37-
/// <summary>
38-
/// Sets a value condition to the fluent condition node builder.
39-
/// </summary>
40-
/// <param name="valueConditionNode">The value condition node.</param>
41-
IConditionNode<TConditionType> Value(IConditionNode<TConditionType> valueConditionNode);
4242
}
4343
}

src/Rules.Framework/Builder/RootConditionNodeBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ public IConditionNode<TConditionType> And(
1111
return ConditionNodeFactory.CreateComposedNode(LogicalOperators.And, conditionFunc);
1212
}
1313

14+
public IConditionNode<TConditionType> Condition(IConditionNode<TConditionType> conditionNode)
15+
{
16+
return conditionNode;
17+
}
18+
1419
public IConditionNode<TConditionType> Or(
15-
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc)
20+
Func<IFluentComposedConditionNodeBuilder<TConditionType>, IFluentComposedConditionNodeBuilder<TConditionType>> conditionFunc)
1621
{
1722
return ConditionNodeFactory.CreateComposedNode(LogicalOperators.Or, conditionFunc);
1823
}
@@ -22,10 +27,5 @@ public IConditionNode<TConditionType> Value<TDataType>(
2227
{
2328
return ConditionNodeFactory.CreateValueNode(conditionType, condOperator, operand);
2429
}
25-
26-
public IConditionNode<TConditionType> Value(IConditionNode<TConditionType> valueConditionNode)
27-
{
28-
return valueConditionNode;
29-
}
3030
}
3131
}

src/Rules.Framework/Condition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace Rules.Framework
22
{
3+
using System;
4+
35
/// <summary>
46
/// Defines a condition to filter rules.
57
/// </summary>
@@ -22,6 +24,7 @@ public Condition(TConditionType Type, object Value)
2224
/// <summary>
2325
/// Creates a Condition.
2426
/// </summary>
27+
[Obsolete("Please use the constructor with parameters instead.")]
2528
public Condition()
2629
{
2730
}

src/Rules.Framework/Extensions/GenericSearchArgsExtensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ public static SearchArgs<TContentType, TConditionType> ToSearchArgs<TContentType
2121
return new SearchArgs<TContentType, TConditionType>(contentType, genericSearchArgs.DateBegin, genericSearchArgs.DateEnd, genericSearchArgs.Active.Value)
2222
{
2323
Conditions = genericSearchArgs.Conditions.Select(condition => new Condition<TConditionType>
24-
{
25-
Value = condition.Value,
26-
Type = (TConditionType)Enum.Parse(typeof(TConditionType), condition.Type.Identifier)
27-
}).ToList(),
24+
(
25+
(TConditionType)Enum.Parse(typeof(TConditionType), condition.Type.Identifier),
26+
condition.Value
27+
)).ToList(),
2828
ExcludeRulesWithoutSearchConditions = genericSearchArgs.ExcludeRulesWithoutSearchConditions
2929
};
3030
}
3131

3232
var searchArgs = new SearchArgs<TContentType, TConditionType>(contentType, genericSearchArgs.DateBegin, genericSearchArgs.DateEnd)
3333
{
3434
Conditions = genericSearchArgs.Conditions.Select(condition => new Condition<TConditionType>
35-
{
36-
Value = condition.Value,
37-
Type = (TConditionType)Enum.Parse(typeof(TConditionType), condition.Type.Identifier)
38-
}).ToList(),
35+
(
36+
(TConditionType)Enum.Parse(typeof(TConditionType), condition.Type.Identifier),
37+
condition.Value
38+
)).ToList(),
3939
ExcludeRulesWithoutSearchConditions = genericSearchArgs.ExcludeRulesWithoutSearchConditions
4040
};
4141

4242
return searchArgs;
4343
}
4444
}
45-
}
45+
}

tests/Rules.Framework.IntegrationTests.Common/Scenarios/Scenario6/Scenario6Data.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ namespace Rules.Framework.BenchmarkTests.Tests.Benchmark1
33
using System;
44
using System.Collections.Generic;
55
using Rules.Framework.BenchmarkTests.Tests;
6-
using Rules.Framework.Builder;
76
using Rules.Framework.Core;
87

98
public class Scenario6Data : IScenarioData<ContentTypes, ConditionTypes>
109
{
11-
public IEnumerable<Condition<ConditionTypes>> Conditions => new[] { new Condition<ConditionTypes> { Type = ConditionTypes.StringCondition, Value = "Let's benchmark this!" } };
10+
public IEnumerable<Condition<ConditionTypes>> Conditions => new[]
11+
{
12+
new Condition<ConditionTypes>(ConditionTypes.StringCondition, "Let's benchmark this!")
13+
};
1214

1315
public DateTime MatchDate => DateTime.Parse("2022-10-01");
1416

tests/Rules.Framework.IntegrationTests.Common/Scenarios/Scenario7/Scenario7Data.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ namespace Rules.Framework.BenchmarkTests.Tests.Benchmark2
33
using System;
44
using System.Collections.Generic;
55
using Rules.Framework;
6-
using Rules.Framework.Builder;
76
using Rules.Framework.Core;
87

98
public class Scenario7Data : IScenarioData<ContentTypes, ConditionTypes>
109
{
1110
public IEnumerable<Condition<ConditionTypes>> Conditions => new[]
1211
{
13-
new Condition<ConditionTypes> { Type = ConditionTypes.Artist, Value = "Queen" },
14-
new Condition<ConditionTypes> { Type = ConditionTypes.Lyrics, Value = "Is this the real life?\nIs this just fantasy?\nCaught in a landside,\nNo escape from reality" },
15-
new Condition<ConditionTypes> { Type = ConditionTypes.ReleaseYear, Value = 1975 }
12+
new Condition<ConditionTypes>(ConditionTypes.Artist, "Queen"),
13+
new Condition<ConditionTypes>(ConditionTypes.Lyrics, "Is this the real life?\nIs this just fantasy?\nCaught in a landside,\nNo escape from reality" ),
14+
new Condition<ConditionTypes>(ConditionTypes.ReleaseYear, 1975 )
1615
};
1716

1817
public DateTime MatchDate => DateTime.Parse("2022-11-01");

tests/Rules.Framework.IntegrationTests.Common/Scenarios/Scenario8/Scenario8Data.RoyalFlush.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace Rules.Framework.BenchmarkTests.Tests.Benchmark3
22
{
33
using System;
44
using System.Collections.Generic;
5-
using Rules.Framework.Builder;
65
using Rules.Framework.Core;
76

87
public partial class Scenario8Data : IScenarioData<ContentTypes, ConditionTypes>
@@ -22,8 +21,8 @@ private IEnumerable<Rule<ContentTypes, ConditionTypes>> GetRoyalFlushRules()
2221
.Value(ConditionTypes.QueenOfClubs, Operators.Equal, true)
2322
.Value(ConditionTypes.KingOfClubs, Operators.Equal, true)
2423
.Value(ConditionTypes.AceOfClubs, Operators.Equal, true)
24+
)
2525
)
26-
)
2726
.Build().Rule,
2827
RuleBuilder.NewRule<ContentTypes, ConditionTypes>()
2928
.WithName("Benchmark 3 - Royal flush of Diamonds: Ace, King, Queen, Jack, 10")
@@ -36,8 +35,8 @@ private IEnumerable<Rule<ContentTypes, ConditionTypes>> GetRoyalFlushRules()
3635
.Value(ConditionTypes.QueenOfDiamonds, Operators.Equal, true)
3736
.Value(ConditionTypes.KingOfDiamonds, Operators.Equal, true)
3837
.Value(ConditionTypes.AceOfDiamonds, Operators.Equal, true)
38+
)
3939
)
40-
)
4140
.Build().Rule,
4241
RuleBuilder.NewRule<ContentTypes, ConditionTypes>()
4342
.WithName("Benchmark 3 - Royal flush of Hearts: Ace, King, Queen, Jack, 10")
@@ -50,8 +49,8 @@ private IEnumerable<Rule<ContentTypes, ConditionTypes>> GetRoyalFlushRules()
5049
.Value(ConditionTypes.QueenOfHearts, Operators.Equal, true)
5150
.Value(ConditionTypes.KingOfHearts, Operators.Equal, true)
5251
.Value(ConditionTypes.AceOfHearts, Operators.Equal, true)
52+
)
5353
)
54-
)
5554
.Build().Rule,
5655
RuleBuilder.NewRule<ContentTypes, ConditionTypes>()
5756
.WithName("Benchmark 3 - Royal flush of Spades: Ace, King, Queen, Jack, 10")
@@ -64,8 +63,8 @@ private IEnumerable<Rule<ContentTypes, ConditionTypes>> GetRoyalFlushRules()
6463
.Value(ConditionTypes.QueenOfSpades, Operators.Equal, true)
6564
.Value(ConditionTypes.KingOfSpades, Operators.Equal, true)
6665
.Value(ConditionTypes.AceOfSpades, Operators.Equal, true)
66+
)
6767
)
68-
)
6968
.Build().Rule,
7069
};
7170
}

0 commit comments

Comments
 (0)