Esempio n. 1
0
        public Repository(params Assembly[] searchAssemblies)
        {
            mInstancePool = new JsonReferenceResolver();

            mSchema     = PersistedNode.GetSchema(searchAssemblies ?? new Assembly[0]);
            mConverters = PersistedNode.GetConverters(searchAssemblies ?? new Assembly[0]);
        }
Esempio n. 2
0
        public void Export(IBlobStore target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var container    = target;
            var instancePool = mInstancePool;

            foreach (var type in mSchema.GetKnownElements().SelectMany(x => x.ConcreteTypes))
            {
                var section          = PersistedNode.GetContainerKey(type);
                var sectionContainer = container.GetChildStore(section);

                foreach (var instance in instancePool.GetAll(type))
                {
                    var key = new StringKey($"{instance.Key}.json");

                    using (var writer = sectionContainer.Open(key).AsText().Write())
                    {
                        try
                        {
                            instance.Serialize(writer, mConverters);
                        }
                        catch (Exception exception)
                        {
                            throw new InvalidDataException($"Object \"{instance.Key}\" ({section}): {exception.Message}", exception);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void Import(IBlobStore source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var container    = source;
            var instancePool = mInstancePool;

            foreach (var keyTypePartition in mSchema.GetPartitionsByDependencyDepth())
            {
                foreach (var keyType in keyTypePartition)
                {
                    foreach (var implementation in keyType.ConcreteTypes)
                    {
                        var section          = PersistedNode.GetContainerKey(implementation);
                        var sectionContainer = container.GetChildStore(section, true);

                        foreach (var leaf in sectionContainer.Keys.Where(x => x.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            using (var reader = sectionContainer.Open(leaf).AsText().Read())
                            {
                                var key = Path.GetFileNameWithoutExtension(leaf);

                                try
                                {
                                    var instance = Persistable.Deserialize(key, implementation, reader, instancePool, mConverters);

                                    mInstancePool.Register(instance);
                                }
                                catch (Exception exception)
                                {
                                    throw new InvalidDataException($"Object \"{key}\" ({section}): {exception.Message}", exception);
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public ImageSource GetIcon <T>(StringKey key)
        {
            var category = PersistedNode.GetContainerKey(typeof(T));

            return(GetIcon(key, category));
        }
Esempio n. 5
0
        public static ObjectDependencyGraph <PersistedNode> GetSchema(params Assembly[] searchAssemblies)
        {
            var schema = new ObjectDependencyGraph <PersistedNode>();

            var types =
                from assembly in searchAssemblies
                from type in assembly.GetTypes()

                where type.IsSubclassOf(typeof(Persistable))
                where !type.IsAbstract && !type.IsInterface

                select type;

            var composedTypeArray    = types.ToArray();
            var typeToInfoDictionary = new Dictionary <Type, PersistedNode>();

            foreach (var type in composedTypeArray)
            {
                var currentHierarchyLevel = type;
                var keyType = type;

                while (currentHierarchyLevel != null && currentHierarchyLevel != typeof(Persistable) && currentHierarchyLevel != typeof(object))
                {
                    if (currentHierarchyLevel.GetCustomAttribute <KeyClassAttribute>(false) != null)
                    {
                        keyType = currentHierarchyLevel;
                        break;
                    }

                    currentHierarchyLevel = currentHierarchyLevel.BaseType;
                }

                var info = typeToInfoDictionary.GetValueByKeyOrDefault(keyType);
                if (info == null)
                {
                    typeToInfoDictionary.Add(keyType, info = new PersistedNode(keyType));
                }

                var transientDependencies =
                    from property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                    from dependency in GetTransientDependencies(property.PropertyType)
                    select dependency;

                info.ConcreteTypes.Add(type);

                foreach (var dependency in transientDependencies)
                {
                    info.Dependencies.Add(dependency);
                }

                schema.Register(info);
                typeToInfoDictionary.AddOrUpdate(keyType, info);
            }

            foreach (var type in typeToInfoDictionary.Keys)
            {
                var info           = typeToInfoDictionary[type];
                var dependencyNode = schema.Element(info);

                foreach (var dependency in info.Dependencies)
                {
                    var dependencyInfo = typeToInfoDictionary.GetValueByKeyOrDefault(dependency);
                    if (dependencyInfo == null || dependencyInfo.Key == dependency)
                    {
                        continue;
                    }

                    dependencyNode.DependsOn(dependencyInfo);
                }
            }

            return(schema);
        }
Esempio n. 6
0
 private bool Equals(PersistedNode other)
 {
     return(Key == other.Key);
 }