Example #1
0
        public void Save(Resource resource)
        {
            lock (Graph.GetWrapper(resource.Id) ?? _fallbackLock)
            {
                using (var uow = UowFactory.Create())
                {
                    var newResources = new HashSet <Resource>();

                    var entity = ResourceEntityAccessor.SaveToEntity(uow, resource);
                    if (entity.Id == 0)
                    {
                        newResources.Add(resource);
                    }

                    var newInstances = ResourceLinker.SaveReferences(uow, resource, entity);
                    newResources.AddRange(newInstances);

                    try
                    {
                        uow.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(LogLevel.Error, ex, "Error saving resource {0}-{1}!", resource.Id, resource.Name);
                        throw;
                    }

                    foreach (var instance in newResources)
                    {
                        AddResource(instance, true);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Get or create an entity for a resource instance
        /// </summary>
        private static ResourceEntity GetOrCreateEntity(ReferenceSaverContext context, Resource instance)
        {
            // First check if the context contains an entity for the instance
            if (context.EntityCache.ContainsKey(instance))
            {
                return(context.EntityCache[instance]);
            }

            ResourceEntity entity;

            if (instance.Id > 0)
            {
                entity = context.UnitOfWork.GetEntity <ResourceEntity>(instance);
            }
            else
            {
                entity = ResourceEntityAccessor.SaveToEntity(context.UnitOfWork, instance);
                context.ResourceLookup[entity] = instance;
            }

            // Get or create an entity for the instance
            return(context.EntityCache[instance] = entity);
        }