WCF DataService,如何避免 POCO 中的 [DataServiceKey]

发布于 2024-10-11 04:57:49 字数 193 浏览 7 评论 0原文

我使用 WCF DataService 并想使用 POCO。 我必须指定一个 DataServiceKey 才能使其工作(显然)。 当我这样做时,我必须引用 System.Data.Services.Client (对于 System.Data.Services.Common) - 这感觉不太 POCO。

有没有办法保持我的对象干净并在其他地方指定密钥?

I use a WCF DataService and want to use POCOs.
I have to specify a DataServiceKey in order for it to work (obviously).
When I do that I have to reference System.Data.Services.Client (for System.Data.Services.Common) - which doesn't feel very POCO.

Is there a way to keep my objects clean and specify the Key somewhere else?

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

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

发布评论

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

评论(1

春花秋月 2024-10-18 04:57:49

如果您使用反射提供程序并且您的类不遵循关键属性的约定,则必须使用 DataServiceKey 属性。
如果您只是向 DataService 提供类定义和上下文类,那么您就会得到反射提供程序。因此,如果您不实现 IDataServiceMetadataProvider,您很可能会使用反射提供程序。
可以使用没有类属性的反射提供程序,但随后 WCF 数据服务会应用启发式方法来找出关键属性。它是这样的:

  • 如果相关类有一个
    属性称为 ID,它是一个实体
    将 ID 作为唯一的关​​键属性。
  • 例如,如果调用该类
    客户及其拥有一个名为
    CustomerID,它是一个实体
    CustomerID 属性作为唯一键
    属性(类的名称是
    显然就像样本一样)。

如果没有 DataServiceKey 属性,则任何其他属性都不会被识别为关键属性。此博客中也对此进行了描述:http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using -the-reflection-provider.aspx

可以使用具有任意关键属性的 100% POCO 类,但随后您必须实现自定义提供程序。这是相当多的工作,因为您必须以编程方式定义类的形状。可以在此处找到示例自定义提供程序演练: http://blogs.msdn.com/b/alexj/archive/2010/01/07/data-service-providers-getting-started.aspx

If you're using a reflection provider and your classes don't follow a convention for key properties, then you have to use the DataServiceKey attribute.
Reflection provider is the one you get if you simply provide class definitions and context class to the DataService. So if you don't implement IDataServiceMetadataProvider, you're very likely using a reflection provider.
It is possible to use reflection provider without the attributes on your classes, but then the WCF Data Services applies a heuristics to figure out the key properties. It goes like this:

  • if the class in question has a
    property called ID, it's an entity
    with the ID as the only key property.
  • if the class is called for example
    Customer and it has a property called
    CustomerID, it's an entity with the
    CustomerID property as the only key
    property (the name of the class is
    obviously just as sample).

No other properties are recognized as key properties without the DataServiceKey attribute. This is also described for example in this blog: http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using-the-reflection-provider.aspx

It is possible to use 100% POCO classes with arbitrary key properties, but then you would have to implement a custom provider. This is considerably more work since you have to define the shape of your classes programatically. A sample custom provider walkthrough can be found here: http://blogs.msdn.com/b/alexj/archive/2010/01/07/data-service-providers-getting-started.aspx

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