Avoid duplicate logic in JavaScript and C#
I am writing a wizard to let users map strings to properties on an object. This is done by using some predefined rules that the user selects and supplies the arguments to. These collections of rules are saved to a database and run later via service calls.
The problem is that in the wizard I have it highlighting and updating some example text as the user selects the rules and types the arguments. This is done using Jav开发者_如何学运维aScript so obviously is duplicating the logic contained inside the C# rules.
So I'm looking for ways to get around this.
The rules are quite simple and just contain a list of arguments to apply and a single method that takes the input string and returns the result.
You can use AJAX to send the data to the backend, process it, and drop it in the right place. This wouldn't duplicate that logic then. You'll likely need to maintain a bit of JS code to keep the screen and the service attached though.
I have a similar situation with JavaScript and Java. My solution was to just use JavaScript: On the client, that's run by the browser. On the server, in my case, it's compiled with Rhino (JavaScript for the JVM), but it's the same source code in both cases.
The .Net platform supports JScript.Net, which is very similar to JavaScript. I expect without too much effort you could write the code once, in JavaScript, and have JScript.Net compile it into a module you could use server-side, alongside your C# code.
精彩评论