WCF DataService,如何避免 POCO 中的 [DataServiceKey]
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用反射提供程序并且您的类不遵循关键属性的约定,则必须使用 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:
property called ID, it's an entity
with the ID as the only key property.
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