/// <summary> /// Convert ResourceModel back to resource and/or update its properties /// </summary> public Resource FromModel(ResourceModel model, HashSet <Resource> resourcesToSave, Resource resource = null) { // Break recursion if we converted this instance already // Try to load by real id first if (_resourceCache.ContainsKey(model.Id)) { return(_resourceCache[model.Id]); } // Otherwise by reference id if (model.Id == 0 && _resourceCache.ContainsKey(model.ReferenceId)) { return(_resourceCache[model.ReferenceId]); } // Only fetch resource object if it was not given if (resource == null) { resource = model.Id == 0 ? _resourceGraph.Instantiate(model.Type) : _resourceGraph.Get(model.Id); } // Write to cache because following calls might only have an empty reference if (model.Id == 0) { _resourceCache[model.ReferenceId] = resource; } else { _resourceCache[model.Id] = resource; } // Do not copy values from partially loaded models if (model.PartiallyLoaded) { return(resource); } // Add to list if object was created or modified if (model.Id == 0 || model.DifferentFrom(resource, _serialization)) { resourcesToSave.Add(resource); } // Copy standard properties resource.Name = model.Name; resource.Description = model.Description; // Copy extended properties EntryConvert.UpdateInstance(resource.Descriptor, model.Properties, _serialization); // Set all other references UpdateReferences(resource, resourcesToSave, model); return(resource); }
/// <summary> /// Convert ResourceModel back to resource and/or update its properties /// </summary> public Resource FromModel(ResourceModel model, HashSet <Resource> resourcesToSave, Resource resource = null) { // Break recursion if we converted this instance already if (_resourceCache.ContainsKey(model)) { return(_resourceCache[model]); } // Only fetch resource object if it was not given if (resource == null) { // Get or create resource resource = model.Id == 0 ? _resourceGraph.Instantiate(model.Type) : _resourceGraph.Get(model.Id); _resourceCache[model] = resource; } // Add to list if object was created or modified if (model.Id == 0 || model.DifferentFrom(resource, _serialization)) { resourcesToSave.Add(resource); } // Copy standard properties resource.Name = model.Name; resource.Description = model.Description; // Copy extended properties if (model.Properties != null) { EntryConvert.UpdateInstance(resource.Descriptor, model.Properties, _serialization); } // Set all other references if (model.References != null) { UpdateReferences(resource, resourcesToSave, model); } return(resource); }
/// <summary> /// Convert ResourceModel back to resource and/or update its properties /// </summary> private Resource FromModel(ResourceModel model, HashSet <long> resourcesToSave, Dictionary <long, Resource> cache, Resource resource = null) { // Break recursion if we converted this instance already // Try to load by real id first if (cache.ContainsKey(model.Id)) { return(cache[model.Id]); } // Otherwise by reference id if (model.Id == 0 && cache.ContainsKey(model.ReferenceId)) { return(cache[model.ReferenceId]); } // Only fetch resource object if it was not given if (resource is null) { if (model.Id == 0 && model.PartiallyLoaded) { resource = (Resource)Activator.CreateInstance(_resourceTypeTree[model.Type].ResourceType); } else if (model.Id == 0) { var id = _resourceModification.Create(_resourceTypeTree[model.Type].ResourceType, r => { }); resource = _resourceModification.Read <Resource>(id, resource => resource); } else { resource = _resourceModification.Read <Resource>(model.Id, resource => resource); } } // Write to cache because following calls might only have an empty reference if (model.Id == 0) { cache[model.ReferenceId] = resource; } else { cache[model.Id] = resource; } // Do not copy values from partially loaded models if (model.PartiallyLoaded) { return(resource); } // Add to list if object was created or modified if (model.Id == 0 || model.DifferentFrom(resource, _serialization)) { resourcesToSave.Add(resource.Id); } // Copy standard properties resource.Name = model.Name; resource.Description = model.Description; // Copy extended properties EntryConvert.UpdateInstance(resource.Descriptor, model.Properties, _serialization); // Set all other references UpdateReferences(resource, resourcesToSave, cache, model); return(resource); }