Ejemplo n.º 1
0
        public void Initialise_New_BlogController()
        {
            // Arrange
            Boolean result       = false;
            String  instanceName = fixture.TestSetupBlogControllerName;

            BlogSetupAttribute[] setupAttributes = new BlogSetupAttribute[1];
            BlogSEOAttribute[]   seoAttributes   = new BlogSEOAttribute[1];

            fixture.Initialise(); // Reset the fixture

            setupAttributes[0] = new BlogSetupAttribute(
                blogId: fixture.TestSetupBlogId,
                provider: fixture.TestSetupProvider,
                providerConnectionString: fixture.TestSetupProviderConnectionString);

            String tags = String.Join(",", fixture.TestSEOTags);

            seoAttributes[0] = new BlogSEOAttribute(
                author: fixture.TestSEOAuthor,
                title: fixture.TestSEOTitle,
                description: fixture.TestSEODescription,
                tags: tags);

            // Act
            BlogRegistrationHelper regHelper = new BlogRegistrationHelper();

            result = regHelper.Register(instanceName, setupAttributes, seoAttributes, ref fixture.Blogs);

            // Assert
            Assert.Contains <String, IBlog>(
                fixture.TestSetupBlogControllerName,
                (IReadOnlyDictionary <String, IBlog>)fixture.Blogs
                );
        }
        /// <summary>
        /// Set up the blog from the configuration section in the app startup
        /// </summary>
        /// <param name="value">The application builder that this attaches to</param>
        /// <param name="env">The hosting environment supplied from the startup process </param>
        public static IApplicationBuilder UseBlog(this IApplicationBuilder value, IHostingEnvironment env)
        {
            // Set the environment
            Environment = env;

            // Scan all of the controllers for blog controllers so that they can be registered at start ip
            // that way the blogs can be used from other pages etc.
            BlogRegistrationHelper regHelper   = new BlogRegistrationHelper();
            IEnumerable <Type>     controllers =
                BaseClassExtensions.GetEnumerableOfType <BlogControllerBase>(Assembly.GetCallingAssembly());

            // Loop the found controllers
            foreach (Type controller in controllers)
            {
                try
                {
                    // Call the registration process
                    regHelper.Register(controller, ref Blogs.Items);

                    // Create a new instance of the blog controller purely to
                    // fire the customisable initialisation routine
                    BlogControllerBase blogInstance = (BlogControllerBase)Activator.CreateInstance(controller);
                    if (blogInstance == null)
                    {
                        throw new NotInitialisedBlogException($"Could not initialise the blog '{controller.Name}'");
                    }
                    else
                    {
                        // Call the blog initialised method so custom actions can be applied
                        blogInstance.BlogInitialised();
                    }
                }
                catch (Exception ex)
                {
                    throw BlogException.Passthrough(ex, new NotInitialisedBlogException($"Could not initialise the blog '{controller.Name}'"));
                }
            }

            // Allow stacking so it's consistent with the other extension methods
            return(value);
        }