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 JavaScript 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.
Related
Is there any way to parse or model the contents of a JavaScript file and determine what undefined references it requires, and what references it exports?
I'm looking for a way to automatically determine the correct loading order of giant collection of JavaScript files for a web app. Each file is essentially a module but many files have strange names and extra snippets of utility-type code; the code and its organization is so obtuse we're going to be doing this manually for weeks if it can't be automated.
This is kind of a broad question, which likely implies a nontrivial solution. A start would be looking at something like Jurassic to see if it lets you look at its AST, and working out what variables in which scope are resolved or not. If these are to run in browser, also consider that in Javascript the global scope is the window object, which you can "import" and "export" through, and this can be aliased to who knows what so you'll need to do some sort of dataflow analysis.
A possible alternative would be looking at the implementation of a minifier (I'd try UglifyJS because it explicitly supports parsing into an AST then builds minification on that), since this sounds like kind of what they need to do when determining which variable names they shouldn't abbreviate. That said minifiers are allowed to be imprecise and accept false positives, which might be a problem for you.
I have stored some C# code in a database table.
I have the whole code of a base class in a string.
With the stored C# code in hand, I add to the class a method named m1 that contains a return <<some C# code>>; (the method always returns object so the C# code can be: 88 + 90, "hello world", this.ToString(), etc.), save it to a file and compile it with CSharpCodeProvider, and run it on my program.
The stored C# code can use some methods in the base class.
This scheme works very well.
Now, I would to use Reflection.Emit to do this, to avoid the compiling step.
Is this possible, and if so, how would it be done?
Now, I would to use Reflection.Emit to do this, to avoid the compiling step.
That doesn't make much sense to me. If you have source code that you want to execute, you basically have two options:
Compile it to some other form that can then be directly executed. (Classic compiled languages like C work like this.)
Parse it into some in-memory structure and then execute that, piece by piece. (Classic interpreted languages work like this, like JavaScript in older browsers.)
Things aren't actually as simple as that these days, because of virtual machines and intermediate languages etc., but those are the basic choices.
If you don't want to use CodeDOM, that leaves you two choices (corresponding to the two options above):
Parse the code and then create some executable form from it, possibly using Reflection.Emit.
Parse the code and directly execute the result. You don't need Reflection.Emit for that.
Choice 1 means you would need to implement full C# compiler. Choice 2 means you would need to implement a half of C# compiler, plus an interpreter of your in-memory structure. In both cases, it would be a giant project and you wouldn't really “avoid the compiling step”.
This may seem like an odd question but I really do think it should be possible.
Here's the entire background that I would like to present so that you get a more clear understanding of what I'm trying to achieve.
I have a framework dll library which my project uses.
Within the library, I have embedded a .js file(lets call it b.js) that creates a javascript object(bjobj) containing the api for all my redundant js required functionality.
The class library also contains a class(lets call it bpage) which extends the system.web.ui.page class and provides additional functionality.
Within bpage, i've created a property that wraps around a Dictionary and lets the page that extends bpage add structs, nullable structs and strings into the dictionary.
Once values are added to the dictionary, developers can now access the values in javascript via the api methods through bjobj object.
My bpage creates json string and sends it to bjobj using clientscript.register... methods.
No hidden variables are used and the json string is automatically recognized by the browser as a json object during parsing.
Doing this eliminates the need for using hidden variables on the page.
The one big drawback to this entire technique is that, the changes made to the variables on the browser, could not be retrived on the server since everything happens from the framework dll.
I now need a mechanism using which no changes need be done on the pages that extend bpage, yet from my framework cs code and js code, I could automatically get the changes made in the json object back on the server.
To achieve the intented functionality, I currently seem to require two functionalities.
Firstly, my bjobj needs to be able to automatically capture any postback events so that it can stringify the json objects prior the beggining of the postback.
Secondly, there must be some mechanism using which the stringified json can be sent back to the server without using hidden fields, so that my bpage can capture the string and deserialize it.
Please provide your insights into the technique I'm trying.
My aim is to make certain that my codebase looks much cleaner and development speed be improved.
Here's the story so far:
I'm doing a C# winforms application to facilitate specifying equipment for hire quotations.
In it, I have a List<T> of ~1500 stock items.
These items have a property called AutospecQty that has a get accessor that needs to execute some code that is specific to each item. This code will refer to various other items in the list.
So, for example, one item (let's call it Item0001) has this get accessor that may need to execute some code that may look something like this:
[some code to get the following items from the list here]
if(Item0002.Value + Item0003.Value > Item0004.Value)
{ return Item0002.Value }
else
{ return Item0004.Value }
Which is all well and good, but these bits of code are likely to change on a weekly basis, so I'm trying to avoid redeploying that often. Also, each item could (will) have wildly different code. Some will be querying the list, some will be doing some long-ass math functions, some will be simple addition as above...some will depend on variables not contained in the list.
What I'd like to do is to store the code for each item in a table in my database, then when the app starts just pull the relevant code out and bung it in a list, ready to be executed when the time comes.
Most of the examples I've seen on the internot regarding executing a string as code seem quite long-winded, convoluted, and/or not particularly novice-coder friendly (I'm a complete amateur), and don't seem to take into account being passed variables.
So the questions are:
Is there an easier/simpler way of achieving what I'm trying to do?
If 1=false (I'm guessing that's the case), is it worth the effort of all the potential problems of this approach, or would my time be better spent writing an automatic update feature into the application and just keeping it all inside the main app (so the user would just have to let the app update itself once a week)?
Another (probably bad) idea I had was shifting all the autospec code out to a separate DLL, and either just redeploying that when necessary, or is it even possible to reference a single DLL on a shared network drive?
I guess this is some pretty dangerous territory whichever way I go. Can someone tell me if I'm opening a can of worms best left well and truly shut?
Is there a better way of going about this whole thing? I have a habit of overcomplicating things that I'm trying to kick :P
Just as additional info, the autospec code will not be user-input. It'll be me updating it every week (no-one else has access to it), so hopefully that will mitigate some security concerns at least.
Apologies if I've explained this badly.
Thanks in advance
Some options to consider:
1) If you had a good continuous integration system with automatic build and deployment, would deploying every week be such an issue?
2) Have you considered MEF or similar which would allow you to substitute just a single DLL containing the new rules?
3) If the formula can be expressed simply (without needing to eval some code, e.g. A+B+C+D > E+F+G+H => J or K) you might be able to use reflection to gather the parameter values and then apply them.
4) You could use Expressions in .NET 4 and build an expression tree from the database and then evaluate it.
Looks like you may be well served by implementing the specification pattern.
As wikipedia describes it:
whereby business logic can be recombined by chaining the business logic together using boolean logic.
Have you considered something like MEF, then you could have lots of small dlls implementing various versions of your calculations and simply reference which one to load up from the database.
That is assuming you can wrap them all in a single (or small number of) interfaces.
I would attack this problem by creating a domain specific language which the program could interpret to execute the rules. Then put snippits of the DSL code in the database.
As you can see, I also like to overcomplicate things. :-) But it works as long as the long-term use is simplified.
You could have your program compile up your rules at runtime into a class that acts like a plugin using the CSharpCodeProvider.
See Compiling code during runtime for a sample of how to do this.
I'm working on an ASP.NET web application that uses a lot of JavaScript on the client side to allow the user to do things like drag-drop reordering of lists, looking up items to add to the list (like the suggestions in the Google search bar), deleting items from the list, etc.
I have a JavaScript "class" that I use to store each of the list items on the client side as well as information about what action the user has performed on the item (add, edit, delete, move). The only time the page is posted to the server is when the user is done, right before the page is submitted I serialize all the information about the changes that were made into JSON and store it in hidden fields on the page.
What I'm looking for is some general advice about how to build out my classes in C#. I think it might be nice to have a class in C# that matches the JavaScript one so I can just deserealize the JSON to instances of this class. It seems a bit strange though to have classes on the server side that both directly duplicate the JavaScript classes, and only exist to support the JavaScript UI implementation.
This is kind of an abstract question. I'm just looking for some guidance form others who has done similar things in terms of maintaining matching client and server side object models.
Makes perfect sense. If I were confronting this problem, I would consider using a single definitive description of the data type or class, and then generating code from that description.
The description might be a javascript source file; you could build a parser that generates the apropriate C# code from that JS. Or, it could be a C# source file, and you do the converse.
You might find more utility in describing it in RelaxNG, and then building (or finding) a generator for both C# and Javascript. In this case the RelaxNG schema would be checked into source code control, and the generated artifacts would not.
EDIT: Also there is a nascent spec called WADL, which I think would help in this regard as well. I haven't evaluated WADL. Peripherally, I am aware that it hasn't taken the world by storm, but I don't know why that is the case. There's a question on SO regarding that.
EDIT2: Given the lack of tools (WADL is apparently stillborn), if I were you I might try this tactical approach:
Use the [DataContract] attributes on your c# types and treat those as definitive.
build a tool that slurps in your C# type, from a compiled assembly and instantiates the type, by using the JsonSerializer on a sample XML JSON document, that provides, a sort of defacto "object model definition". The tool should somehow verify that the instantiated type can round-trip into equivalent JSON, maybe with a checksum or CRC on the resulting stuff.
run that tool as part of your build process.
To make this happen, you'd have to check in that "sample JSON document" into source code and you'd also have to make sure that is the form you were using in the various JS code in your app. Since Javascript is dynamic, you might also need a type verifier or something, that would run as part of jslint or some other build-time verification step, that would check your Javascript source to see that it is using your "standard" objbect model definitions.