开发者

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 }
e开发者_如何学Clse
{ 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


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.


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.


Have you considered something like MEF, then you could have lots of small dlls implementing various versions of your calculations and simply reference which one to load up from the database.

That is assuming you can wrap them all in a single (or small number of) interfaces.


I would attack this problem by creating a domain specific language which the program could interpret to execute the rules. Then put snippits of the DSL code in the database.

As you can see, I also like to overcomplicate things. :-) But it works as long as the long-term use is simplified.


You could have your program compile up your rules at runtime into a class that acts like a plugin using the CSharpCodeProvider.

See Compiling code during runtime for a sample of how to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜