Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize specification pattern to database

OK, we have to calculate eligibility and rates for insanely complicated insurance enrollment data. Policies can be available based on age, employment characteristics, shoe size, etc. People born before 1962 may be excluded except when they live in Texas and have a poodle, etc.

So we can build classes that implement a specification pattern. We can nest and specifications, or specifications, etc. That's great. But now how do we serialize this to the database?

We could dump our c# classes to xml and leave it at that. But that is brittle, but almost impossible to query. Serializing a class to xml and dumping it into a big text field has a definite code smell.

Is there a canonical answer to how to put a nested specification into a database? My Google-fu fails me.

like image 687
Code Silverback Avatar asked Apr 26 '12 15:04

Code Silverback


2 Answers

Normalisation, it can be the bane of many a project.

Generally I opt for a pragmatic approach of having individual columns for anything that can be queried (and hence indexed) and dumping all others into a blob in a serialised form.

If this isn't an option for you, you're going to have to introduce redundancy into your table by creating columns that are not applicable to all rows.

I'm yet to come across a one-size fits all approach to this sort of problem.

like image 64
Rich O'Kelly Avatar answered Nov 18 '22 04:11

Rich O'Kelly


Using reflection, all you need to get a hold on a class is a fully qualified name . So, store THAT in the database.

like image 41
Mohamad Akra Avatar answered Nov 18 '22 05:11

Mohamad Akra