本文介绍了保持JavaScript和C#的对象模型之间的一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个使用大量的JavaScript客户端上,以允许用户做这样的事情列表拖放重新排序的ASP.NET Web应用程序的工作,查找商品添加到列表(如在谷歌搜索栏建议),从列表中删除等项目。

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.

我有一个JavaScript下课,我用它来存储每个在列表中的项客户端的信息以及有关什么行动用户在该项目上执行(添加,编辑,删除,移动)。页面被发送到服务器的唯一情况是,当用户完成后,提交页面序列化所有关于那个是在隐藏字段在页面上制作成JSON和存储它的变化信息的权利之前。

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.

我正在寻找的是如何在C#中打造出了我的班了一些一般性建议。我想这可能是不错的在C#中的类相匹配的JavaScript的一个,这样我就可以deserealize的JSON这一类的实例。这似乎有点奇怪,虽然有在服务器端,这两个直接复制的JavaScript类,只存在支持JavaScript的用户界面实现类。

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.

的描述可能是一个JavaScript源文件;你可以建立产生从JS的apropriate C#代码分析器。或者,它可能是一个C#源文件,并且你做的相反。

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.

您可能会在描述它RelaxNG,然后建立(或发现)发现更多的实用程序发电机为C#和Javascript。在这种情况下,RelaxNG模式将被签入到源代码控制,并且生成的构件不会。

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.

修改:另外有一个叫新生规范的,我认为这将有助于在这方面也是如此。我还没有评估WADL。外周,我知道它并没有席卷世界,但我不知道为什么是这样。 的问题。

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:由于缺乏工具(WADL显然是死胎),如果我是你,我可能会尝试这种战术的方法:

Given the lack of tools (WADL is apparently stillborn), if I were you I might try this tactical approach:


  • 使用 [DataContract] 属性在c#类型和治疗那些为明确。

  • 构建一个工具,吸食你的C#类型,从编译的程序集和实例的类型,使用JsonSerializer上一个示例XML JSON文件,它提供,一种事实上的的对象模型定义。该工具应该以某种方式验证实例化的类型可以往返成等价的JSON,也许与产生的东西校验或CRC。

  • 运行该工具作为构建过程的一部分。

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

要做到这一点,你必须检查在样品JSON文件为源代码,你也不得不要确保的的是你使用在你的应用程序的各种JS代码的形式。由于Javascript是动态的,你可能还需要一个类型验证什么的,这将运行为的JSLint或其他一些构建时的验证步骤的一部分,这将检查你的JavaScript源地看到,它正在使用您的标准objbect模型定义。

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.

这篇关于保持JavaScript和C#的对象模型之间的一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:46