Beispiel #1
0
 public MutatorsTree(ModelConfigurationNode tree, IPathFormatter pathFormatter, IPathFormatterCollection pathFormatterCollection, int priority)
 {
     this.tree = tree;
     this.pathFormatterCollection = pathFormatterCollection;
     this.pathFormatter           = pathFormatter;
     this.priority = priority;
 }
Beispiel #2
0
        internal override MutatorsTreeBase <T> Migrate <T>(ModelConfigurationNode converterTree)
        {
            var migratedTree = ModelConfigurationNode.CreateRoot(typeof(T));

            tree.Migrate(typeof(T), migratedTree, converterTree);
            return(new MutatorsTree <T>(migratedTree, pathFormatterCollection.GetPathFormatter <T>(), pathFormatterCollection, priority));
        }
Beispiel #3
0
        private static List <KeyValuePair <Expression, Expression> > ExtractAliases(ModelConfigurationNode converterTree, CompositionPerformer performer)
        {
            var aliases = new List <KeyValuePair <Expression, Expression> >();

            ExtractAliases(converterTree, performer, aliases);
            return(aliases);
        }
Beispiel #4
0
        internal override MutatorsTreeBase <TData> MigratePaths <T>(ModelConfigurationNode converterTree)
        {
            var performer = new CompositionPerformer(typeof(TData), typeof(T), converterTree);
            var resolver  = new AliasesResolver(ExtractAliases(converterTree, performer));

            return(new MutatorsTree <TData>(tree, new PathFormatterWrapper(pathFormatterCollection.GetPathFormatter <T>(), pathFormatterCollection.GetPathFormatter <TData>(), converterTree, performer, resolver), pathFormatterCollection, priority));
        }
        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 #6
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);
        }
 internal ModelConfigurationNode(Type rootType, Type nodeType, ModelConfigurationNode root, ModelConfigurationNode parent, ModelConfigurationEdge edge, Expression path)
 {
     RootType = rootType;
     NodeType = nodeType;
     Root     = root ?? this;
     Parent   = parent;
     Edge     = edge;
     Path     = path;
     mutators = new List <KeyValuePair <Expression, MutatorConfiguration> >();
     children = new Dictionary <ModelConfigurationEdge, ModelConfigurationNode>();
 }
        public Expression GetFormattedPath(Expression[] paths)
        {
            var migratedPaths = new List <Expression>();

            foreach (var path in paths)
            {
                var conditionalSetters = performer.GetConditionalSetters(path);
                if (conditionalSetters != null)
                {
                    migratedPaths.AddRange(conditionalSetters.SelectMany(setter => setter.Key.CutToChains(true, true)));
                }
                else
                {
                    var performedPath = performer.Perform(path);
                    if (performedPath.NodeType == ExpressionType.Constant && ((ConstantExpression)performedPath).Value == null)
                    {
                        var primaryDependencies = Expression.Lambda(path, path.ExtractParameters()).ExtractPrimaryDependencies().Select(lambda => lambda.Body).ToArray();
                        if (primaryDependencies.Length > 1)
                        {
                            return(basePathFormatter.GetFormattedPath(paths));
                        }
                        var subRoot = converterTree.Traverse(primaryDependencies[0], false);
                        if (subRoot == null)
                        {
                            return(basePathFormatter.GetFormattedPath(paths));
                        }
                        ModelConfigurationNode keyLeaf = subRoot.FindKeyLeaf();
                        if (keyLeaf != null)
                        {
                            performedPath = performer.Perform(keyLeaf.Path);
                        }
                        else
                        {
                            var subNodes = new List <ModelConfigurationNode>();
                            subRoot.FindSubNodes(subNodes);
                            performedPath = Expression.NewArrayInit(typeof(object), subNodes.Select(node => Expression.Convert(performer.Perform(node.Path), typeof(object))));
                        }
                    }

                    var primaryDependenciez = Expression.Lambda(performedPath, performedPath.ExtractParameters()).ExtractPrimaryDependencies();
                    if (primaryDependenciez.Length == 0)
                    {
                        return(basePathFormatter.GetFormattedPath(paths));
                    }
                    migratedPaths.AddRange(performedPath.CutToChains(true, true));
                }
            }

            return(resolver.Visit(pathFormatter.GetFormattedPath(migratedPaths.GroupBy(chain => new ExpressionWrapper(chain, false)).Select(grouping => grouping.Key.Expression).ToArray())));
        }
Beispiel #9
0
        private static void ExtractAliases(ModelConfigurationNode node, CompositionPerformer performer, List <KeyValuePair <Expression, Expression> > aliases)
        {
            if (node.GetMutators().Any())
            {
                var conditionalSetters = performer.GetConditionalSetters(node.Path);
                if (conditionalSetters != null && conditionalSetters.Count == 1)
                {
                    var setter = conditionalSetters.Single();
                    if (setter.Value == null)
                    {
                        var chains = setter.Key.CutToChains(true, true);
                        if (chains.Length == 1)
                        {
                            var chain = chains.Single();
                            aliases.Add(new KeyValuePair <Expression, Expression>(node.Path, chain));
                            if (IsEachOrCurrent(node.Path))
                            {
                                aliases.Add(new KeyValuePair <Expression, Expression>(Expression.Call(MutatorsHelperFunctions.CurrentIndexMethod.MakeGenericMethod(node.Path.Type), node.Path), Expression.Call(MutatorsHelperFunctions.CurrentIndexMethod.MakeGenericMethod(chain.Type), chain)));
                            }
                        }
                    }
                }
            }
            else if (IsEachOrCurrent(node.Path))
            {
                var conditionalSetters = performer.GetConditionalSetters(((MethodCallExpression)node.Path).Arguments.Single());
                if (conditionalSetters != null && conditionalSetters.Count == 1)
                {
                    var setter = conditionalSetters.Single();
                    if (setter.Value == null)
                    {
                        var chains = setter.Key.CutToChains(true, true);
                        if (chains.Length == 1)
                        {
                            var chain = chains.Single();
                            chain = Expression.Call(MutatorsHelperFunctions.CurrentMethod.MakeGenericMethod(chain.Type.GetItemType()), chain);
                            aliases.Add(new KeyValuePair <Expression, Expression>(Expression.Call(MutatorsHelperFunctions.CurrentIndexMethod.MakeGenericMethod(node.Path.Type), node.Path), Expression.Call(MutatorsHelperFunctions.CurrentIndexMethod.MakeGenericMethod(chain.Type), chain)));
                        }
                    }
                }
            }

            foreach (var child in node.Children)
            {
                ExtractAliases(child, performer, aliases);
            }
        }
        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);
        }
        private MigratedTrees <T> GetMigratedTrees <T>(string key, ModelConfigurationNode converterTree)
        {
            var tuple         = new Tuple <Type, string>(typeof(T), key);
            var migratedTrees = (MigratedTrees <T>)hashtable[tuple];

            if (migratedTrees == null)
            {
                lock (lockObject)
                {
                    migratedTrees = (MigratedTrees <T>)hashtable[tuple];
                    if (migratedTrees == null)
                    {
                        hashtable[tuple] = migratedTrees = new MigratedTrees <T>
                        {
                            EntirelyMigrated = Migrate <T>(converterTree),
                            PathsMigrated    = MigratePaths <T>(converterTree)
                        };
                    }
                }
            }

            return(migratedTrees);
        }
 public ConverterConfigurator(ModelConfigurationNode root)
     : this(null, root, null)
 {
 }
 internal MutatorsTreeBase <TData> MigratePaths <T>(string key, ModelConfigurationNode converterTree)
 {
     return(GetMigratedTrees <T>(key, converterTree).PathsMigrated);
 }
 public PathFormatterWrapper(IPathFormatter pathFormatter, IPathFormatter basePathFormatter, ModelConfigurationNode converterTree, CompositionPerformer performer, AliasesResolver resolver)
 {
     this.pathFormatter     = pathFormatter;
     this.basePathFormatter = basePathFormatter;
     this.converterTree     = converterTree;
     this.performer         = performer;
     this.resolver          = resolver;
 }
 internal MutatorsTreeBase <T> Migrate <T>(string key, ModelConfigurationNode converterTree)
 {
     return(GetMigratedTrees <T>(key, converterTree).EntirelyMigrated);
 }
Beispiel #17
0
 public ConverterConfigurator(ModelConfigurationNode root, LambdaExpression condition = null)
 {
     Condition = condition;
     this.root = root;
 }
 internal abstract MutatorsTreeBase <TData> MigratePaths <T>(ModelConfigurationNode converterTree);
 public MutatorsConfigurator(ModelConfigurationNode root, LambdaExpression condition = null)
 {
     this.root = root;
     Condition = condition;
 }
Beispiel #20
0
 internal override MutatorsTreeBase <TData> MigratePaths <T>(ModelConfigurationNode converterTree)
 {
     return(new MultipleMutatorsTree <TData>(trees.Select(tree => tree.MigratePaths <T>(converterTree)).ToArray()));
 }
 internal ConverterConfigurator(ConfiguratorReporter reporter, ModelConfigurationNode root, LambdaExpression condition = null)
 {
     this.reporter = reporter;
     Condition     = condition;
     Root          = root;
 }