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); }