private SaveResult ProcessSaveResult(EntityManager entityManager, string saveResultJson) { var jo = JObject.Parse(saveResultJson); var jn = new JNode(jo); var kms = jn.GetArray <KeyMapping>("KeyMappings"); var keyMappings = kms.Select(km => ToEntityKeys(km)).ToDictionary(tpl => tpl.Item1, tpl => tpl.Item2); using (entityManager.NewIsLoadingBlock(false)) { keyMappings.ForEach(km => { var targetEntity = entityManager.FindEntityByKey(km.Key); targetEntity.EntityAspect.SetDpValue(km.Key.EntityType.KeyProperties[0], km.Value.Values[0]); }); var prop = jo.Property("Entities"); if (prop == null) { return(null); } var entityNodes = (JArray)prop.Value; var serializer = new JsonSerializer(); var jsonConverter = new JsonEntityConverter(entityManager, MergeStrategy.OverwriteChanges, NormalizeEntityTypeName); serializer.Converters.Add(jsonConverter); // Don't use the result of the Deserialize call to get the list of entities // because it won't include entities added on the server. serializer.Deserialize <IEnumerable <IEntity> >(entityNodes.CreateReader()); var allEntities = jsonConverter.AllEntities; allEntities.ForEach(e => e.EntityAspect.AcceptChanges()); return(new SaveResult(allEntities, keyMappings)); } }
public async Task <IEnumerable> ExecuteQuery(EntityQuery query) { if (query.TargetType == null) { throw new Exception("Cannot execute a query with a null TargetType"); } var dataService = query.DataService != null ? query.DataService : this.DefaultDataService; await FetchMetadata(dataService); var resourcePath = query.GetResourcePath(); // HACK resourcePath = resourcePath.Replace("/*", ""); var result = await dataService.GetAsync(resourcePath); var mergeStrategy = query.QueryOptions.MergeStrategy ?? this.DefaultQueryOptions.MergeStrategy ?? QueryOptions.Default.MergeStrategy; // cannot reuse a jsonConverter - internal refMap is one instance/query var jsonConverter = new JsonEntityConverter(this, mergeStrategy.Value); Type rType; if (resourcePath.Contains("inlinecount")) { rType = typeof(QueryResult <>).MakeGenericType(query.TargetType); } else { rType = typeof(IEnumerable <>).MakeGenericType(query.TargetType); } using (NewIsLoadingBlock()) { return((IEnumerable)JsonConvert.DeserializeObject(result, rType, jsonConverter)); } }
private SaveResult ProcessSaveResult(EntityManager entityManager, string saveResultJson) { var jo = JObject.Parse(saveResultJson); var jn = new JNode(jo); var kms = jn.GetArray<KeyMapping>("KeyMappings"); var keyMappings = kms.Select(km => ToEntityKeys(km)).ToDictionary(tpl => tpl.Item1, tpl => tpl.Item2); using (entityManager.NewIsLoadingBlock(false)) { keyMappings.ForEach(km => { var targetEntity = entityManager.FindEntityByKey(km.Key); targetEntity.EntityAspect.SetDpValue(km.Key.EntityType.KeyProperties[0], km.Value.Values[0]); }); var prop = jo.Property("Entities"); if (prop == null) return null; var entityNodes = (JArray)prop.Value; var serializer = new JsonSerializer(); var jsonConverter = new JsonEntityConverter(entityManager, MergeStrategy.OverwriteChanges, NormalizeEntityTypeName); serializer.Converters.Add(jsonConverter); // Don't use the result of the Deserialize call to get the list of entities // because it won't include entities added on the server. serializer.Deserialize<IEnumerable<IEntity>>(entityNodes.CreateReader()); var allEntities = jsonConverter.AllEntities; allEntities.ForEach(e => e.EntityAspect.AcceptChanges()); return new SaveResult(allEntities, keyMappings); } }