开发者

perform validations dynamically depending upon rules

开发者 https://www.devze.com 2022-12-21 09:18 出处:网络
I am trying to design an HR leave application system. I need to know how best I can try to implement the following requirement in the front-end, as well as how to persist data in the db so that I can

I am trying to design an HR leave application system. I need to know how best I can try to implement the following requirement in the front-end, as well as how to persist data in the db so that I can handle validations dynamically. Here is a small example of how we'd go about validating whether the employee is eligible for a leave.

Suppose he/she is applying for "sic开发者_C百科k leave" this type of leave cannot come directly next day to a "casual leave". One cannot apply for casual leave if the day previous to which he/she is applying for was marked for sick leave. Once we have validated this, we need to validate against leave balances. Suppose the employee is a programmer; he/she can take a max of 1 sick leave per month (or salary cycle). The number of sick leaves one can take in one month is dependent on the employee's designation.

The above was a simple example. The rules can go complex, and numerous depending upon the employee designation. I am looking at persisting such rules in the database.(what structure/attributes would I need for this) AND also evaluating the rules dynamically in front-end code (C#) (what design patterns do I need for this)


I recently read an article on Martin Fowler's site that talks about the "Specification" pattern.

Here is the link Specification Pattern

I think it could be suitable in your situation.


Rules Engines are ideal for complex business rules that can change frequently. Here's a list for .Net.

We use Drools (Java version) to process complex, ever-changing financial rules and it's been very successful for us.

Here's a good "getting started" tutorial

Getting Started with Drools.Net

Here's the full user guide

Drools.Net User Guide


I worked on a system once where we had a set of rules let's say we defined 10 distant types of rules. Leave Balance would be 1 rule, for example. We then created a very simple expression tree and evaluated each node to determine if our condition passed. For example here is an AndRule which can be used to tie two operands together.

public Rule
{

   public abstract Eval(ContextData data);
}

public AndRule
{
    public Rule Left{get;set;}
    public Rule Right{get;set;}
    public override bool Eval(ContextData data)
    { 
       return Right.Eval(data) && Left.Eval(data);
    }
}

The next thing we did was enable the whole graph to be serialized to Xml. This allowed to persist the a clob of the rules to the database. The rules can be dynamically switched out by replacing the tree. Now yes you cannot dynamically create a new type of rule but I doubt your HR department is sitting around inventing new ways to limit days off (or maybe they are if you work for the PHB)

0

精彩评论

暂无评论...
验证码 换一张
取 消