private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context)
        {
            var key  = context.GetKey();
            var slot = (HashtableSlot)hashtable[key];

            if (slot == null)
            {
                lock (lockObject)
                {
                    slot = (HashtableSlot)hashtable[key];
                    if (slot == null)
                    {
                        var root = ModelConfigurationNode.CreateRoot(typeof(TData));
                        Configure(context, new MutatorsConfigurator <TData>(root));
                        hashtable[key] = slot = new HashtableSlot
                        {
                            Tree          = root,
                            MutatorsTrees = new Hashtable()
                        };
                    }
                }
            }

            return(slot);
        }
Exemplo n.º 2
0
        private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context)
        {
            var key  = context.GetKey();
            var slot = (HashtableSlot)hashtable[key];

            if (slot == null /* || MutatorsAssignRecorder.IsRecording()*/)
            {
                lock (lockObject)
                {
                    slot = (HashtableSlot)hashtable[key];
                    if (slot == null /* || MutatorsAssignRecorder.IsRecording()*/)
                    {
                        var tree = ModelConfigurationNode.CreateRoot(typeof(TDest));
                        ConfigureInternal(context, new ConverterConfigurator <TSource, TDest>(tree));
                        var validationsTree = ModelConfigurationNode.CreateRoot(typeof(TSource));
                        tree.ExtractValidationsFromConverters(validationsTree);

                        var treeConverter = (Expression <Action <TDest, TSource> >)tree.BuildTreeMutator(typeof(TSource));

                        Action <TDest, TSource> compiledTreeConverter;
                        var sw = Stopwatch.StartNew();
                        try
                        {
                            compiledTreeConverter = LambdaCompiler.Compile(treeConverter, CompilerOptions.All);
                        }
                        finally
                        {
                            sw.Stop();
                            LogConverterCompilation(context, sw);
                        }

                        slot = new HashtableSlot
                        {
                            ConverterTree   = tree,
                            ValidationsTree = validationsTree,
                            Converter       = source =>
                            {
                                var dest = new TDest();
                                BeforeConvert(source);
                                compiledTreeConverter(dest, source);
                                AfterConvert(dest, source);
                                return(dest);
                            },
                            Merger = (source, dest) =>
                            {
                                BeforeConvert(source);
                                compiledTreeConverter(dest, source);
                                AfterConvert(dest, source);
                            },
                            ValidationMutatorsTrees = new Hashtable()
                        };
                        //if(!MutatorsAssignRecorder.IsRecording())
                        hashtable[key] = slot;
                    }
                }
            }

            return(slot);
        }
        private HashtableSlot GetOrCreateHashtableSlot(MutatorsContext context)
        {
            var key  = context.GetKey();
            var slot = (HashtableSlot)hashtable[key];

            if (slot == null)
            {
                lock (lockObject)
                {
                    slot = (HashtableSlot)hashtable[key];
                    if (slot == null)
                    {
                        var tree = ModelConfigurationNode.CreateRoot(GetType(), typeof(TDest));
                        ConfigureInternal(context, new ConverterConfigurator <TSource, TDest>(tree));
                        var validationsTree = ModelConfigurationNode.CreateRoot(GetType(), typeof(TSource));
                        tree.ExtractValidationsFromConverters(validationsTree);

                        var lazyCompiledConverter = new Lazy <Action <TDest, TSource> >(() => CompileTree(tree, context), LazyThreadSafetyMode.ExecutionAndPublication);

                        slot = new HashtableSlot
                        {
                            ConverterTree   = tree,
                            ValidationsTree = validationsTree,
                            Converter       = source =>
                            {
                                var dest = new TDest();
                                BeforeConvert(source);
                                lazyCompiledConverter.Value(dest, source);
                                AfterConvert(dest, source);
                                return(dest);
                            },
                            Merger = (source, dest) =>
                            {
                                BeforeConvert(source);
                                lazyCompiledConverter.Value(dest, source);
                                AfterConvert(dest, source);
                            },
                            ValidationMutatorsTrees = new Hashtable()
                        };
                        //if(!MutatorsAssignRecorder.IsRecording())
                        hashtable[key] = slot;
                    }
                }
            }

            return(slot);
        }