Exemple #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);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// A collection with "AutoSave = true" was modified. Write current state to the database
        /// </summary>
        private void OnAutoSaveCollectionChanged(object sender, ReferenceCollectionChangedEventArgs args)
        {
            var instance = args.Parent;
            var property = args.CollectionProperty;

            lock (Graph.GetWrapper(instance.Id)) // Unlike Save AutoSave collections are ALWAYS part of the Graph
            {
                using (var uow = UowFactory.Create())
                {
                    var newResources = ResourceLinker.SaveSingleCollection(uow, instance, property);

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

                    foreach (var newResource in newResources)
                    {
                        AddResource(newResource, true);
                    }
                }
            }
        }
Exemple #3
0
        public bool Destroy(IResource resource, bool permanent)
        {
            var instance = (Resource)resource;

            ((IPlugin)resource).Stop();

            // Load entity and relations to disconnect resource and remove from database
            using (var uow = UowFactory.Create())
            {
                var resourceRepository = uow.GetRepository <IResourceEntityRepository>();
                var relationRepository = uow.GetRepository <IResourceRelationRepository>();

                // Fetch entity and relations
                // Update properties on the references and get rid of relation entities
                var entity    = resourceRepository.GetByKey(instance.Id);
                var relations = ResourceRelationAccessor.FromEntity(uow, entity);
                foreach (var relation in relations)
                {
                    var reference = Graph.Get(relation.ReferenceId);

                    ResourceLinker.RemoveLinking(resource, reference);

                    if (permanent)
                    {
                        relationRepository.Remove(relation.Entity);
                    }
                }

                resourceRepository.Remove(entity, permanent);

                uow.SaveChanges();
            }

            // Unregister from all events to avoid memory leaks
            UnregisterEvents(instance);

            // Remove from internal collections
            var removed = Graph.Remove(instance);

            // Notify listeners about the removal of the resource
            if (removed && instance is IPublicResource publicResource)
            {
                RaiseResourceRemoved(publicResource);
            }

            // Destroy the object
            TypeController.Destroy(instance);

            return(removed);
        }
Exemple #4
0
        public void ExecuteInitializer(IResourceInitializer initializer)
        {
            var roots = initializer.Execute(Graph);

            if (roots.Count == 0)
            {
                throw new InvalidOperationException("ResourceInitializer must return at least one resource");
            }

            using (var uow = UowFactory.Create())
            {
                ResourceLinker.SaveRoots(uow, roots);
                uow.SaveChanges();
            }
        }
Exemple #5
0
 /// <summary>
 /// Build object graph from simplified <see cref="ResourceEntityAccessor"/> and flat resource list
 /// </summary>
 private void LinkReferences(ResourceEntityAccessor entityAccessor)
 {
     ResourceLinker.LinkReferences(entityAccessor.Instance, entityAccessor.Relations);
 }