예제 #1
0
        /// <summary>
        /// Initializes the Model Builder Pipeline (class constructor).
        /// </summary>
        static ModelBuilderPipeline()
        {
            using (new Tracer())
            {
                IList <IModelBuilder> modelBuilders = new List <IModelBuilder>();
                try
                {
                    ModelBuilderPipelineConfiguration config = (ModelBuilderPipelineConfiguration)ConfigurationManager.GetSection(ModelBuilderPipelineConfiguration.SectionName);
                    if (config != null)
                    {
                        foreach (ModelBuilderSettings modelBuilderSettings in config.ModelBuilders)
                        {
                            Type          modelBuilderType = Type.GetType(modelBuilderSettings.Type, throwOnError: true, ignoreCase: true);
                            IModelBuilder modelBuilder     = (IModelBuilder)modelBuilderType.CreateInstance();
                            modelBuilders.Add(modelBuilder);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Throwing exceptions from a class constructor results in rather cryptic error messages, so we handle the exception here.
                    Log.Error(ex);
                    Log.Warn("An error occurred while initializing the Model Builder Pipeline. Using the Default Model Builder.");
                    modelBuilders.Clear();
                }

                if (modelBuilders.Count == 0)
                {
                    modelBuilders.Add(new DefaultModelBuilder());
                }

                ModelBuilders = modelBuilders;
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes the Model Builder Pipeline (class constructor).
        /// </summary>
        static ModelBuilderPipeline()
        {
            Log.Debug($"Constructing ModelBuilderPipeline");
            using (new Tracer())
            {
                IList <IPageModelBuilder>   pageModelBuilders   = new List <IPageModelBuilder>();
                IList <IEntityModelBuilder> entityModelBuilders = new List <IEntityModelBuilder>();
                try
                {
                    ModelBuilderPipelineConfiguration config = (ModelBuilderPipelineConfiguration)ConfigurationManager.GetSection(ModelBuilderPipelineConfiguration.SectionName);
                    if (config == null)
                    {
                        Log.Warn($"No '{ModelBuilderPipelineConfiguration.SectionName}' configuration section found.");
                    }
                    else
                    {
                        foreach (ModelBuilderSettings modelBuilderSettings in config.ModelBuilders)
                        {
                            Type                modelBuilderType   = Type.GetType(modelBuilderSettings.Type, throwOnError: true, ignoreCase: true);
                            object              modelBuilder       = modelBuilderType.CreateInstance();
                            IPageModelBuilder   pageModelBuilder   = modelBuilder as IPageModelBuilder;
                            IEntityModelBuilder entityModelBuilder = modelBuilder as IEntityModelBuilder;
                            IDataModelExtension modelExtension     = modelBuilder as IDataModelExtension;
                            if (modelExtension != null)
                            {
                                Log.Info($"Using Data Model Extension Type '{modelBuilderType.FullName}'");
                                SiteConfiguration.ModelServiceProvider?.AddDataModelExtension(modelExtension);
                            }
                            if ((pageModelBuilder == null) && (entityModelBuilder == null))
                            {
                                Log.Warn($"Configured Model Builder Type '{modelBuilderType.FullName}' does not implement IPageModelBuilder nor IEntityModelBuilder; skipping.");
                                continue;
                            }
                            if (pageModelBuilder != null)
                            {
                                Log.Info($"Using Page Model Builder Type '{modelBuilderType.FullName}'");
                                pageModelBuilders.Add(pageModelBuilder);
                            }
                            if (entityModelBuilder != null)
                            {
                                Log.Info($"Using Entity Model Builder Type '{modelBuilderType.FullName}'");
                                entityModelBuilders.Add(entityModelBuilder);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Throwing exceptions from a class constructor results in rather cryptic error messages, so we handle the exception here.
                    Log.Error(ex);
                    Log.Warn("An error occurred while initializing the Model Builder Pipeline. Using the Default Model Builder only.");
                    pageModelBuilders.Clear();
                    entityModelBuilders.Clear();
                }

                // Ensure there is always at least one Page/Entity Model Buidler.
                if (!pageModelBuilders.Any() || !entityModelBuilders.Any())
                {
                    DefaultModelBuilder defaultModelBuilder = new DefaultModelBuilder();
                    if (!pageModelBuilders.Any())
                    {
                        Log.Warn($"No Page Model Builders configured; using '{defaultModelBuilder.GetType().FullName}' only.");
                        pageModelBuilders.Add(defaultModelBuilder);
                    }
                    if (!entityModelBuilders.Any())
                    {
                        Log.Warn($"No Entity Model Builders configured; using '{defaultModelBuilder.GetType().FullName}' only.");
                        entityModelBuilders.Add(defaultModelBuilder);
                    }
                }

                _pageModelBuilders   = pageModelBuilders;
                _entityModelBuilders = entityModelBuilders;
            }
        }