Preserving calculations in The Database : The Question

I am creating a webpage that creates documents in a highly templated way.

Part of the documents though involves a calculation. My problem is that there are an abundance of different calculations it can be.

What I’d love to have is to create a lambda expression and persist it to the database (SQL, MongoDB, or DocumentDB) directly. This would mean that upon retrieving the template it would simply be a matter of invoking the expression against the data. Unfortunately it does not seem to be able to be done in a straightforward manner. For all the advancements in C# being more functional. Database persistence of functions is still not something that’s available

Option 1:

Store Name of Calculation in an Enum (and enum in the DB) then execute calculation based on the value of the enum.

Pros:

  • Easy to store
  • Easy to understand

Cons:

  • The code will be confusing
  • Not actually storing the calculation. Just the promise it wont change

Option 2:

Execute the calculation in some other language that does allow persistence of functions. i.e. javascript.

Pros:

  • Can likely be very powerful
  • Should be easy to understand the value stored

    Cons:

  • Unlikely the interop is smooth
  • Another language adds to complexity

Option 3 :

Some form of text storage and Eval.

Pros:

  • Easy to understand whats going on

    Cons:

  • External Library
  • Likely very slow
  • Lots of restrictions on actions.

Option 4:

Something else I haven’t even considered. Maybe you have the right answer.