<commit_before>﻿namespace Caelan.Frameworks.Common.Classes

open Autofac
open Caelan.Frameworks.Common.Helpers
open Caelan.Frameworks.Common.Interfaces
open System
open System.Reflection

module Builder = 
    let internal getMapper<'TSource, 'TDestination> (assemblies : Assembly []) = 
        let getMapperType (mapperType : Type) = 
            let isMapper (t : Type) = mapperType.IsAssignableFrom(t) && not t.IsAbstract && not t.IsInterface && not t.IsGenericTypeDefinition
            let cb = ContainerBuilder()
            let mainAssemblies = assemblies |> Array.filter (isNull >> not)
            let refAssemblies = mainAssemblies |> Array.collect (fun i -> i.GetReferencedAssemblies() |> Array.map Assembly.Load)
                                                        
            let allAssemblies = 
                mainAssemblies
                |> Array.append refAssemblies
                |> Array.filter (fun a -> a.GetTypes() |> Array.exists isMapper)
            cb.RegisterGeneric(typedefof<DefaultMapper<'TSource, 'TDestination>>).As(typedefof<IMapper<'TSource, 'TDestination>>) |> ignore
            cb.RegisterAssemblyTypes(allAssemblies).Where(fun t -> t |> isMapper).AsImplementedInterfaces() |> ignore
            let container = cb.Build()
            using (container.BeginLifetimeScope()) (fun _ -> container.Resolve<IMapper<'TSource, 'TDestination>>())
        typeof<IMapper<'TSource, 'TDestination>> |> MemoizeHelper.Memoize getMapperType
    
    [<Sealed>]<commit_msg>autofac builder<commit_after>﻿namespace Caelan.Frameworks.Common.Classes

open Autofac
open Caelan.Frameworks.Common.Helpers
open Caelan.Frameworks.Common.Interfaces
open System
open System.Reflection

module Builder = 
    let mutable private container =
        let cb = ContainerBuilder()
        //cb.RegisterGeneric(typedefof<DefaultMapper<_, _>>).As(typedefof<IMapper<_, _>>) |> ignore
        cb.Build()

    let internal getMapper<'TSource, 'TDestination> (assemblies : Assembly []) = 
        let getMapperType (mapperType : Type) = 
            let mutable mapper = Unchecked.defaultof<IMapper<'TSource, 'TDestination>>
            using (container.BeginLifetimeScope()) (fun _ ->
                if container.TryResolve<IMapper<'TSource, 'TDestination>>(&mapper) |> not then
                    let isMapper (t : Type) = mapperType.IsAssignableFrom(t) && not t.IsAbstract && not t.IsInterface && not t.IsGenericTypeDefinition
                    let cb = ContainerBuilder()
                    let mainAssemblies = assemblies |> Array.filter (isNull >> not)
                    let refAssemblies = mainAssemblies |> Array.collect (fun i -> i.GetReferencedAssemblies() |> Array.map Assembly.Load)
                                                                
                    let allAssemblies = 
                        mainAssemblies
                        |> Array.append refAssemblies
                        |> Array.filter (fun a -> a.GetTypes() |> Array.exists isMapper)
                    cb.RegisterGeneric(typedefof<DefaultMapper<'TSource, 'TDestination>>).As(typedefof<IMapper<'TSource, 'TDestination>>) |> ignore
                    cb.RegisterAssemblyTypes(allAssemblies).Where(fun t -> t |> isMapper).AsImplementedInterfaces() |> ignore
                    cb.Update(container)
                    mapper <- container.Resolve<IMapper<'TSource, 'TDestination>>()
            )
            mapper
        typeof<IMapper<'TSource, 'TDestination>> |> MemoizeHelper.Memoize getMapperType
    
    [<Sealed>]
{'fraction_non_alphanumeric': 0.10621468926553672, 'fraction_numerical': 0.0, 'mean_word_length': 2.4146576663452266, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '910691', 'n_tokens_mistral': 983, 'n_tokens_neox': 958, 'n_words': 193}