<commit_before>﻿using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using WebApi.HypermediaExtensions.Hypermedia.Actions;
using WebApi.HypermediaExtensions.JsonSchema;
using WebApi.HypermediaExtensions.Query;
using WebApi.HypermediaExtensions.Util;
using WebApi.HypermediaExtensions.WebApi.AttributedRoutes;
using WebApi.HypermediaExtensions.WebApi.Controller;
using WebApi.HypermediaExtensions.WebApi.Formatter;
using WebApi.HypermediaExtensions.WebApi.RouteResolver;

namespace WebApi.HypermediaExtensions.WebApi.ExtensionMethods
{
    public static class MvcOptionsExtensions
    {
        public static IMvcBuilder AddHypermediaExtensions(this IMvcBuilder builder, IServiceCollection services, 
            HypermediaExtensionsOptions hypermediaOptions,
            params Assembly[] controllerAndHypermediaAssemblies)
        {
            //TODO: register ApplicationModel as singleton and use it everywhere to replace runtime reflection 
            builder.AddMvcOptions(o => 
                o.AddHypermediaExtensions(controllerAndHypermediaAssemblies: controllerAndHypermediaAssemblies)
                 .AddHypermediaParameterBinders(!hypermediaOptions.ImplicitHypermediaActionParameterBinders)
            );
            RegisterServices(services, hypermediaOptions, controllerAndHypermediaAssemblies);

            return builder;
        }

        public static IMvcCoreBuilder AddHypermediaExtensions(this IMvcCoreBuilder builder, IServiceCollection services,
            HypermediaExtensionsOptions hypermediaOptions,
            params Assembly[] controllerAndHypermediaAssemblies)
        {
            //TODO: register ApplicationModel as singleton and use it everywhere to replace runtime reflection 
            builder.AddMvcOptions(o =>
                o.AddHypermediaExtensions(controllerAndHypermediaAssemblies: controllerAndHypermediaAssemblies)
                 .AddHypermediaParameterBinders(!hypermediaOptions.ImplicitHypermediaActionParameterBinders)
            );
            RegisterServices(services, hypermediaOptions, controllerAndHypermediaAssemblies);

            return builder;
        }

        private static void RegisterServices(
            IServiceCollection services,
            HypermediaExtensionsOptions hypermediaOptions,
            Assembly[] controllerAndHypermediaAssemblies)
        {
            if (hypermediaOptions.AutoDeliverJsonSchemaForActionParameterTypes) { 
                services.AutoDeliverActionParameterSchemas(controllerAndHypermediaAssemblies);
            }

            services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
        }

        /// <summary>
        /// Adds the Hypermedia Extensions.<commit_msg>- passs assemblies to crawl to auto parameter binders setup<commit_after>﻿using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using WebApi.HypermediaExtensions.Hypermedia.Actions;
using WebApi.HypermediaExtensions.JsonSchema;
using WebApi.HypermediaExtensions.Query;
using WebApi.HypermediaExtensions.Util;
using WebApi.HypermediaExtensions.WebApi.AttributedRoutes;
using WebApi.HypermediaExtensions.WebApi.Controller;
using WebApi.HypermediaExtensions.WebApi.Formatter;
using WebApi.HypermediaExtensions.WebApi.RouteResolver;

namespace WebApi.HypermediaExtensions.WebApi.ExtensionMethods
{
    public static class MvcOptionsExtensions
    {
        public static IMvcBuilder AddHypermediaExtensions(this IMvcBuilder builder, IServiceCollection services, 
            HypermediaExtensionsOptions hypermediaOptions,
            params Assembly[] controllerAndHypermediaAssemblies)
        {
            //TODO: register ApplicationModel as singleton and use it everywhere to replace runtime reflection 
            builder.AddMvcOptions(o => 
                o.AddHypermediaExtensions(controllerAndHypermediaAssemblies: controllerAndHypermediaAssemblies)
                 .AddHypermediaParameterBinders(!hypermediaOptions.ImplicitHypermediaActionParameterBinders, controllerAndHypermediaAssemblies)
            );
            RegisterServices(services, hypermediaOptions, controllerAndHypermediaAssemblies);

            return builder;
        }

        public static IMvcCoreBuilder AddHypermediaExtensions(this IMvcCoreBuilder builder, IServiceCollection services,
            HypermediaExtensionsOptions hypermediaOptions,
            params Assembly[] controllerAndHypermediaAssemblies)
        {
            //TODO: register ApplicationModel as singleton and use it everywhere to replace runtime reflection 
            builder.AddMvcOptions(o =>
                o.AddHypermediaExtensions(controllerAndHypermediaAssemblies: controllerAndHypermediaAssemblies)
                 .AddHypermediaParameterBinders(!hypermediaOptions.ImplicitHypermediaActionParameterBinders, controllerAndHypermediaAssemblies)
            );
            RegisterServices(services, hypermediaOptions, controllerAndHypermediaAssemblies);

            return builder;
        }

        private static void RegisterServices(
            IServiceCollection services,
            HypermediaExtensionsOptions hypermediaOptions,
            Assembly[] controllerAndHypermediaAssemblies)
        {
            if (hypermediaOptions.AutoDeliverJsonSchemaForActionParameterTypes) { 
                services.AutoDeliverActionParameterSchemas(controllerAndHypermediaAssemblies);
            }

            services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
        }

        /// <summary>
        /// Adds the Hypermedia Extensions.
{'fraction_non_alphanumeric': 0.05351783927975992, 'fraction_numerical': 0.0, 'mean_word_length': 3.6903831118060983, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5015692', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1333, 'n_words': 266}