Serialize specification pattern to database
Asked Answered
B

2

7

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.

Bruis answered 26/4, 2012 at 15:12 Comment(5)
You didn't specified what database you use. I assume it's some relational one. You may try some other database types. Say something like MongoDB may help you in this case.Kei
I'm not really sure how throwing these things into an object database is really any different than just serializing them to the filesystem or chucking them into a giant text blob. What does Mongo get me here that I can't get by deserializing and running linq to objects on them?Bruis
If I understood right, your problem is how to place different objects (objects with different properties) into a database and then query them. MongoDB (or other same databases) let you to put data of any structure in one collection (analog of table in relational DB) and efficiently and fast query them later. You don't need to worry about (de)serialization, as it's done for you by a database.Kei
...forgot: MongoDB will index your data. But if you serialize it yourself, you just get row data (binary, text, whatever), than can't be queried without get deserialized back. In MongoDB you may just query your data without any preparations.Kei
I appreciate it was a decade ago but do you remember what you ended up doing and how it worked out?Dipterocarpaceous
G
1

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.

Gauss answered 26/4, 2012 at 15:18 Comment(0)
R
1

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

Redeemer answered 3/6, 2012 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.