I work on system with an angular based frontend and Net Core / webapi backend. This is a powerful engineering app that does a lot of calculations and analisys in typescript code in order to provide the user a rich and informative experience. This code includes things like statistics and calculations performed client side and immediately presented to the user in the form of plot, charts or warnings. Therefore, lot of the business logic resides now in the form of typescript code. This code needs to be client side for user experience/reactivity purposes.
Now, we're building on backend tasks such as data interfaces or even daily evaluations of user data and therefore the server "needs" to performs such calculations as well, in order for it to replicate this user experience but in an automated/unattended way. For instance, load data from some remote web service and tell the user simmilar stuff the frontend would tell in real time. Code duplication is not an option since this is a lot of complex code. The logic needs to be the same.
I make this preamble to state that this is not a frontend-first vs backends-first issue. Therefore, I am looking for a way to have some of that code available to the C# backend. I imagine stripping the typescript -say- static code such as "function PerformComplexCalculation(jsonObjectParam,...,...)" and executing the same or at least a synced copy of that code in C#.
I know it is possible to interop with JS, but I have never done it so there are a couple questions
- how does it work with parameters / returned data? I guess theres's serialization involved all over the place
- could it work with typescript?
I could live with the idea of a nodejs or simmilar backend app/serice handling this I could live with the idea of having to copy the codebase. A synced copy is not the same as logic duplication I understand the "interopped" classes should be fat-free (pure & static functions)
I know this is not a simple question nor it has a single answer. For the sake of the argument I'll try to add some dummy code
- typescript shared code
export class FringeMethods
{
static doWeirdStuff(allInputsNeeded){
let x = allInputsNeeded.a + allInputsNeeded.b / allInputsNeeded.c;
if(x > 0) return "boom!";
else return "bang!";
}
}
- C# theoric code
public void LoadData(){
foreach(var entity in someData){
var allInputsNeeded = convertEntityToSomethingInteroperable(entity);
var evaluation = interop.invoke("FringeMethods.doWeirdStuff", allInputsNeeded );
if(evaluation == "bang!"){
SendEmailToSuperman("Entity {entity} goes bang!");
}
}
}