【问题标题】:Breeze Json Serialization using WebAPI 2使用 WebAPI 2 进行微风 Json 序列化
【发布时间】:2014-07-24 02:18:05
【问题描述】:

我对 Breeze 还是很陌生,正在开发一个项目,即客户端的 Breeze 和服务器端的 ASP.NET Web API 2。我采用了 Breeze 网站 (http://www.breezejs.com/documentation/web-api-controller) 上列出的所有最佳实践。

但我有一个问题是为什么每个数据节点在项目被序列化时都列出对象的 .NET 类型。每当我得到一个包含大量记录的列表时,我可能会看到大部分数据是 $type 实体,列出了完整的类命名空间和程序集名称。这个 $type 实体对每个数据节点重复,代码是 100 次重复。

似乎更合乎逻辑并且节省了轻量级调用的有效负载来执行类似于名称空间在服务器端的 Xml 序列化中所做的事情。可能在标题中列出完整的类签名,而对象只有在标题中完整定义的快捷方式。如:

标题:

$typeFull: {
key: 'ABC',
namespace: 'ABCCompany.Data.Client.ViewModels.Customers'

数据:

$id: 1, $typeKey: 'abc',

下面是当前返回给微风的数据示例:

[
{
$id: "1",
$type: "Assembly.Root, ABCCompany.Data.Client.ViewModels.Customers",
CustomerId: 1,
Name: "ABC Company",
LongDescription: "Describe me! Describe me! Describe me! Describe me! Describe me! Describe me! Describe me! Describe me! Describe me!  Describe me! Describe me! Describe me! Describe me! Describe me! Describe me! Describe me! Describe me!",
Phone: "800-555-1212",
PrimaryContactName: "Bugs Bunny",
AddressId: 1,
Address: {
$id: "2",
$type: "Assembly.Location, ABCCompany.Data.Client.ViewModels.Customers.Addresses",
AddressLine1: "2600 Red Box Blvd",
AddressLine2: null,
City: "Anaheim",
StateId: 35,
Zip: "44122",
CountryCode: "US",

【问题讨论】:

    标签: c# json asp.net-web-api breeze


    【解决方案1】:

    我不在微风开发团队,所以我无法为他们回答,但这是我的 2 美分:

    实际上,冗余信息并不重要,因为数据将被压缩。以下是确保 json 被压缩的方法(假设您使用的是 IIS):

    http://benfoster.io/blog/aspnet-web-api-compression

    【讨论】:

    • 不喜欢依靠压缩来弥补缺点的想法。当这不是默认做法时,为什么说数据“将被压缩”?在进行该设置之前,它不会被压缩
    • 再说一次,我的观点是:我不认为这是一个缺点。考虑您示例中 json 中的标签:“CustomerId”、“AddressLine1”等。当您有“100 次重复”时,这些标签会重复。在实践中,它们至少占数据的 50%。如果这是 xml,则百分比会更高。 Json 和 xml 每天都在使用,理解压缩将平衡格式的描述性。压缩是必要的,微风类型信息无关紧要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多