Wednesday, 1 July 2009

Loosely couple code/data for static known types

So the title is a bit of a mouthful but I couldn't think of a better one.

The problem is simple and we have all faced it.  We have a database with static data that never or hardly changes.  The major issue we have is that we keep a copy of the data on the database and the we make loose runtime references to the code which can break our application.

For example lets say I have a payroll database.  Some employees are paid weekly, every 2 weeks or ever calendar month.  So I may choose to represent this data in a table called payfrequencytypes where i store my 3 rows.  From a database perspective this is cool.  I can join to this table, I can run reports etc.

From my application layer this is not so great.  If I wish to create some application business logic to determine when to pay my employee, I will need to check their pay frequency.  At this point I need to mix code and data, typically you will see something like

if (emp.PayFrequency == "Monthly")

PayFequency types will never change so I want static compiler checking.  I really want my code to be something like

if (emp.PayFrequency == PayFrequencies.Monthly)

This is much better for me at the app layer however the problem here is that I need to keep my 2 layers in sync now (app and code). 

I think code generation solves this problem.  Really we need to define in our application what static data we require, how we generate it the database and validation routines to make sure it is correct in the db.  We can then make our application responsible for generation and validation meaning we can feel sure that we can use static types

Obviously this example can get more complicated but you see the point.

I will try and put some code generation stuff when I have time that solves that issue for simple examples.

In the future I think this is exactly the sort of problem that Oslo could solve for us.

No comments: