是否将用户定义的类定义为 DataContract

发布于 2025-01-07 11:30:09 字数 1013 浏览 0 评论 0原文

我想使用 AJAX 调用来使用 WCF REST 服务。

假设我有以下内容,其中 Person 是用户定义的类:

[ServiceContract]
public interface IPerson
{
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]   
    [OperationContract]
    Person GetPerson();
}

Person 定义为 DataContract 与我这样做有何区别没有将 Person 定义为 DataContract

[DataContract]
public class Person
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

在客户端,无论 Person 是否定义为 DataContract ,我总是可以使用以下内容,对吧?

<script type="text/javascript">
    $().ready(function() {
        $("#Button1").click(function() {
            $.getJSON("<url of the service>/GetPerson", CallBackMethod);
        });
    });

    function CallBackMethod(result) {
        alert(result.FirstName);
        alert(result.LastName);
    }
</script>

I want to consume a WCF REST service using an AJAX call.

Suppose I have the following where Person is a user-defined class:

[ServiceContract]
public interface IPerson
{
    [WebInvoke(ResponseFormat = WebMessageFormat.Json)]   
    [OperationContract]
    Person GetPerson();
}

What are the differences between defining a Person as a DataContract and if I do not define Person as a DataContract?

[DataContract]
public class Person
{
    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }
}

On the client side I can always use the following no matter whether Person is defined as DataContract or not, right?

<script type="text/javascript">
    $().ready(function() {
        $("#Button1").click(function() {
            $.getJSON("<url of the service>/GetPerson", CallBackMethod);
        });
    });

    function CallBackMethod(result) {
        alert(result.FirstName);
        alert(result.LastName);
    }
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2025-01-14 11:30:09

数据契约只是 .NET Framework 支持的一种序列化形式;它们本质上是 WCF 之前的 XML 序列化的替代品。这一切都是通过 DataContractSerializer 完成的,它负责将对象转换为与数据协定匹配的序列化 XML。这是 WCF 和 ASP.NET JSON 代码等使用的序列化器类。

理论上,您应该使用 DataContract 和 DataMember 属性来标记您的合约对象,以明确它们的用途。实际上,DataContractSerializer 将从您提供的任何自定义类型中创建数据协定;如果您忽略 DataContract 和 DataMember 属性,它会为您推断它们。添加它们可以让您更好地控制序列化的工作方式,与添加 XML 序列化属性的方式完全相同。如果缺乏显式属性,对象中的所有公共字段都会成为数据契约的一部分。

因此,您问题的真正答案是,Person总是定义为 DataContract,至少就序列化器而言是如此;将属性添加到您的类及其所有公共成员中将产生相同的效果。 IMO,没有充分的理由这样做:这会花费你几毫秒的打字时间,如果你不这样做,运行时就会假装你已经这样做了。

Data contracts are just one form of serialization that the .NET Framework supports; they are essentially the replacement for pre-WCF XML Serialization. This is all done through the DataContractSerializer, which is responsible for turning your object into serialized XML that matches the data contract. This is the serializer class that is used by WCF and the ASP.NET JSON code, among other pleaces.

In theory, you should tag your contract objects with the DataContract and DataMember attributes to make clear what they are for. In practice, the DataContractSerializer will make a data contract out of any custom type you give it; it will infer the DataContract and DataMember attributes for you if you leave them off. Adding them gives you more control over how the serialization works, in exactly the same way that adding XML serialization attributes did. Lacking the explicit attributes, all public fields in your object become part of the data contract.

So, the real answer to your question is that Person is always defined as a DataContract, at least as far as the serializer is concerned; adding the attributes to your class and all of its public members will produce the same effect. IMO, there's no good reason not to do so: it costs you a few milliseconds of typing, and if you don't the runtime will just pretend you did anyway.

夢归不見 2025-01-14 11:30:09

首先,在某些情况下,一个类型可能实现多个编程模型。例如,它可以既是 DataContract 又是 IXmlSerialized,或者它可以既是 CollectionDataContract 又是 IXmlSerialized。数据契约编程模型(不仅仅是 DataContractAttribute)规定了如何序列化此类类型。请参阅这篇精彩的博客文章 了解其工作原理的详细信息。

为了回答您的具体问题,在 .NET 3.5 SP1 之前,您无法序列化“POCO”类型(普通的旧 C# 类型),除非它们用 DataContractAttribute 修饰。然而,一旦引入 POCO 支持,就没有什么区别了。请参阅 此博文了解详细信息。

希望这有帮助!

First of all, in some cases, a type may implement more than one programming model. For example, it could be both a DataContract and an IXmlSerializable, or it could be both a CollectionDataContract and an IXmlSerializable. The Data Contract programming model -- which goes beyond just DataContractAttribute -- dictates how such types should be serialized. Please see the awesome blog post here for details on how this works.

To answer your specific question, until .NET 3.5 SP1, you could NOT serialize "POCO" types (plain old C# types) unless they were decorated with DataContractAttribute. Once POCO support was introduced, however, it made no difference. See this blog post for details.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文