Keeping the same logic implemented in C# and JS in sync

1.1k views Asked by At

There is a need to have a calculator for our invoices which will be used both in the web UI and in the back-end. This requires two implementations of the same logic in Javascript and C# and the real problem is to keep these two logics in sync when it comes to changing the rules.

At the moment, what we do in the JS side is that we send all the requests for recalculations to the backend through ajax calls in order to use the calculator implemented in C#. This keeps our servers busy and I don't like the idea. I think that there should be a rule engine or something somewhere that we could utilise to keep the calculation logic in a language other than C# or JS and use/interpret it in both the front-end and the back-end.

Has anyone else faced the same problem? Any idea would be appreciated.

3

There are 3 answers

3
citykid On BEST ANSWER

Faced the same problem and solved it by implementing the logic in javascript and then run it with https://jurassic.codeplex.com/ on the server. Works nicely.

0
xxbbcc On

You can try using Haxe to implement your rule engine and then cross-compile it to JavaScript and C#. This would give you a common implementation base for both and then - during compilation - your two different language outputs would be formed.

Disclaimer: I haven't used Haxe yet so I can't testify if it'd work for your situation but it supports both languages as targets.

0
Arash Aghlara On

You can use any decision-as-a-service platform. They usually expose decisions(bunch of connected business rules with relations and dependencies) as a REST API service. So you can simply interact with the service for execution and management of rules.

For example here is a sample how you can communicate with decision service for execution by passing input parameter values in JavaScript.