예제 #1
0
        /// <summary>
        /// Initializes the manager.
        /// NOTE: This method should be called before the usage of the manager,
        ///       usually at the entry point of the application!
        /// </summary>
        /// <returns></returns>
        public async Task InitializeAsync()
        {
            // If there isn't an options file...
            if (!File.Exists(OptionsFileName))
            {
                // There is nothing to initialize from, so return
                return;
            }

            // Get the options
            var options = XMLHelpers.FromXml <CeidDiplomatikiOptionsDataModel>(OptionsFileName);

            // For every database...
            foreach (var database in options.Databases)
            {
                // Add it
                Register(database);
            }

            // For every database options...
            foreach (var databaseOptions in Databases)
            {
                // Get the connection string
                databaseOptions.TryGetConnectionString(out var connectionString);

                // Get the analyzer
                var analyzer = CeidDiplomatikiDI.GetDatabaseAnalyzer(databaseOptions.Provider);

                // Get the database
                var database = analyzer.GetDatabases().First(x => x.DatabaseName == databaseOptions.DatabaseName);

                // Get the tables
                var tables = analyzer.GetTables(databaseOptions.DatabaseName);

                // Get the columns
                var columns = analyzer.GetColumns(database.DatabaseName, null);

                // For every query map related to that database...
                foreach (var queryMapDataModel in options.QueryMaps.Where(x => x.DatabaseId == databaseOptions.Id))
                {
                    // The joins collection
                    var joins = new List <JoinMap>();

                    // For every pair...
                    foreach (var joinDataModel in queryMapDataModel.Joins)
                    {
                        // Get the principle column
                        var principleColumn = columns.First(x => x.ColumnName == joinDataModel.PrincipleKeyColumnName);

                        // Get the foreign key column
                        var referencedColumn = columns.First(x => x.ColumnName == joinDataModel.ForeignKeyColumnName);

                        // Create the join map
                        var joinMap = new JoinMap(tables.First(x => x.TableName == joinDataModel.TableName), principleColumn, tables.First(x => x.TableName == joinDataModel.ReferencedTableName), referencedColumn, joinDataModel.Index, joinDataModel.IsInverted);

                        // Add it to the joins
                        joins.Add(joinMap);
                    }

                    // Create the map
                    var queryMap = QueryMap.FromDataModel(databaseOptions, database, tables.Where(x => queryMapDataModel.TableNames.Contains(x.TableName)).ToList(), columns.Where(x => queryMapDataModel.TableNames.Contains(x.TableName)).ToList(), joins, queryMapDataModel);

                    // Register it
                    await RegisterAsync(queryMap);
                }
            }

            // For every page...
            foreach (var page in options.PageMaps)
            {
                // Add it
                Register(PageMap.FromDataModel(page, null));
            }

            // Await a task
            await Task.CompletedTask;
        }