Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business rules that are valid for specific time span – how to manage in an orderly manner

I just started working for a government agency and I have come across an interesting problem: business rules depend on legislature and as such they have to respect the exact time periods that legislature has been active.

To give you an example, if a subject has applied for a subsidy on a certain date, than he has to be evaluated according to criteria that vas valid on that given date. That same subsidy, for someone that applied on some later date has different criteria. I was wondering if there is a known pattern to deal with these time-dependent rules in orderly fashion. At the moment, the code is sprinkled with expressions akin to:

if application.date >”July 17th, 2008”

What is the best way to manage this problem?

like image 497
Dan Avatar asked Jun 30 '11 17:06

Dan


3 Answers

This looks like a case for Chain of Responsibility. You would have handlers for each legislature. You pass the application to the most recent handler first. If it is too old, it passes it down to the handler for the previous legislature.

like image 196
Björn Pollex Avatar answered Nov 14 '22 14:11

Björn Pollex


You can use a few patterns. For example using the temporal property pattern you can create on object which contains business rules which are active at a certain point in time. Also, using the specification pattern you can create business rules which are effective based on a certain date. You can associate an effective date range with each policy so that the decision of whether a given policy applies can be made. The book Analysis Patterns contains many patterns that could be applicable in your scenario.

EDIT: I meant to link to the temporal object pattern instead of temporal property. This may shed some light on implementing the database mapping.

like image 5
eulerfx Avatar answered Nov 14 '22 15:11

eulerfx


If the rules are complex you may need a (business) rules engine. I don't know what implementations exist for your platform, but for Java the typical choice is Drools and its Expert subproject. It defines domain-specific language for defining rules. Note that it might have an interface for non-java users.

Otherwise you may try something like the Strategy pattern - you have a list of strategies with two methods appliesTo(date) and handle(data). You iterate the list and if a strategy is applicable to a date - let it handle the data.

like image 1
Bozho Avatar answered Nov 14 '22 14:11

Bozho