在 WCF 服务中创建私有数据成员

发布于 2025-01-07 12:04:29 字数 75 浏览 0 评论 0原文

在 WCF 服务中创建私有数据成员是一个好主意吗?如果这是一个好做法,那么我们何时/何处初始化这些成员变量,因为客户端仅调用服务的方法。

Is it a good idea to create private data members in WCF service and if it is a good practice then when/where we initilize these member variable as the client is only calling methods of the service.

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

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

发布评论

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

评论(2

冬天旳寂寞 2025-01-14 12:04:29

仅将您的数据契约用作 DTO,并在处理数据的代码中扩展它们。

像这样的事情:

[DataContract]
public class WCFDataClass
{
    [DataMember]
    public String Foo { get; set; }
}

// Your class, used for internal processing
class MyWCFDataClass : WCFDataClass
{
    public String MyFoo { get; set; }

    public String DoFoo()
    {
        return this.Foo + this.MyFoo;
    }
}

Use your data contracts merely as DTO's, and extend these in the code that does the processing of the data.

Something like this:

[DataContract]
public class WCFDataClass
{
    [DataMember]
    public String Foo { get; set; }
}

// Your class, used for internal processing
class MyWCFDataClass : WCFDataClass
{
    public String MyFoo { get; set; }

    public String DoFoo()
    {
        return this.Foo + this.MyFoo;
    }
}
难理解 2025-01-14 12:04:29

如果您对互操作性感兴趣,我认为这通常不是一个好的做法。

首先创建一个契约(操作契约、消息契约、数据契约等),以明确的方式指定支持什么和不支持什么。它公开明确地指定了这些事情。当您开始声明私人成员是公共合同的一部分时,很快就会变得非常混乱。对于追随你的程序员来说,辨别正在发生的事情是有问题的。

您可能试图将逻辑封装在数据契约中,这不是数据契约的目的。正如 @CodeCaster 建议的那样,此类操作应在数据协定类之外执行。即使是像构造函数填充默认值这样简单的事情也可能会出现问题。此类逻辑也应该在 DataContract 类之外执行。

此外,当您将私有成员声明为 [DataMember] 时,您依赖于数据协定序列化程序的非标准实现细节 - 它可以读取/写入非公共成员的事实- 即 DataConstractSerializer(至少在 .NET 平台上)可以完成您在自己的代码中无法完成的事情。你依赖的是“魔法”。虽然这种“魔力”有助于 DataConstractSerializer 提供令人惊叹的性能,但我认为您不应该依赖其实现细节。例如,您会发现此类 DataContract 类无法与 Silverlight 应用程序共享,因为在该平台上 DataConstractSerializer 无法读取/写入非公共成员。

然而,就像所有的“实践”一样,你必须考虑你的具体情况。如果互操作性和可维护性不是必需的,那么上述大部分内容都是不相关的并且可以忽略。 :)

If you have any interest in interoperability, I dont't believe it is generally a good practice.

First a contract (operation contract, message contract, data contract , etc.) is created to specify, in an unambiguous way, what is supported and what is not. It explicitly specifies those things publicly. It gets very confusing, very quickly, when you start declaring private members to be part of a public contract. It becomes problematic for the programmer who comes after you to discern what is going on.

You are likely attempting to encapsulate logic in your data contracts, which is not the purpose of a data contract. As suggested by @CodeCaster, such manipulation should be performed outside the data contract class. Even simple things like constructors populating default values can be problematic. Such logic should also be performed outside the DataContract class.

In addition, when you declare private members to be [DataMember]s you are relying on a non-standard implementation detail of the data contract serialiser - the fact that it can read/write members that are not public - i.e. the DataConstractSerializer (at least on the .NET platform) can do things you couldn't do in your own code. You are depending on 'magic'. While this 'magic' helps DataConstractSerializer to deliver amazing performance, I don't think you should be depending on its implementation details. For example, you will find that such DataContract classes cannot be shared with Silverlight applications because on that platform the DataConstractSerializer cannot read/write non-public members.

However, like all 'practices', you have to consider your circumstances. If interoperability and maintainability are not a requirement, then most of the above is not relevant and can be disregarded. :)

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