Exemple #1
0
        /// <summary>
        ///     Registers content from one or more sources that can be injected into the application.
        ///     <para/>
        ///     This method does not associate the content with a specific type, so to access it later,
        ///     you must inject a <see cref="IContentManager"/> instance and use it's
        ///     <see cref="IContentManager.GetContentSet(string)"/> to get this content set by name.
        /// </summary>
        /// <param name="services">The services collection.</param>
        /// <param name="name">
        ///     A name that can be used to reference this content when accessing through an injected
        ///     <see cref="IContentManager"/>.
        /// </param>
        /// <param name="sourceBuilder">
        ///     A function used to set up the source for the content, along with any fallback sources.
        /// </param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if the <paramref name="services"/> or <paramref name="sourceBuilder"/> parameters
        ///     are <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     Thrown if the <paramref name="name"/> parameter is <c>null</c>, empty or whitespaces.
        /// </exception>
        public static IServiceCollection AddContent(this IServiceCollection services,
                                                    string name,
                                                    Action <ContentBuilder> sourceBuilder)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(Errors.InvalidContentSetName, nameof(name));
            }
            if (sourceBuilder is null)
            {
                throw new ArgumentNullException(nameof(sourceBuilder));
            }

            // Register the IContentManager interface. This can be done multiple times.
            services.AddSingleton <IContentManager>(ContentManager.Global);

            // Create the content builder and register it.
            var builder = new ContentBuilder();

            sourceBuilder(builder);
            ContentManager.Global.Register(name, builder.Build());

            return(services);
        }
        public ContentManager Register(string name, Action <ContentBuilder> builderSetup)
        {
            if (builderSetup is null)
            {
                throw new ArgumentNullException(nameof(builderSetup));
            }

            var builder = new ContentBuilder();

            builderSetup(builder);
            ContentSource[] sources = builder.Build();

            return(Register(name, sources));
        }
        public ContentManager Register <TContentSet>(Action <ContentBuilder> builderSetup)
            where TContentSet : ContentSet, new()
        {
            if (builderSetup is null)
            {
                throw new ArgumentNullException(nameof(builderSetup));
            }

            var builder = new ContentBuilder();

            builderSetup(builder);
            ContentSource[] sources = builder.Build();

            return(Register <TContentSet>(typeof(TContentSet).AssemblyQualifiedName, sources));
        }