是否有更简单的方法来创建 WCF/OData 数据服务查询提供程序?
我有一个简单的小数据模型,如下所示:
InventoryContext {
IEnumerable<计算机>; GetComputers()
IEnumerable<打印机>; GetPrinters()
}
计算机 {
公共字符串计算机名 { get;放; }
公共字符串位置{ get;放; }
}
打印机 {
公共字符串 PrinterName { get;放; }
公共字符串位置{ get;放; 。
结果
来自非 SQL 源,因此该数据不是来自连接到数据库的实体框架
现在我想通过 WCF OData 服务公开数据。到目前为止,我发现做到这一点的唯一方法是根据此博客教程创建自己的数据服务查询提供程序:
http://blogs.msdn.com/alexj/archive/2010/01/04/creating-a- data-service-provider-part-1-intro.aspx
...这很棒,但似乎是一项非常复杂的任务。提供程序的代码将比我的整个数据模型长 4 倍,用于生成所有资源集和属性定义。
在实体框架和从零开始编写自己的数据源之间是否有类似通用提供程序的东西?也许有某种方法可以构建对象数据源或其他东西,以便神奇的 WCF 独角兽可以获取我的数据并在日落时消失,而无需显式地对提供程序进行编码?
I have a simple little data model resembling the following:
InventoryContext
{
IEnumerable<Computer> GetComputers()
IEnumerable<Printer> GetPrinters()
}
Computer
{
public string ComputerName { get; set; }
public string Location { get; set; }
}
Printer
{
public string PrinterName { get; set; }
public string Location { get; set; }
}
The results come from a non-SQL source, so this data does not come from Entity Framework connected up to a database.
Now I want to expose the data through a WCF OData service. The only way I've found to do that thus far is creating my own Data Service Query Provider, per this blog tutorial:
http://blogs.msdn.com/alexj/archive/2010/01/04/creating-a-data-service-provider-part-1-intro.aspx
... which is great, but seems like a pretty involved undertaking. The code for the provider would be 4 times longer than my whole data model to generate all of the resource sets and property definitions.
Is there something like a generic provider in between Entity Framework and writing your own data source from zero? Maybe some way to build an object data source or something, so that the magical WCF unicorns can pick up my data and ride off into the sunset without having to explicitly code the provider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用所谓的“反射提供程序”。这假设您有一个返回 IQueryable 的属性(或多个属性)(T 是您的实体类型)。
请观看此视频,了解简单的“操作方法”以帮助您入门。
http://msdn.microsoft.com/en-us/data/cc745968。 ASPX
You can use so called "reflection provider". This assumes that you have a property (or many properties) which returns IQueryable (T being your entity type).
Take a look at this video for a simple "How to" to get you started.
http://msdn.microsoft.com/en-us/data/cc745968.aspx
您可以使用内置的反射提供程序。
将以下内容添加到您的 InventoryContext:
并按如下方式修改实体(您需要将对
System.Data.Services.Client
的引用添加到您的项目中):完成此操作后,只需将数据服务指向 InventoryContext,如下所示:
这应该就是您需要做的全部事情。 InventoryContext 需要有一个无参数构造函数。
You can use the built in Reflection Provider.
Add the following to your InventoryContext:
And modify the entities as follows (you'll need to add a reference to
System.Data.Services.Client
to your project):Once you've done that, just point your data service at your InventoryContext, like this:
That should be all you need to do. The InventoryContext will need to have a parameterless constructor.
您可以使用 WCF 数据服务工具包。
它最初是由 Microsoft 的一群人编写的,允许以非常灵活的方式创建不基于实体框架的 OData 服务。
Jonathon Carter 去年在 mix 上做了一个出色的演示,围绕 Mongo DB 封装了 OData 服务。
http://channel9.msdn.com/events/MIX/MIX11/FRM16
该代码也可以在 codeplex http://wcfdstoolkit.codeplex.com/ 上找到,
希望这会有所帮助。
You can use the WCF Data Services toolkit.
It was written originally by a group of guys from Microsoft and allows a very flexible way of creating OData services that are not based on Entity Framework.
Jonathon Carter did an excellent demo at mix last year wrapping an OData service around Mongo DB.
http://channel9.msdn.com/events/MIX/MIX11/FRM16
The code is also available on codeplex http://wcfdstoolkit.codeplex.com/
Hope this helps.