Rules Engine in .NET

Introduction

This article provides a basic understanding of the Rules Engine for beginners .

The Rules Engine is a . NET C# project that validates business logic by defining a multiple rules for your data classes. Rules are defined using a fluent-interface (fluent validation) helper class, and not by decorating your existing objects with attributes, therefore de-coupling validation logic from data (or domain) classes.

Background

It makes it easier for developers to define business rules on domain objects without coupling the domain object to the business rule. The Rule Engine supports cross-field validation, conditional validation and the rules are also Inheritable and Extensible .

Using the code

Here you can watch the sample rule created, which is an Operator Test. We can create our own rules related to our scenario.

public class ProductBooks

{

    public ProductBooks(decimal Price)

    {

        this._Price = Price;

 

    }

 

    private decimal _Price;

    public decimal Price

    {

        get { return _Price; }

        set { _Price = value; }

    }

 

    public static bool IsValueNull(decimal value, string errormsg)

    {

        bool isvalid;

        if (value == null)

        {

            isvalid = false;

 

        }

        else

        {

            isvalid = true;

            errormsg = "erro";

        }

        return isvalid;

    }

}

 

Creation Of Rules

  1. Open Visual Studio 2012 or earlier. Create a Sample Project.

  2. Add a Folder named Rules, then add all your class files, where you can download the associated files.
     

Rules-Engine1.jpg

Figure 1

Testing of Rules

  1. Right-click on Solution Explorer add New Unit Test Project where you can write all the test cases as shown in Figure 1

  2. Add a reference of Sample.Rules in test project

  3. Set the test project as the start up project

Here you can see the sample test methods:

[TestClass()]

    public class ProductBooksTest

    {

        enum Column

        {

            ID,

            BookName,

            BookDescription,

            Price

 

        }

 

        private TestContext testContextInstance;

 

        /// <summary>

        ///Gets or sets the test context which provides

        ///information about and functionality for the current test run.

        ///</summary>

        public TestContext TestContext

        {

            get

            {

                return testContextInstance;

            }

            set

            {

                testContextInstance = value;

            }

        }

 

        #region Additional test attributes

 

        #endregion

 

       /// <summary>

       /// IsvaluenullTest method will check the price columns which are specified in enum and datatable

       /// and it throws error when the column value is NULL.

       /// </summary>

        [TestMethod()]

        [DataSource("System.Data.SqlClient", "Ur Connectin String", "Books", DataAccessMethod.Sequential)]

        public void IsvaluenullTest()

        {

            decimal PRICEE = (decimal)TestContext.DataRow[(int)Column.Price];

            string errormsg = string.Empty; // TODO: Initialize to an appropriate value

            bool expected = true; // TODO: Initialize to an appropriate value

            bool actual;

            actual = ProductBooks.IsValueNull(PRICEE, errormsg);

            Assert.AreEqual(expected, actual);

           // Assert.Inconclusive("Verify the correctness of this test method.");

        }

 

    }

<span class="Apple-style-span" style="font-family: 'Segoe UI', Arial, sans-serif; white-space: normal; font-size: 14.4px; color: rgb(17, 17, 17); "> </span>


Here you can watch the Test Results passed:

Rules-Engine2.jpg

Figure 2

Points OF Interest

Validating XML Rules

Scheduling a rule to execute at a specified date and time

Conclusion

Use of a rules engine can significantly reduce the complexity of components that implement the business rules logic in your applications.

Up Next
    Ebook Download
    View all
    Learn
    View all