Skip to content

Latest commit

 

History

History
133 lines (93 loc) · 6.57 KB

README.md

File metadata and controls

133 lines (93 loc) · 6.57 KB

Rules Framework

Rules.Framework is a generic framework that allows defining and evaluating rules for complex business scenarios.

Codacy Badge .NET build

What is a rule

A rule is a data structure limited in time (date begin and date end), whose content is categorized by a content type. Its applicability is constrained by conditions, and a priority value is used as untie criteria when there are multiple rules applicable.

Why use rules

By using rules, we're able to abstract a multiplicity of business scenarios through rules configurations, instead of heavy code developments. Rules enable a fast response to change and a better control of the business logic by the product owners.

Packages

Name nuget.org downloads fuget.org
Rules.Framework Nuget Package downloads Rules.Framework on fuget.org
Rules.Framework.Providers.MongoDB Nuget Package downloads Rules.Framework on fuget.org
Rules.Framework.WebUI Nuget Package downloads Rules.Framework on fuget.org

Rules.Framework

Nuget Package

The Rules.Framework package contains the core of the rules engine. It includes an in-memory provider for the rules data source.

Basic usage

To set up a RulesEngine, define the content types and condition types to be used.

enum ContentType { FreeSample = 1, ShippingCost = 2 }
enum ConditionType { ClientType = 1, Country = 2 }

Build the engine with the RulesEngineBuilder.

var rulesEngine = RulesEngineBuilder.CreateRulesEngine()
    .WithContentType<ContentType>()
    .WithConditionType<ConditionType>()
    .SetInMemoryDataSource()
    .Configure(c => c.PriorityCriteria = PriorityCriterias.TopmostRuleWins)
    .Build();

Use the RuleBuilder to assemble a rule.

var ruleForPremiumFreeSample = RuleBuilder
    .NewRule<ContentType, ConditionType>()
    .WithName("Rule for perfume sample for premium clients.")
    .WithContent(ContentType.FreeSample, "SmallPerfumeSample")
    .WithCondition(ConditionType.ClientType, Operators.Equal, "Premium")
    .WithDateBegin(new DateTime(2020, 01, 01))
    .Build();

Add a rule to the engine with the AddRuleAsync().

rulesEngine.AddRuleAsync(ruleForPremiumFreeSample.Rule, RuleAddPriorityOption.ByPriorityNumber(1));

Get a matching rule by using the MatchOneAsync() and passing a date and conditions.

var matchingRule = rulesEngine.MatchOneAsync(
        ContentType.FreeSample, 
        new DateTime(2021, 12, 25), 
        new[]
        {
            new Condition<ConditionType>(ConditionType.ClientType, "Premium")
        });

Complex scenarios

For a more thorough explanation of the Rules.Framework and all it enables, check the Wiki.

Check also the test scenarios and samples available within the source-code, to see more elaborated examples of its application.

Rules.Framework.Providers.MongoDB

Nuget Package

To keep rules persisted in a MongoDB database, use the extension method in the Providers.MongoDB package to pass your MongoClient and MongoDbProviderSettings to the RulesEngineBuilder.

var rulesEngine = RulesEngineBuilder.CreateRulesEngine()
    .SetInMongoDBDataSource(mongoClient, mongoDbProviderSettings)

Rules.Framework.WebUI

Nuget Package

The WebUI package offers a way of visualizing the rules in your web service. To configure the UI, pass the rules engine as generic to the IApplicationBuilder extension method provided.

app.UseRulesFrameworkWebUI(rulesEngine.CreateGenericEngine());

Access is done via the endpoint {host}/rules/index.html.

webUISample

Features

The following list presents features already available and others planned:

  • Rules evaluation (match one)
  • Rules evaluation (match many)
  • Rules content serialization
  • Rules management (Create, Read, Update)
  • In-memory data source support
  • MongoDB data source support
  • SQL Server data source support
  • Rules data source caching
  • Rules data source compilation
  • WebUI for rules visualization

Documentation

See the Wiki for full documentation, short examples and other information.

Contributing

Contributions are more than welcome! Submit comments, issues or pull requests, we promise to keep an eye on them :)

Head over to CONTRIBUTING for further details.

License

MIT License