Пример #1
0
        /// <summary>
        /// Only build the database schema.
        /// </summary>
        /// <param name="dispatcher"></param>
        /// <param name="containerFileName"></param>
        /// <param name="connections"></param>
        /// <param name="configHash"></param>
        /// <returns></returns>
        private static MetadataContainer BuildMetadata(IQueryDispatcher dispatcher, string containerFileName,
                                                       List <Connection> connections, string configHash)
        {
            var container = new MetadataContainer {
                ConfigFileHash = configHash
            };

            //Copy connection strings
            foreach (var cs in connections)
            {
                container.ConnectionStrings.Add(
                    new SqlConnection(cs.Id)
                {
                    ProviderName     = cs.ProviderName,
                    ConnectionString = cs.ConnectionString
                });
            }

            FetchMetadata(dispatcher, ref container);
            container.Metadatas.FinalizeSchema();

            //Save container
            container.Save(containerFileName);
            return(container);
        }
Пример #2
0
        /// <summary>
        /// Verify if the last build of the container's metadata still match with the current cloning settings.
        /// </summary>
        /// <param name="dispatcher">Query dispatcher</param>
        /// <param name="app">Application user config</param>
        /// <param name="mapId">MapId</param>
        /// <param name="behaviourId">BehaviourId</param>
        /// <param name="container">Container</param>
        public static void VerifyIntegrityWithSettings(IQueryDispatcher dispatcher, Settings settings, ref MetadataContainer container)
        {
            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (settings.Project == null)
            {
                throw new ArgumentNullException(nameof(settings.Project));
            }
            if (String.IsNullOrWhiteSpace(settings.Project.Name))
            {
                throw new ArgumentException(nameof(settings.Project.Name));
            }
            if (settings.Project.ConnectionStrings != null &&
                !settings.Project.ConnectionStrings.Any())
            {
                throw new NullReferenceException("settings.Project.ConnectionStrings");
            }

            var       project           = settings.Project;
            var       containerFileName = project.Name + "_";
            Map       map             = null;
            Behaviour clonerBehaviour = null;

            //Hash the selected map, connnectionStrings and the cloner
            //configuration to see if it match the lasted builded container
            var configData = new MemoryStream();

            SerializationHelper.Serialize(configData, project.ConnectionStrings);

            if (settings.MapId.HasValue)
            {
                map = settings.Project.Maps.FirstOrDefault(m => m.Id == settings.MapId);
                if (map == null)
                {
                    throw new Exception($"Map id '{settings.MapId}' not found in configuration file for application '{project.Name}'!");
                }
                containerFileName += map.From + "-" + map.To;
                SerializationHelper.Serialize(configData, map);

                if (map.UsableBehaviours != null && map.UsableBehaviours.Split(',').ToList().Contains(settings.BehaviourId.ToString()))
                {
                    clonerBehaviour = project.Behaviours.FirstOrDefault(c => c.Id == settings.BehaviourId);
                    if (clonerBehaviour == null)
                    {
                        throw new KeyNotFoundException(
                                  $"There is no behaviour '{settings.BehaviourId}' in the configuration for the appName name '{project.Name}'.");
                    }

                    SerializationHelper.Serialize(configData, clonerBehaviour);
                    SerializationHelper.Serialize(configData, project.Templates);
                }
            }
            else
            {
                containerFileName += "defaultMap";
            }

            if (settings.BehaviourId != null)
            {
                containerFileName += "_" + settings.BehaviourId;
            }
            containerFileName += ".cache";

            //Hash user config
            configData.Position = 0;
            //var murmur = MurmurHash.Create32(managed: false);
            //var configHash = Encoding.UTF8.GetString(murmur.ComputeHash(configData));
            var configHash = container.ConfigFileHash + "random";

            //If in-memory container is good, we use it
            if (container != null && container.ConfigFileHash == configHash)
            {
                return;
            }

            if (!settings.UseInMemoryCacheOnly)
            {
                //If container on disk is good, we use it
                container = TryLoadContainer(containerFileName, configHash);
                if (container != null)
                {
                    dispatcher.InitProviders(container.Metadatas, container.ConnectionStrings);
                    return;
                }
            }

            //We rebuild the container
            container = BuildMetadataWithSettings(dispatcher, containerFileName, project, clonerBehaviour, map, configHash);

            //Persist container
            if (!settings.UseInMemoryCacheOnly)
            {
                container.Save(containerFileName);
            }
        }