title: How to allow users to define financial formulas in a C# app
body: <p>I need to allow my users to be able to define formulas which will calculate values based on data.  For example</p>
  
  <pre><code>//Example 1
  return GetMonetaryAmountFromDatabase("Amount due") * 1.2;
  //Example 2
  return GetMonetaryAmountFromDatabase("Amount due") * GetFactorFromDatabase("Discount");
  </code></pre>
  
  <p>I will need to allow / * + - operations, also to assign local variables and execute IF statements, like so</p>
  
  <pre><code>var amountDue = GetMonetaryAmountFromDatabase("Amount due");
  if (amountDue &gt; 100000) return amountDue * 0.75;
  if (amountDue &gt; 50000) return amountDue * 0.9;
  return amountDue;
  </code></pre>
  
  <p>The scenario is complicated because I have the following structure..</p>
  
  <ol>
  <li>Customer (a few hundred)</li>
  <li>Configuration (about 10 per customer)</li>
  <li>Item (about 10,000 per customer configuration)</li>
  </ol>
  
  <p>So I will perform a 3 level loop. At each "Configuration" level I will start a DB transaction and compile the forumlas, each "Item" will use the same transaction + compiled formulas (there are about 20 formulas per configuration, each item will use all of them).</p>
  
  <p>This further complicates things because I can't just use the compiler services as it would result in continued memory usage growth. I can't use a new AppDomain per each "Configuration" loop level because some of the references I need to pass cannot be marshalled.</p>
  
  <p>Any suggestions?</p>
  
  <p>--Update--
  This is what I went with, thanks!
  <a href="http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application" rel="nofollow noreferrer">http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application</a></p>