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);
        }
Beispiel #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);
        }
Beispiel #3
0
 public Func <TSource, TDest> GetConverter(MutatorsContext context)
 {
     if (MutatorsAssignRecorder.IsRecording())
     {
         MutatorsAssignRecorder.RecordConverter(GetType().Name);
     }
     return(GetOrCreateHashtableSlot(context).Converter);
 }
        private void LogConverterCompilation(MutatorsContext context, Stopwatch sw)
        {
            var converterCollectionTypeName = GetType().Name;
            var message = new StringBuilder($"{converterCollectionTypeName} was compiled in {sw.ElapsedMilliseconds} ms\n");

            message.AppendLine("Context:\n");
            var mutatorsContextTypeName = context.GetType().Name;

            foreach (var propertyInfo in context.GetType().GetProperties())
            {
                message.AppendLine($"\t{mutatorsContextTypeName}.{propertyInfo.Name}: {propertyInfo.GetValue(context)}");
            }
            logger.Info(message.ToString());
        }
Beispiel #5
0
        private void LogConverterCompilation(MutatorsContext context, Stopwatch sw)
        {
            var logProperties = new Dictionary <string, object>
            {
                { "ConverterCollectionName", GetType().Name },
                { "CompilationTimeMilliseconds", sw.ElapsedMilliseconds }
            };
            var mutatorsContextTypeName = context.GetType().Name;

            foreach (var propertyInfo in context.GetType().GetProperties())
            {
                logProperties.Add($"{mutatorsContextTypeName}.{propertyInfo.Name}", propertyInfo.GetValue(context));
            }
            logger.Log(new LogEvent(LogLevel.Info, DateTimeOffset.UtcNow, "{ConverterCollectionName} was compiled in {CompilationTimeMilliseconds} ms", logProperties));
        }
        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);
        }
        private Action <TDest, TSource> CompileTree(ModelConfigurationNode tree, MutatorsContext context)
        {
            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);
            }
            return(compiledTreeConverter);
        }
        public MutatorsTreeBase <TSource> GetValidationsTree(MutatorsContext context, int priority)
        {
            var slot   = GetOrCreateHashtableSlot(context);
            var result = (MutatorsTreeBase <TSource>)slot.ValidationMutatorsTrees[priority];

            if (result == null)
            {
                lock (lockObject)
                {
                    result = (MutatorsTreeBase <TSource>)slot.ValidationMutatorsTrees[priority];
                    if (result == null)
                    {
                        slot.ValidationMutatorsTrees[priority] = result = new MutatorsTree <TSource>(slot.ValidationsTree, pathFormatterCollection.GetPathFormatter <TSource>(), pathFormatterCollection, priority);
                    }
                }
            }

            return(result);
        }
        public MutatorsTreeBase <TData> GetMutatorsTree(MutatorsContext context, int validationsPriority = 0)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            var slot   = GetOrCreateHashtableSlot(context);
            var result = (MutatorsTreeBase <TData>)slot.MutatorsTrees[validationsPriority];

            if (result == null)
            {
                lock (lockObject)
                {
                    result = (MutatorsTreeBase <TData>)slot.MutatorsTrees[validationsPriority];
                    if (result == null)
                    {
                        slot.MutatorsTrees[validationsPriority] = result = new MutatorsTree <TData>(slot.Tree, pathFormatterCollection.GetPathFormatter <TData>(), pathFormatterCollection, validationsPriority);
                    }
                }
            }

            return(result);
        }
 protected abstract void Configure(MutatorsContext context, MutatorsConfigurator <TData> configurator);
 protected abstract void Configure(MutatorsContext context, ConverterConfigurator <TSource, TDest> configurator);
 public MutatorsTreeBase <TDest> MigratePaths(MutatorsTreeBase <TDest> mutatorsTree, MutatorsContext context)
 {
     return(mutatorsTree?.MigratePaths <TSource>(GetOrCreateHashtableSlot(context).ConverterTree));
 }
 protected void ConfigureInternal(MutatorsContext context, ConverterConfigurator <TSource, TDest> configurator)
 {
     Configure(context, configurator);
     ConfigureCustomFields(configurator);
 }
 public Action <TSource, TDest> GetMerger(MutatorsContext context)
 {
     return(GetOrCreateHashtableSlot(context).Merger);
 }
 public Func <TSource, TDest> GetConverter(MutatorsContext context)
 {
     return(GetOrCreateHashtableSlot(context).Converter);
 }
 public static MutatorsTreeBase <TDest> GetMutatorsTree <TSource, TDest>(this IDataConfiguratorCollection <TDest> collection, MutatorsContext sourceMutatorsContext, MutatorsContext destMutatorsContext, MutatorsContext converterContext)
 {
     return(collection.GetMutatorsTree(new[] { typeof(TSource) }, new[] { sourceMutatorsContext, destMutatorsContext }, new[] { converterContext }));
 }