Maintaining Consistency Between JavaScript and C# Object Models - c#

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.

Related

.cs class definitions auto-created in .ts

I need to have a REST server (written in C#) pass a JSON object to/from my typescript client running in a browser. The best way to do this is define the class on the C# side that then creates a JSON object passed to the client where that JSON object matches the class structure in the client.
Which leads to the obvious question - is there a way to define the classes in C# and then run some program that will create the .ts class definitions? Or the reverse where I write out the classes in .TS and a program then creates matching .cs classes?
What I want to avoid is having to make sure any member added on one side is then added exactly the same on the other side.
And in a perfect world, the comments written for the class members are carried across too.
Update: I know I can write such a tool. However I'm hoping it already exists as that's a lot of work.
Type lite http://type.litesolutions.net/ gets you halfway. Just the data member signature.
As you know json doesn't carry behaviour just data. So no functions will not be available on the other side. It's not a "transpiler"
And in a perfect world, the comments written for the class members are carried across too.
Doesn't do this.
I created a library which allows you to create JS-models for knockout and backbone out of c#-classes (mainly for domain-classes, so it comes with stuff like DataAnnotations-support, etc).
I added support for Typescript, as well as a small tool to create the files directly.
Check it out and if you have time, I'd love some feedback :)
https://jsmapper.codeplex.com/
Cheers,
Richard

Content type lifecycle management

I saw thisarticle in the link below and I wonder if you could help me with some questions I have
http://msdn.microsoft.com/en-us/library/ee330223(v=office.12).aspx
In our current project, which we didn’t not develop but we have to maintain, we are facing some issues, it looks like the first time the other firm developed the content types, they were done fine, using xml definitions, creating list templates and list instances was also done fine and in an organized way.
However at some point in time and after the content types and lists were already running on production, some changes had to be done (adding new fields to existing content types, changing translations of displayname or groupname, changing properties like required, showinnewform, showineditform, etc)
Across the internet I have found that many people have problems with unghosted content types, which means that the content type is detached from its XML definition, as far as I know this happens when somebody modified the child content type or list using the UI.
I am trying to collect a list of best practices for managing content types after they are deployed:
1.How to add a new field to an existing content type?
For this we have used UpgradeActions and AddFieldRef
2.How to remove an existing field from a content type?
For this, we haven’t needed it yet, but I have see that there also exist the RemoveFieldRef element which could be used inside UpgradeActions
3.How to reorder fields in a content type?
We do this by code in a custom upgrade action.
4.How to change a translation in an existing field?
We do this by code in a custom upgrade action.
5.How to change properties like ShowInDisplayForm, ShowInNewForm, Hidden, Required, etc.
We do this by code in a custom upgrade action.
I wonder if my list above specially points 3,4 and 5 can be called best practices, or if I am missing something or doing something wrong? Why? A few weeks ago we had a lot of problems, when doing changes via code and pushing down the changes was not working, the changes were not pushed(we were not seeing the changes in the lists). After reading for many hours, I saw that this might be possible due to that the list content type LINK is broken from its Parent content type definition.
I found that a way to restablish this link can be done using SQL but it’s not supported of course.
http://www.olavaukan.com/2010/10/content-types-can-be-unghosted-too/
http://soerennielsen.wordpress.com/2007/09/08/convert-%E2%80%9Dvirtual-content-types%E2%80%9D-to-physical/
Maybe somebody can guide me in the right direction?

C#/.NET and JavaScript: Determining implicit exports and imports of .js file

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.

How to obtain json string during postback without using hidden fields?

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.

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 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.

Categories

Resources