Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# executing a string as code...is it worth the effort?

Here's the story so far:

I'm doing a C# winforms application to facilitate specifying equipment for hire quotations.

In it, I have a List<T> of ~1500 stock items.

These items have a property called AutospecQty that has a get accessor that needs to execute some code that is specific to each item. This code will refer to various other items in the list.

So, for example, one item (let's call it Item0001) has this get accessor that may need to execute some code that may look something like this:

[some code to get the following items from the list here]

if(Item0002.Value + Item0003.Value > Item0004.Value)
{ return Item0002.Value }
else
{ return Item0004.Value }

Which is all well and good, but these bits of code are likely to change on a weekly basis, so I'm trying to avoid redeploying that often. Also, each item could (will) have wildly different code. Some will be querying the list, some will be doing some long-ass math functions, some will be simple addition as above...some will depend on variables not contained in the list.

What I'd like to do is to store the code for each item in a table in my database, then when the app starts just pull the relevant code out and bung it in a list, ready to be executed when the time comes.

Most of the examples I've seen on the internot regarding executing a string as code seem quite long-winded, convoluted, and/or not particularly novice-coder friendly (I'm a complete amateur), and don't seem to take into account being passed variables.

So the questions are:

  1. Is there an easier/simpler way of achieving what I'm trying to do?
  2. If 1=false (I'm guessing that's the case), is it worth the effort of all the potential problems of this approach, or would my time be better spent writing an automatic update feature into the application and just keeping it all inside the main app (so the user would just have to let the app update itself once a week)?
  3. Another (probably bad) idea I had was shifting all the autospec code out to a separate DLL, and either just redeploying that when necessary, or is it even possible to reference a single DLL on a shared network drive?

I guess this is some pretty dangerous territory whichever way I go. Can someone tell me if I'm opening a can of worms best left well and truly shut?

Is there a better way of going about this whole thing? I have a habit of overcomplicating things that I'm trying to kick :P

Just as additional info, the autospec code will not be user-input. It'll be me updating it every week (no-one else has access to it), so hopefully that will mitigate some security concerns at least.

Apologies if I've explained this badly.

Thanks in advance

like image 397
Jez Clark Avatar asked Oct 12 '22 10:10

Jez Clark


2 Answers

Some options to consider:

1) If you had a good continuous integration system with automatic build and deployment, would deploying every week be such an issue?

2) Have you considered MEF or similar which would allow you to substitute just a single DLL containing the new rules?

3) If the formula can be expressed simply (without needing to eval some code, e.g. A+B+C+D > E+F+G+H => J or K) you might be able to use reflection to gather the parameter values and then apply them.

4) You could use Expressions in .NET 4 and build an expression tree from the database and then evaluate it.

like image 110
Ian Mercer Avatar answered Oct 15 '22 09:10

Ian Mercer


Looks like you may be well served by implementing the specification pattern.

As wikipedia describes it:

whereby business logic can be recombined by chaining the business logic together using boolean logic.

like image 27
Oded Avatar answered Oct 15 '22 11:10

Oded