Breezejs SaveChanges:是否可以以某种方式返回自定义的SaveResult对象? [英] Breezejs SaveChanges: is it possible to return a custom SaveResult object, somehow?
问题描述
说我有一个电影实体,平均得分。用户可以对电影进行评分,为此我在客户端上调用datacontext.savechanges,将评分对象发送到服务器。在服务器上,调用SaveChanges方法,然后在BeforeSaveEntity方法中,调整电影的平均得分。
Say I have a movie entity, with an average score. A user can rate a movie, and for that I call datacontext.savechanges on the client, sending a Rating object to the server. On the server, the SaveChanges method is called, and in the BeforeSaveEntity method, I adapt the movie's average score.
这里的问题是:如何从服务器的SaveChanges方法,例如在SaveResult对象内部?
Here's the question: how to return that average score from the server's SaveChanges method, for example inside the SaveResult object?
我以为可以将电影实体添加到SaveResult Entities列表中,但是:
-我会需要从saveBundle参数
中访问属性-我将不得不重新查询数据库,就像我在BeforeSaveEntity
I thought I could add the movie entity to the SaveResult Entities list, but then: - I would need to access attributes from within the saveBundle parameter - I would have to requery the DB, which I just did in BeforeSaveEntity
谢谢
尼古拉斯
推荐答案
是的,有可能。
像平常一样将控制器写成EDMX。对于我们来说,它翻译成这样的东西:
Write your controller against an EDMX as you would do normally do. For us, it translates into something like this:
public class PersonalizationController : MultiTenantBreezeController<PersonalizationEntities>
其中PersonalizationEntities是ObjectContext。
where the PersonalizationEntities is an ObjectContext.
然后在服务器上,我们只需定义SaveChanges(不要介意重写,我们有一个基类)
Then on the server, we simply define the SaveChanges (do not mind the override, we do have a base class)
[HttpPost]
public override SaveResult SaveChanges(JObject saveBundle)
{
// Deserialize the object that needs to get saved (ApplicationDefaults is my DTO)
var applicationDefaultsList = JsonConvert.DeserializeObject<List<ApplicationDefaults>>(saveBundle.SelectToken("entities").ToString());
// Do whatever logic you need to save the data
using (var repo = ServiceLocator.Current.Container.Resolve<IUserPreferenceRepository>())
{
// Your save logic here
}
// Construct the save result to inform the client that the server has completed the save operation
var keyMappings = new List<KeyMapping>();
return new SaveResult()
{
Entities = applicationDefaultsList.Cast<object>().ToList(),
Errors = null,
KeyMappings = keyMappings
};
}
这篇关于Breezejs SaveChanges:是否可以以某种方式返回自定义的SaveResult对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!