Exemplo n.º 1
0
        // *******************************************************************

        /// <summary>
        /// This method builds the specified product instance.
        /// </summary>
        /// <typeparam name="TProduct">The type of product to build.</typeparam>
        /// <returns>A new product instance.</returns>
        TProduct IBuilder.Build <TProduct>()
        {
            // If there are no external providers then we'll need to insist
            //   that the builder contains at least one source.
            if (_externalProviders?.Count() == 0)
            {
                // The builder requires at least one source before it can build
                //   anything.
                if (this.Sources().Count() == 0)
                {
                    throw new BuilderException(
                              Resources.BuilderBase_NoSources
                              );
                }
            }

            // Create a product instance.
            var product = new TProduct();

            // Should we add providers from a parent product?
            if (_externalProviders?.Count() > 0)
            {
                // Add any external providers.
                foreach (var provider in _externalProviders)
                {
                    product.AddProvider(provider);
                }
            }

            // Loop and build providers from the sources.
            foreach (var source in _sources)
            {
                // Build up the provider from the source.
                var provider = source.Build(this);

                // Add the provider to the product.
                product.AddProvider(provider);
            }

            // Initialize the product after building it.
            product.Initialize();

            // Return the product instance.
            return(product);
        }