Exemple #1
0
        public void TestThrowsWhenPathsCountGreaterThanExpressionParametersCount()
        {
            Expression <Func <A, string> > pathFromRoot1 = a => a.S;
            Expression <Func <B, D> >      pathFromRoot2 = b => b.C[1].D;
            Expression <Func <D, string> > exp           = d => d.S;
            var merger = new ExpressionMerger(pathFromRoot1, pathFromRoot2);

            Assert.Throws <ArgumentException>(() => merger.Merge(exp));
        }
        public static void BatchSet <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TDestValue>(
            this ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TDestValue> configurator,
            Expression <Func <TDestValue, TSourceChild, Batch> > batch)
        {
            var        methodReplacer    = new MethodReplacer(MutatorsHelperFunctions.EachMethod, MutatorsHelperFunctions.CurrentMethod);
            var        pathToSourceChild = (Expression <Func <TSourceRoot, TSourceChild> >)methodReplacer.Visit(configurator.PathToSourceChild);
            var        pathToChild       = (Expression <Func <TDestRoot, TDestChild> >)methodReplacer.Visit(configurator.PathToChild);
            var        merger            = new ExpressionMerger(pathToSourceChild);
            var        initializers      = ((ListInitExpression)batch.Body).Initializers;
            Expression primaryKeyIsEmpty = null;

            foreach (var initializer in initializers)
            {
                Expression dest        = initializer.Arguments[0];
                var        clearedDest = ClearNotNull(dest);
                if (clearedDest != null)
                {
                    var current = Expression.Equal(clearedDest, Expression.Constant(null, clearedDest.Type));
                    primaryKeyIsEmpty = primaryKeyIsEmpty == null ? current : Expression.AndAlso(primaryKeyIsEmpty, current);
                }

                dest = clearedDest ?? dest;
                if (dest.Type != typeof(object))
                {
                    dest = Expression.Convert(dest, typeof(object));
                }
                Expression source = methodReplacer.Visit(initializer.Arguments[1]);
//                if(source.Type != typeof(object))
//                    source = Expression.Convert(source, typeof(object));
                LambdaExpression value = merger.Merge(Expression.Lambda(source, batch.Parameters[1]));
                if (dest.NodeType == ExpressionType.Convert)
                {
                    dest = ((UnaryExpression)dest).Operand;
                }
                dest = pathToChild.Merge(Expression.Lambda(dest, batch.Parameters[0])).Body;
                configurator.ToRoot().SetMutator(dest, EqualsToConfiguration.Create(typeof(TDestRoot), value, null));
                //configurator.Target(Expression.Lambda<Func<TDestValue, object>>(dest, batch.Parameters[0])).SetMutator(EqualsToConfiguration.Create(typeof(TDestRoot), value, null));
            }

            if (primaryKeyIsEmpty == null)
            {
                return;
            }
            var condition = (Expression <Func <TDestRoot, bool?> >)pathToChild.Merge(Expression.Lambda(Expression.Convert(methodReplacer.Visit(primaryKeyIsEmpty), typeof(bool?)), batch.Parameters[0]));

            foreach (var initializer in initializers)
            {
                Expression dest = initializer.Arguments[0];
                if (ClearNotNull(dest) != null)
                {
                    continue;
                }
                if (dest.Type != typeof(object))
                {
                    dest = Expression.Convert(dest, typeof(object));
                }
                if (dest.NodeType == ExpressionType.Convert)
                {
                    dest = ((UnaryExpression)dest).Operand;
                }
                dest = pathToChild.Merge(Expression.Lambda(dest, batch.Parameters[0])).Body;
                configurator.ToRoot().SetMutator(dest, NullifyIfConfiguration.Create(condition));

                //configurator.Target(Expression.Lambda<Func<TDestValue, object>>(dest, batch.Parameters[0])).SetMutator(NullifyIfConfiguration.Create(condition));
            }
        }