是否有更简单的方法来创建 WCF/OData 数据服务查询提供程序?

发布于 2024-09-03 08:25:57 字数 995 浏览 5 评论 0原文

我有一个简单的小数据模型,如下所示:

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 技术交流群。

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

发布评论

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

评论(3

缺⑴份安定 2024-09-10 08:25:57

您可以使用所谓的“反射提供程序”。这假设您有一个返回 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

新雨望断虹 2024-09-10 08:25:57

您可以使用内置的反射提供程序

将以下内容添加到您的 InventoryContext:

IQueryable<Computer> Computers { get { return GetComputers().AsQueryable(); } }
IQueryable<Printer> Printers { get { return GetPrinters().AsQueryable(); } }

并按如下方式修改实体(您需要将对 System.Data.Services.Client 的引用添加到您的项目中):

using System.Data.Services.Common;

[DataServiceKey("ComputerName")]
public class Computer 
{
    public string ComputerName { get; set; }
    public string Location { get; set; } }
}

[DataServiceKey("PrinterName")]
public class Printer
{
    public string PrinterName { get; set; }
    public string Location { get; set; } }
}

完成此操作后,只需将数据服务指向 InventoryContext,如下所示:

public InventoryDataService : DataService<InventoryContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;         
    }
}

这应该就是您需要做的全部事情。 InventoryContext 需要有一个无参数构造函数。

You can use the built in Reflection Provider.

Add the following to your InventoryContext:

IQueryable<Computer> Computers { get { return GetComputers().AsQueryable(); } }
IQueryable<Printer> Printers { get { return GetPrinters().AsQueryable(); } }

And modify the entities as follows (you'll need to add a reference to System.Data.Services.Client to your project):

using System.Data.Services.Common;

[DataServiceKey("ComputerName")]
public class Computer 
{
    public string ComputerName { get; set; }
    public string Location { get; set; } }
}

[DataServiceKey("PrinterName")]
public class Printer
{
    public string PrinterName { get; set; }
    public string Location { get; set; } }
}

Once you've done that, just point your data service at your InventoryContext, like this:

public InventoryDataService : DataService<InventoryContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;         
    }
}

That should be all you need to do. The InventoryContext will need to have a parameterless constructor.

早乙女 2024-09-10 08:25:57

您可以使用 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.

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