breezejs issues with the save bundle

橙三吉。 提交于 2019-11-30 18:48:02

问题


im working with breezejs, and the server-side code of my app is .net.

in my views (client side), i want to add and entity then i want to save it. Lets assume that an entity is like this :

{
  "Id": 1,
  "Name": "someName",
  "CreatedDate": "1900-01-01T05:00:00Z",
  "UpdatedDate": "1900-01-01T05:00:00Z",
  "CreatedBy": null,
  "UpdatedBy": null,
  "RowVersion": 0,
   etc ...
  }
}

i want to set the values of CreatedDate UpdatedDate CreatedBy and UpdatedBy, i can do that using javascript of course, BUT i dont want the client to take care of those kind of things.

my breeze controller where lives this function is like this:

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
    return _contextProvider.SaveChanges(saveBundle);
 }

as u can see the saveBundle is a JObject, when i debug i see the saveBundle like this:

{
"entities": [
{
  "Id": 1,
  "Name": "someName",
  "CreatedDate": "1900-01-01T05:00:00Z",
  "UpdatedDate": "1900-01-01T05:00:00Z",
  "CreatedBy": null,
  "UpdatedBy": null,
  "RowVersion": 0,
   etc ...
    }
  }
}
],
"saveOptions": {}
}

How can i change the values of CreatedDate UpdatedDate CreatedBy and UpdatedBy in the saveBundle before the save is commited ???

this is a JObject with an array of objects as proprety, i can manipulate Json with javascript, how can i do it with .Net ???

Thanks a lot.


回答1:


Thanks to Jay Traband post I finally found a way to make changes to an entity before saving it.

I overloaded the BeforeSaveEntity like this :

protected override bool BeforeSaveEntity(EntityInfo entityInfo) {
        // Return false if don´t want to  save the entity
        var entityType = entityInfo.Entity.GetType();

        if (entityInfo.Entity.GetType() == typeof(MyEntityTypeModel))
        {
            if (entityInfo.EntityState == EntityState.Added)
            // It can be 'Modified' or 'Deleted'
            {
                var MyModel = entityInfo.Entity as MyEntityTypeModel;

                MyModel.CreatedDate = DateTime.Now;
                MyModel.UpdatedDate = DateTime.Now;
                string username = Membership.GetUser().UserName;
                MyModel.CreatedBy = username;
                MyModel.UpdatedBy = username;
            }
        }
        return true;
   }

Thanks a lot, and I hope this will help someone someday.




回答2:


You can intercept Breeze's server side save process by using the ContextProvider's virtual BeforeSaveEntity and BeforeSaveEntities methods. The documentation here and here has more information on this topic.

For example, you can override BeforeSaveEntities with code something like the following.

protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {

   foreach (var type in saveMap.Keys) {
     var list = saveMap[type];
      foreach (var entityInfo in list) {
         var entity = entityInfo.Entity;
         // .. do something interesting here
      }
 }


来源:https://stackoverflow.com/questions/19223135/breezejs-issues-with-the-save-bundle

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