Joseph Caruana
Joseph Caruana

Reputation: 2271

Dynamic Code - C#

We are developing a product that will be deployed for a number of clients. In an area of this product, we need to execute some formulas. The problem is that this formulas can be different for each client. So we need to have a good architecture that can 'execute code dynamically'

The application is a web application hosted on sharepoint 2010 (thereby using .net 3.5)


A Simplified example:
I have class MyClassA with two numeric properties PropA and PropB For one client the formula is PropA + PropB. For the other it is PropA-PropB. This is just a simplified example as the formula is more complex than this.

I need to have a way that client A we can set PropA+PropB perhaps in an XMl file or database. I would then load the code dynamically?

Does this make sense? Has anyone implement similar scenario in a production environment please?

I have found that the following article describes a similar situation but I do not know whether it is 100% reliable for a production environment: http://west-wind.com/presentations/dynamicCode/DynamicCode.htm

I have also found that IronPython can also solve a similar problem but I cannot understand how I would use my ClassA with IronPython.

Any assistance would be greately appreciated.

Update ... Thanks everyone for the detailed feedback. It was a very constructive exercise. I have reviewed the different approaches and it seems very likely that we will go ahead with the nCalc approach. nCalc seems to be a wonderful tool and I am already loving the technology :) Thank you all!!

Upvotes: 3

Views: 489

Answers (4)

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

ClassA with Ironpython? Keep it simple

Run through the classA instance for each member (maybe a custom attribute to mark up the ones you want to use in the calc) and end up with name=value pair which by some unfortunate coincidence looks like an assignment

e.g

PropA = 100
PropB = 200

Prepend that to your python script

PropAPropB = PropA + PropB

Execute the script which is the assignments and the calculation

Then it's basically

ClassB.PropAPropB = ipCalc.Eval("PropAPropB");

You can start getting real clever with it, but a methods to get the inputs from an instance and one the evaluatios the result of the calc and sets teh properties.

Bob's your mother's sister's brother...

Upvotes: 0

Greg Kramida
Greg Kramida

Reputation: 4244

Do you have a fixed set of formulas, or does the client have the capacity to dynamically type those in, i.e. as for a calculator? In the first case, I'd recommend the following: set of C# delegates, which get called/call each other in a particular order, and (a) Dictionary(ies) of closures which fit the delegates. The closures would then be assigned to the delegates based on your predefined conditions.

In the alternative case, I wouldn't compile .NET code based on what the client types in, since that (unless preempted) represents a server-side security risk. I would implement/adapt a simple parser for expressions that you're expecting.

P.S. nCalc sugguested by Chris Lively is definitely a viable option for this kind of task, and is better than directly using delegates if you have tons and tons of formulas that you don't want to keep in memory.

Upvotes: 1

Francesco Belladonna
Francesco Belladonna

Reputation: 11719

I'm just proposing this because I don't know the problem very well but the idea could be a Dll for each formula (so you can handle the code as you wish, with normal C# functionalities instead of an uncomfortable xml file).

With MEF you can inject dll into your code (you just have to upload those when you develop a new one, no need to recompile the exe file) and have a different formula for each client.

This is my idea because it looks like a perfect example for Strategy pattern

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88082

Look into nCalc.

You could store your calculations in a database field then simply execute them on demand. Pretty simple and powerful. We are using this in a multi-tenant environment to help with similar types of customization.

Upvotes: 5

Related Questions