拦截Breezejs的变化并将其变成新的记录

时间:2014-07-10 13:33:20

标签: breeze asp.net-web-api

我有一个javascript应用程序,它保存前端的更改,将它们推送到WebAPI控制器并将它们保存到Entity Framework上下文。为了保存对表所做更改的历史记录,我想截取某些表的某些编辑,并将它们从编辑行类型命令更改为带有编辑数据的新行命令。我当前的控制器看起来像这样

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

如何设置此项以创建行?

1 个答案:

答案 0 :(得分:2)

更新属于BeforeSaveEntities委托或重写方法的'saveMap'。使用ContextProvider的CreateEntityInfo方法创建要添加到saveMap的项目。

public SaveResult SaveChanges(JObject saveBundle) {
    _ContextProvider.BeforeSaveEntitiesDelegate = CreateNewFoo;
    return _ContextProvider.SaveChanges(saveBundle);
}

// the saveMap parameter contains the entities that were passed from the client to be saved
// you can add to the map here:
private Dictionary<Type, List<EntityInfo>> CreateNewFoo(Dictionary<Type, List<EntityInfo>> saveMap) {
  // create your new entity.
  var foo = new Foo();
  foo.CreatedOn = DateTime.Now;
  // SaveOptions.Tag is free form data that can be passed from the client.
  var tag = ContextProvider.SaveOptions.Tag;
  foo.Comment = (tag == null) ? "Generic comment" : tag.ToString();

  // create an EntityInfo object from the new entity
  var ei = ContextProvider.CreateEntityInfo(foo);

  // add it to the saveMap
  List<EntityInfo> fooInfos;
  if (!saveMap.TryGetValue(typeof(Foo), out fooInfos)) {
    fooInfos = new List<EntityInfo>();
    saveMap.Add(typeof(Foo), fooInfos);
  }
  fooInfos.Add(ei);

  // return the updated saveMap
  return saveMap;
}