views:

326

answers:

3

I am creating a payroll application in .NET. One of the requirements is that the attendance and deduction rules should be dynamic and flexible to the most extent. The user should be able to define his own rules, where every employee will be bound to an attendance rule.

One solution is to compile C# code on the fly where the code to every attendance rule is stored in the database and compiled at run time, but this isn't the most elegant solution since the rules can only be read/understood by a technical person, also the performance and readability of the code will not be the best possible thing.

I am wondering if there is a solution \ architecture pattern that allows me to define and apply attendance rules and calculate the deductions based on them without writing scripts or dynamically compile C# code.

+1  A: 

Boo is a full-fledged .NET language with an extensible compiler. Ayende has written a nice book called "Building Domain Specific Languages in Boo", where he discusses the topics of extending a compiler to allow for something like:

when User.is_preferred and Order.total_cost > 1000: 
    add_discount_precentage 5 
    apply_free_shipping 
when not User.is_preferred and Order.total_cost > 1000: 
   suggest_upgrade_to_preferred  
    apply_free_shipping 
when User.is_not_preferred and Order.total_cost > 500: 
   apply_free_shipping

When compiled to .NET assembly these rules will execute blazingly fast. You do need, however, to execute the generated code in another AppDomain so that it can be unloaded in case rules change.

Anton Gogolev
A: 

You can embed an interpreter and use some templates, visual tools to let your users add new fractions of code.

M. Utku ALTINKAYA
+1  A: 

Its a really bad practice to rely on code compilation to change your system, you might as well say "rules can be changed any time, all you need is a developer".

If you define your rules, you can store the parameters in the DB, and apply them with code that reads them at runtime. Your code will be more complex, but that's the price we pay for configuration.

You need to define the rules - ie formally say what paramaters my be used, and how they can be applied.

eg. if 90% attendance means a 5% deduction, then you'll store these 2 values in the DB. Your code will then get the actual attendance, find the corresponding row and apply the deduction. Unless your rules are so complex that they cannot be modelled in parameters like this, using a configuration system is the best way to proceed. You can then supply a simple GUI to the users to tweak the values in the rules.

gbjbaanb