微风本地查询枚举类型 [英] Breeze Local Query for EnumType

查看:199
本文介绍了微风本地查询枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public Object Lookups()
{
var duration = Enum.GetNames(typeof(Duration));
var status = Enum.GetNames(typeof(Status));
返回新的{持续时间,状态};
}

然后我用breeze

$ b查询服务器
$ b

 返回entityQuery.from('Lookups')
.using(manager).execute()
.then(querySucceeded,_queryFailed );

函数querySucceeded(data){

console.log(检索[查找]+数据);
返回true;
}

现在稍后我想从缓存中获取这些查找,请注意,元数据我的枚举居住在EnumType下,看起来像微风没有提供支持从服务器端检索枚举,所以我作为一个查询返回它们。现在,我想知道如何让他们在本地减少额外的往返?



谢谢

解决方案

.NET枚举是常规Web API支持的。他们的类型名称出现在元数据中,并被Breeze识别。



我不认为枚举值会自动传播给客户端(这样很好),但是您可以通过其他方式将它们发送给客户端,例如使用查找端点。


Web API OData 不支持枚举。事实上,如果一个属性返回一个枚举,而WCF数据服务是内容来处理它们的字符串名称,那就死了。目前我们无法就Web API OData的这个缺陷做出任何处理。


枚举不是实体,我不认为你想要将它们伪造成客户端上的实体。你不可以不会。



由于Breeze不是实体,Breeze不会将它们存储在缓存中。您的查询返回JavaScript简单对象,而不是实体,并且没有可以找到它们的缓存查询。



我认为在你的情况下我会做的是捕获这些枚举值作为 datacontext 服务的数组属性...与您正在使用的相同的服务(我希望)封装了您的基于Breeze的数据访问活动,以便ViewModels与 datacontext 而不是直接使用Breeze组件。



因此,查询查询成功回调可能如下所示:

  //在datacontext服务内
var isReady = null;
var dc = {
ready:ready,
持续时间:[],
状态:[],
...
}

return dc;
/////////////
function ready(){
return isReady || (isReady = getReady())); //返回准备好的承诺
}

函数getReady(){
//你做的主要事项
//返回一个true 当您完成
//准备异步任务,如获取查找
...
}
...
函数lookupsQuerySucceeded(data){
console.log(Retrieved [Lookups]+ data);
dc.Duration = data.results.Duration;
dc.Status = data.results.Status;
返回true;
}

我正在删除细节,但我希望你能得到这个想法。 p>

只有一次访问服务器,使用这种技术,单次访问以获取初始的东西并填充缓存和/或(在这种情况下)属性的code> datacontext 本身。


I setup WebApi method for Lookups which return

 public Object Lookups()
 {
    var durations = Enum.GetNames(typeof(Duration));
    var status = Enum.GetNames(typeof(Status));
    return new { durations, status };
 }

Then I make a query to the server with breeze

 return entityQuery.from('Lookups')
         .using(manager).execute()
         .then(querySucceeded, _queryFailed);

    function querySucceeded(data) {

        console.log("Retrieving [Lookups] " + data);
        return true;
   }

Now later on I want to get those lookups from cache, please note that in metadata my Enums reside under "EnumType", It looks like breeze not provide support yet to retrieve enums from server side so I returning them as a Lookups. Now, I am wondering how can I get them locally to reduce extra round trip?

Thanks

解决方案

.NET Enums are supported with the regular Web API. Their type names appear in metadata and are recognized by Breeze.

I don't think the enum values are propagated to the client automatically (that would be nice) but you can send them to the client by other means such as you are doing with a lookups endpoint.

The Web API OData does not support enums yet either. In fact, it dies if a property returns an enum whereas WCF Data Services is content to deal with their string names. Nothing we can do about this defect of Web API OData at the moment.

Enums are not entities and I don't think you want to fake them as entities on the client. You could but I wouldn't.

Because they are not entities, Breeze won't store them in cache. Your query returns JavaScript simple objects, not entities, and there is no cache query that could find them.

I think what I would do in your situation is capture these enum values as array properties of a datacontext service ... the same service that your are using (I hope) to encapsulate your Breeze-based data access activities so that ViewModels interact with the datacontext rather than directly with Breeze components.

Accordingly, your lookups query success callback might look like this:

// Inside the datacontext service
var isReady = null;
var dc = {
     ready: ready,
     Duration: [],
     Status: [],
     ...
}

return dc;
///////////////
function ready() {
   return isReady || (isReady = getReady())); // returns the ready promise
}

function getReady() {
   // stuff you do to prime the pump
   // returns a promise that is "true" when you've completed your
   // preparatory async tasks such as fetching lookups
   ...
}
...
function lookupsQuerySucceeded(data) {
    console.log("Retrieved [Lookups] " + data);
    dc.Duration = data.results.Duration;
    dc.Status = data.results.Status;
    return true;
}

I'm leaving out details but I hope you get the idea.

There is only one trip to the server with this technique, a single visit to get the initial stuff and populate the cache and/or (in this case) properties of the datacontext itself.

这篇关于微风本地查询枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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