Breezejs SaveChanges: is it possible to return a custom SaveResult object, somehow?

荒凉一梦 提交于 2020-01-06 03:49:27

问题


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.

Here's the question: how to return that average score from the server's SaveChanges method, for example inside the SaveResult object?

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

Thanks

Nicolas


回答1:


As pawel pointed out in the comments: To return the movie in the SaveChanges promise, update the movie in your BeforeSaveEntities method on your custom EFContextProvider and add it to the saveMap.

I've put together some code for you.

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type,   List<EntityInfo>> saveMap) {
    Movie movie = null;
    // initialize the movie variable and update the movie as needed
    saveMap.Add(typeof(Movie), movie);

    return saveMap;
  }



回答2:


Yes, it is possible.

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>

where the PersonalizationEntities is an ObjectContext.

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
     };
}


来源:https://stackoverflow.com/questions/17424230/breezejs-savechanges-is-it-possible-to-return-a-custom-saveresult-object-someh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!