Bra*_*don 5 c# wcf dynamic-data odata
我有一个简单的小数据模型,类似于以下内容:
InventoryContext {
IEnumerable<Computer> GetComputers()
IEnumerable<Printer> GetPrinters()
}
电脑 {
public string ComputerName { get; set; }
public string Location { get; set; }
}
打印机 {
public string PrinterName { get; set; }
public string Location { get; set; }
}
结果来自非SQL源,因此该数据不是来自连接到数据库的Entity Framework.
现在我想通过WCF OData服务公开数据.到目前为止,我发现这样做的唯一方法是根据此博客教程创建自己的数据服务查询提供程序:
http://blogs.msdn.com/alexj/archive/2010/01/04/creating-a-data-service-provider-part-1-intro.aspx
......这很棒,但似乎是一项非常复杂的事业.提供者的代码将比我的整个数据模型长4倍,以生成所有资源集和属性定义.
在Entity Framework之间是否存在类似通用提供程序并从零编写自己的数据源?也许某种方式来构建对象数据源或其他东西,以便神奇的WCF独角兽能够获取我的数据并骑行到日落而无需明确编码提供者?
您可以使用内置的Reflection Provider。
将以下内容添加到您的 InventoryContext 中:
IQueryable<Computer> Computers { get { return GetComputers().AsQueryable(); } }
IQueryable<Printer> Printers { get { return GetPrinters().AsQueryable(); } }
Run Code Online (Sandbox Code Playgroud)
并按如下方式修改实体(您需要添加对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; } }
}
Run Code Online (Sandbox Code Playgroud)
完成此操作后,只需将数据服务指向 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这应该就是您需要做的全部。InventoryContext 需要有一个无参数构造函数。
| 归档时间: |
|
| 查看次数: |
3513 次 |
| 最近记录: |