如何在不使用EF的情况下向我的实体添加上下文元数据? [英] How can I add context metadata to my entities without using EF?

查看:46
本文介绍了如何在不使用EF的情况下向我的实体添加上下文元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一个架构,不幸的是,我们将SharePoint 2013有效地用作我们的主要数据库。 (不是我的选择,以防万一您没有捡到)。我在服务器上有一个Asp.Net MVC外观应用程序,处理来自SP和其他几个数据源的数据组成,然后处理JavaScript SPA作为客户端。另外一个麻烦是客户端需要能够脱机工作,因此我需要使用IndexedDB来存储数据以供脱机访问。

I have to implement an architecture where, unfortunately, we are using SharePoint 2013 as, effectively, our principle database. (Not my choice, in case you hadn't picked that up). I have an Asp.Net MVC facade application on the server, handling composition of data from SP and a couple of other data sources, and then a JavaScript SPA as client. An additional wrinkle is that the client needs to be able to work offline, so I need to use IndexedDB to store the data for offline access.

这似乎是breeze.js的完美用例。我的基本体系结构是在MVC外观中定义一个强类型化的模型,该模型将包装从SP中获得的未类型化数据(形式为object [ property]-使用SP客户端对象模型)。 Breeze将处理该模型与客户端之间的同步,我将使用导出/导入功能根据需要在IndexedDB中缓存数据。

This seems a perfect use-case for breeze.js. My basic architecture is to define a strongly typed model in the MVC facade that will wrap the untyped data I get from SP (in the form object["property"] - using the SP Client Side Object Model). Breeze will handle synchronization between this model and the client, and I will use the export/import functionality to cache data in IndexedDB as required.

到目前为止,一切都很好。但是...微风站点上的SOA示例仍在开发中(对我而言,这基本上是SOA体系结构,每个SP列表都是要组成的服务)。我能找到的最接近的东西是 NoDB 示例,但这是在客户端上对元数据进行硬编码的示例。我想在MVC模型中建立关系和验证,然后将它们传递给客户端,以便验证可以在两个地方运行相同的声明。

So far so good. But... the SOA examples on the breeze site are still under development (and to me, this is fundamentally an SOA architecture, with each SP List a service to be composed). The closest thing I can find is the NoDB sample but this hard-codes metadata on the client. I'd like to establish relationships and validation in the MVC model, and then pass these through to the client, so validation can run off the same declaration in both places.

这可能吗?如果是这样-如何?如果没有,没有人有解决方法或更好的主意吗?我已经辞职了,打算在两个不同的地方(正面和隐式地SP列表的结构)定义模型。我非常想避免在客户端中再次实施它。我愿意让breeze.js直接与SP REST端点对话,但是我对 https://stackoverflow.com/a的理解/ 15364503/1014822 的实现是有缺陷的,并且没有公开所需的元数据。

Is this possible? If so - how? If not does anyone have a workaround or a better idea? I'm already resigned to defining the model in two separate places (the facade and, implicitly, the structure of the SP lists). I would dearly like to avoid implementing it a third time in the client. I'm open to having breeze.js talk directly to the SP REST endpoints, but my understanding from https://stackoverflow.com/a/15364503/1014822 is that the implementation is flawed and does not expose the required metadata.

分辨率:基于公认的下面的答案,我得出以下解决方案:

Resolution: Based on the accepted answer below, I came to the following solution:

1)从SP ListData.svc端点生成服务引用-从而创建edmx和代理类。

1) Generate a service reference from the SP ListData.svc endpoint - thus creating an edmx and proxy classes.

2)在我的存储库中扩展ContextProvider并覆盖 BuildJsonMetadata 像这样:

2) Extend ContextProvider in my Repository and override BuildJsonMetadata like so:

protected override string BuildJsonMetadata()
{
    XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath("PATH_TO_EDMX"));
    String xString = xDoc.ToString();
    xString = xString.Replace("DATA_SERVICE_NAMESPACE", "APP_NAMESPACE");
    xDoc = XDocument.Parse(xString);
    var jsonText = CsdlToJson(xDoc);
    return jsonText;
}

3)稍微修改breeze.js,编辑第12383行:

3) Modify breeze.js very slightly, editing line 12383:

var schema = metadata.schema || metadata["edmx:Edmx"]["edmx:DataServices"].schema;

(我也可以在ContextProvider中通过选择后代而不是根节点来解决此问题我的xDoc)

(I could presumably also have fixed this in the ContextProvider by choosing a descendant rather than the root node for my xDoc)

4)-(可选)使用@Christoff非常有用的 T4TS.tt 模板脚本从服务代理类生成d.ts,这样我就可以对微风加载的数据进行类型安全性。

4) - Optionally use @Christoff's very useful T4TS.tt template script to generate a d.ts from the service proxy classes so I can have type safety on the data that breeze loads.

所以到目前为止,使用此设置效果很好-我可以使用SP作为数据源支持带有元数据的基本CRUD。

So far so good with this setup - I can perform basic CRUD with metadata, backed by SP as a data source.

推荐答案

v 1.2.7 ,我们已经记录了Breeze的元数据架构,并且Breeze现在将遵守从您的Web服务返回的遵循此架构的json对象。

As of v 1.2.7, We have documented Breeze's metadata schema, and json objects that adhere to this schema that are returned from your webservice will now be honored by Breeze.

---下面的上一个帖子

--- previous post below

我们正在记录如何在下周左右公开任意服务器端元数据,此后不久通过一些例子f如何使用任意Web服务。

We are in the process of documenting how to expose arbitrary server side metadata over the next week or so, followed soon thereafter by some examples of how to consume an arbitrary web service. There are a few small code changes involved as well.

目前,直到这些文档完成,最好的解决方法是在客户端上创建元数据并使用一个jsonResultsAdapter,将您的服务调用的结果调整为实体。您在客户端上创建的元数据将与最终将在服务器上创建并向下发送到客户端的元数据完全相同。

For the time being, until these docs are complete, the best workaround is to create your metadata on the client and use a jsonResultsAdapter to shape the results of your service call into "entities". The metadata you create on the client will be exactly the same as the metadata that you will eventually be creating on the server and sending down to the client.

希望这会有所帮助。

这篇关于如何在不使用EF的情况下向我的实体添加上下文元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆