Skip to content

Commit 50970dc

Browse files
committed
Add rule set and rule classes
1 parent 152fafa commit 50970dc

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Diff for: SharedCode.Core/Workflow/Rule.Generic.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <copyright file="Rule.cs" company="improvGroup, LLC">
2+
// Copyright © 2022 improvGroup, LLC. All Rights Reserved.
3+
// </copyright>
4+
5+
namespace SharedCode.Workflow;
6+
7+
using System;
8+
9+
/// <summary>
10+
/// The rule class.
11+
/// </summary>
12+
/// <typeparam name="T">The type of input on which the rule operates.</typeparam>
13+
public class Rule<T>
14+
{
15+
private readonly Func<T, bool> condition;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="Rule{T}" /> class.
19+
/// </summary>
20+
/// <param name="condition">The function used to evaluate the rule condition; returns true if passed, false otherwise.</param>
21+
public Rule(Func<T, bool> condition) => this.condition = condition;
22+
23+
/// <summary>
24+
/// Determines whether the specified input satisfies the condition.
25+
/// </summary>
26+
/// <param name="input">The input to be evaluated.</param>
27+
/// <returns><c>true</c> if the specified input is satisfies the condition; otherwise, <c>false</c>.</returns>
28+
public bool IsSatisfied(T input) => this.condition(input);
29+
}

Diff for: SharedCode.Core/Workflow/Rule.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// <copyright file="Rule.cs" company="improvGroup, LLC">
2+
// Copyright © 2022 improvGroup, LLC. All Rights Reserved.
3+
// </copyright>
4+
5+
namespace SharedCode.Workflow;
6+
7+
/// <summary>
8+
/// The rule class.
9+
/// </summary>
10+
public class Rule : Rule<object?>
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="Rule"/> class.
14+
/// </summary>
15+
/// <param name="condition">The function used to evaluate the rule condition; returns true if passed, false otherwise.</param>
16+
public Rule(Func<object?, bool> condition) : base(condition)
17+
{
18+
}
19+
}

Diff for: SharedCode.Core/Workflow/RuleSet.Generic.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// <copyright file="Rule.cs" company="improvGroup, LLC">
2+
// Copyright © 2022 improvGroup, LLC. All Rights Reserved.
3+
// </copyright>
4+
5+
namespace SharedCode.Workflow;
6+
7+
using System.Collections.Generic;
8+
9+
/// <summary>
10+
/// A class that holds a set of rules that can be evaluated.
11+
/// </summary>
12+
/// <typeparam name="T">The type of input to be evaluated.</typeparam>
13+
public class RuleSet<T>
14+
{
15+
private readonly List<Rule<T>> rules = new();
16+
17+
/// <summary>
18+
/// Adds the specified rule to the set.
19+
/// </summary>
20+
/// <param name="rule">The rule to be added.</param>
21+
public void AddRule(Rule<T> rule) => this.rules.Add(rule);
22+
23+
/// <summary>
24+
/// Removes the specified rule from the set.
25+
/// </summary>
26+
/// <param name="rule">The rule to be removed.</param>
27+
public void RemoveRule(Rule<T> rule) => this.rules.Remove(rule);
28+
29+
/// <summary>
30+
/// Evaluates the specified input against the rules in the set.
31+
/// </summary>
32+
/// <param name="input">The input to evaluate.</param>
33+
/// <returns><c>true</c> if the input satisfies the conditions of all rules in the set, <c>false</c> otherwise.</returns>
34+
public bool Evaluate(T input) => this.rules.All(rule => rule.IsSatisfied(input));
35+
}

Diff for: SharedCode.Core/Workflow/RuleSet.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// <copyright file="Rule.cs" company="improvGroup, LLC">
2+
// Copyright © 2022 improvGroup, LLC. All Rights Reserved.
3+
// </copyright>
4+
5+
namespace SharedCode.Workflow;
6+
7+
/// <summary>
8+
/// A class that holds a set of rules that can be evaluated.
9+
/// </summary>
10+
public class RuleSet : RuleSet<object?>
11+
{
12+
}

0 commit comments

Comments
 (0)