Пример #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);
                    }
                }
            }
        }
Пример #2
0
        public Resource Instantiate(string type)
        {
            // Create simplified template and instantiate
            var template = new ResourceEntityAccessor {
                Type = type
            };
            var instance = template.Instantiate(TypeController, this);

            // Initially set name to value of DisplayNameAttribute if available
            var typeObj         = instance.GetType();
            var displayNameAttr = typeObj.GetCustomAttribute <DisplayNameAttribute>();

            instance.Name = displayNameAttr?.DisplayName ?? typeObj.Name;

            return(instance);
        }
Пример #3
0
        public void Initialize()
        {
            // Set delegates on graph
            Graph.SaveDelegate    = Save;
            Graph.DestroyDelegate = Destroy;

            _startup = ResourceStartupPhase.LoadResources;
            using (var uow = UowFactory.Create(ContextMode.AllOff))
            {
                // Create all objects
                var allResources = ResourceEntityAccessor.FetchResourceTemplates(uow);
                if (allResources.Count > 0)
                {
                    LoadResources(allResources);
                }
                else
                {
                    Logger.Log(LogLevel.Warning, "The ResourceManager initialized without a resource." +
                               "Execute a resource initializer to add resources with \"exec ResourceManager initialize\"");
                }
            }

            _startup = ResourceStartupPhase.Initializing;
            // Boot resources
            Parallel.ForEach(Graph.GetAll(), resourceWrapper =>
            {
                try
                {
                    resourceWrapper.Initialize();
                }
                catch (Exception e)
                {
                    resourceWrapper.ErrorOccured();
                    Logger.LogException(LogLevel.Warning, e, "Failed to initialize resource {0}-{1}", resourceWrapper.Target.Id, resourceWrapper.Target.Name);
                }
            });
            _startup = ResourceStartupPhase.Initialized;
        }
Пример #4
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);
        }
Пример #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);
 }