1

I have a School management system built in ASP.NET Core 2.0 MVC that each school has a user (it is a single application deploy and multi user).

However each school has it your own way to calculate student grades. It it's not a simple average, sometimes there some conditionals and some simple logic. Our product is applied in South America, each school has its own grade process.

In our previous system we used to use a DB Registry containing a PHP code that is easily evaluated as a PHP logic that calculates the grades and, sometimes, adds a custom element in the rendered page.

I'm looking for something that can make me achieve the similar results using our new ASP.NET MVC Core 2.0 App. Once C# is compiled do MSIL Instead of a JIT code like php.

How can I evaluate a custom logic based on a string ? Is that possible in C#?

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
  • show what have you done so far. – Vijunav Vastivch Jan 17 '18 at 00:18
  • We do not calculate grades average in our news system yet. – Daniel Santos Jan 17 '18 at 00:23
  • 1
    We're not here to write your logic for you. It's up to you to make an attempt. If you get stuck on a specific part of your implementation, then provide an [MCVE](https://stackoverflow.com/help/mcve). – mason Jan 17 '18 at 00:41
  • @mason I'm not asking for a "ready to use" code nor a logic. I'm not a newbie user.. I'm asking if is possible to achieve the same results using asp.net, once c# is compiled do MSIL Instead of JIT code like php. I was wondering for something like "try to use this method" or even a "it is not possible using c#" answer. I'm sorry if I didn't make my question clear enough. I do have a language barrier from my own. – Daniel Santos Jan 17 '18 at 00:55
  • By your description I'm thinking interfaces. Use dependency injection in your app and implement interfaces for each different client logic. – Crowcoder Jan 17 '18 at 01:03
  • 2
    There are many ways depending on what you want to do. In MVC 5 you could make a custom view engine to be able to execute views that are stored in a database (pretty sure you can still do it in .NET Core). You can use [emit](https://msdn.microsoft.com/en-us/library/system.reflection.emit(v=vs.110).aspx) to build your own logic on the fly. Or use a library such as [JavaScript.NET](https://github.com/JavascriptNet/Javascript.Net) or [NCalc](https://github.com/MichaelAguilar/NCalc) to execute logic or do math based on string input. – NightOwl888 Jan 17 '18 at 01:04

1 Answers1

1

One option would be to define your expression syntax and then write a parser for it that outputs a System.Linq.Expression. You can then .Compile() and Invoke() it.

        System.Linq.Expressions.Expression<Func<int, int, int, double>> calculateGrade;

        calculateGrade = (int english, int math, int french) => (english + math + french) / 3.0;

        double result = calculateGrade.Compile().Invoke(23, 34, 19);

Instead of the constant expression shown here you would assemble one using Expression.Lambda, Expression.Parameter etc. from your parsed result.

My own library AboditNLP does something similar. It parses either an english language (NLP) description or a serialized expression to create a parsed expression tree than can then be converted to a System.Linq.Expression and evaluated against an in-memory object, or it can be converted to a SQL expression for a query against a database.

See https://stackoverflow.com/a/12903023/224370 for some suggestions as to parsers that could be used.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133