public static MutatorsConfigurator <TRoot, TChild, TValue> Set <TRoot, TChild, TValue>( this MutatorsConfigurator <TRoot, TChild, TValue> configurator, Expression <Func <TChild, TValue> > value) { configurator.SetMutator(EqualsToConfiguration.Create(configurator.Root.ConfiguratorType, typeof(TChild), value, null)); return(configurator); }
public void TestSetConstant() { configurator.Target(d => d.RootS).Set("zzz"); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => "zzz")); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public void TestSetFromSourceAndDest() { configurator.Target(d => d.RootS).Set((s, d) => s.RootS + d.A.S); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, DestRoot, string> >)((s, d) => s.RootS + d.A.S)); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public void TestSetWithValueAndConverter() { configurator.Target(d => d.RootS).Set(s => s.RootS, x => x + "def"); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS + "def")); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public void TestPathWithEachAndEach() { configurator.Target(d => d.As.Each().S).Set(s => s.As.Each().S); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.As.Each().S), null); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.As.Each().S); }
public void TestResolvingInterfaceMembersInTargetPath() { configurator.Target(d => ((Interface)d.Impl).S).Set(s => s.RootS); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS), null); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.Impl.S); }
public void TestWithoutConditionBeforeTarget() { configurator.If(s => s.A.S == "zzz").WithoutCondition().Target(d => d.RootS).Set(s => s.RootS); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS), null); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public static ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget> Set <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget>( this ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget> configurator, Expression <Func <TSourceChild, TTarget> > value) { var pathToSourceChild = (Expression <Func <TSourceRoot, TSourceChild> >)configurator.PathToSourceChild.ReplaceEachWithCurrent(); LambdaExpression valueFromRoot = pathToSourceChild.Merge(value); configurator.SetMutator(EqualsToConfiguration.Create(configurator.Root.ConfiguratorType, typeof(TDestRoot), valueFromRoot, null)); return(configurator); }
public static ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget> Set <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget>( this ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TTarget> configurator, Expression <Func <TSourceChild, TTarget> > value) { var methodReplacer = new MethodReplacer(MutatorsHelperFunctions.EachMethod, MutatorsHelperFunctions.CurrentMethod); var pathToSourceChild = (Expression <Func <TSourceRoot, TSourceChild> >)methodReplacer.Visit(configurator.PathToSourceChild); LambdaExpression valueFromRoot = pathToSourceChild.Merge(value); configurator.SetMutator(EqualsToConfiguration.Create(typeof(TDestRoot), valueFromRoot, null)); return(configurator); }
public void TestGotoInDestAndSource() { var subConfigurator = configurator.GoTo(d => d.As.Each(), s => s.As.Current()); subConfigurator.Target(d => d.S).Set(s => s.S); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.As.Current().S), null); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.As.Each().S); }
public void TestSetWithValidator() { configurator.Target(d => d.RootS).Set(s => s.RootS, x => x, x => ValidationResult.Ok); var sourceValidator = StaticValidatorConfiguration.Create <SourceRoot>(MutatorsCreator.Sharp, "SetWithValidator", 0, null, (Expression <Func <SourceRoot, string> >)(x => x.RootS), (Expression <Func <SourceRoot, string> >)(x => x.RootS), (Expression <Func <string, ValidationResult> >)(x => ValidationResult.Ok)); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS), sourceValidator); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public void TestSetWithValueAndValidatorAndConverter() { var validationResult = ValidationResult.Warning(new TestText { Text = "test text" }); configurator.Target(d => d.RootS).Set(s => s.RootS, x => x + "abc", x => x + "def", x => validationResult); var sourceValidator = StaticValidatorConfiguration.Create <SourceRoot>(MutatorsCreator.Sharp, "SetWithValidator", 0, null, (Expression <Func <SourceRoot, string> >)(x => x.RootS), (Expression <Func <SourceRoot, string> >)(x => x.RootS + "abc"), (Expression <Func <string, ValidationResult> >)(x => validationResult)); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS + "abc" + "def"), sourceValidator); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public void TestSetWithMessage() { configurator.Target(d => d.RootS).Set(s => s.RootS, x => x + "def", x => x.Length == 5, s => new TestText { Text = "text!" }, 0, ValidationResultType.Warning); var sourceValidator = StaticValidatorConfiguration.Create <SourceRoot>(MutatorsCreator.Sharp, "SetWithValidator", 0, null, (Expression <Func <SourceRoot, string> >)(x => x.RootS), (Expression <Func <SourceRoot, string> >)(x => x.RootS), (Expression <Func <string, ValidationResult> >)(x => (bool?)(x.Length == 5) == true ? new ValidationResult(ValidationResultType.Warning, new TestText { Text = "text!" }) : ValidationResult.Ok)); var equalsToConfiguration = EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS + "def"), sourceValidator); AssertEquivalentConfigurations(equalsToConfiguration); AssertEquivalentPaths(d => d.RootS); }
public static ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TDestValue> Set <TSourceRoot, TSourceChild, TSourceNode, TSourceValue, TDestRoot, TDestChild, TDestValue>( this ConverterConfigurator <TSourceRoot, TSourceChild, TDestRoot, TDestChild, TDestValue> configurator, Expression <Func <TSourceChild, TSourceNode> > node, Expression <Func <TSourceNode, TSourceValue> > value, Expression <Func <TSourceValue, TDestValue> > converter, Expression <Func <TSourceNode, ValidationResult> > validator, int priority = 0) { var pathToSourceChild = (Expression <Func <TSourceRoot, TSourceChild> >)configurator.PathToSourceChild.ReplaceEachWithCurrent(); var nodeFromRoot = pathToSourceChild.Merge(node); var valueFromRoot = nodeFromRoot.Merge(value); var convertedValue = converter == null ? (LambdaExpression)valueFromRoot : valueFromRoot.Merge(converter); var validatorConfiguration = validator == null ? null : StaticValidatorConfiguration.Create(MutatorsCreator.Sharp, "SetWithValidator", priority, null, nodeFromRoot, valueFromRoot, validator); configurator.SetMutator(EqualsToConfiguration.Create(configurator.Root.ConfiguratorType, typeof(TDestRoot), convertedValue, validatorConfiguration)); return(configurator); }
public void TestBatchSet() { configurator.GoTo(d => d.A).BatchSet((d, s) => new Batch { { d.S.NotNull(), s.RootS }, { d.Bs[0].S, s.As[0].S }, { d.Bs[1].S.NotNull(), s.As[1].S }, { d.Bs.Each().S, s.As.Each().S } }); AssertEquivalentConfigurations( EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.RootS)), EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.As[0].S)), EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.As[1].S)), EqualsToConfiguration.Create <DestRoot>((Expression <Func <SourceRoot, string> >)(s => s.As.Current().S)), NullifyIfConfiguration.Create <DestRoot>(null, (d => d.A.S == null && d.A.Bs[1].S == null)), NullifyIfConfiguration.Create <DestRoot>(null, (d => d.A.S == null && d.A.Bs[1].S == null)) ); AssertEquivalentPathBodies(d => d.A.S, d => d.A.Bs[0].S, d => d.A.Bs[1].S, d => d.A.Bs.Each().S, d => d.A.Bs[0].S, d => d.A.Bs.Each().S); }
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)); } }
private void ConfigureCustomFields(ConverterConfigurator <TSource, TDest> configurator, LambdaExpression pathToSourceChild, LambdaExpression pathToDestChild, Func <Expression, bool> sourceCustomFieldFits, Func <Expression, bool> destCustomFieldFits) { var sourceChildType = pathToSourceChild.Body.Type; var destChildType = pathToDestChild.Body.Type; LambdaExpression pathToSourceCustomFieldsContainer; var sourceCustomFieldsContainer = FindCustomFieldsContainer(sourceChildType, out pathToSourceCustomFieldsContainer); var sourceCustomFields = FindCustomFields(sourceChildType, sourceCustomFieldFits, pathToSourceChild); LambdaExpression pathToDestCustomFieldsContainer; var destCustomFieldsContainer = FindCustomFieldsContainer(destChildType, out pathToDestCustomFieldsContainer); var destCustomFields = FindCustomFields(destChildType, destCustomFieldFits, pathToDestChild); var converterType = configurator.Root.ConfiguratorType; if (sourceCustomFields.Length > 0) { if (destCustomFields.Length > 0 || sourceCustomFieldsContainer != null) { throw new InvalidOperationException(); } if (destCustomFieldsContainer == null) { return; } var destParameter = pathToDestCustomFieldsContainer.Parameters.Single(); var indexerGetter = destCustomFieldsContainer.PropertyType.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(); foreach (var customField in sourceCustomFields) { var path = customField.Path; var rootProperty = customField.RootProperty; var titleType = customField.TitleType; var value = customField.Value; TypeCode typeCode; LambdaExpression convertedValue; if (stringConverter != null && stringConverter.CanConvert(value.Body.Type)) { typeCode = TypeCode.String; convertedValue = Expression.Lambda( Expression.Call(Expression.Constant(stringConverter, typeof(IStringConverter)), convertToStringMethod.MakeGenericMethod(value.Body.Type), Expression.Convert(value.Body, typeof(object))), value.Parameters); } else { typeCode = GetTypeCode(value.Body.Type); convertedValue = value; } if (rootProperty.PropertyType.IsArray && !IsALeaf(rootProperty.PropertyType)) { // An array of complex types var delimiterIndex = path.IndexOf('ё'); var pathToArray = path.Substring(0, delimiterIndex); var pathToLeaf = path.Substring(delimiterIndex + 1, path.Length - delimiterIndex - 1); var pathToTarget = pathToDestChild.Merge(Expression.Lambda(Expression.Call(pathToDestCustomFieldsContainer.Body, indexerGetter, Expression.Constant(pathToArray)), destParameter)).Body; configurator.SetMutator(Expression.Property(pathToTarget, "TypeCode"), EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(TypeCode.Object)))); configurator.SetMutator(Expression.Property(pathToTarget, "IsArray"), EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(true)))); var pathToDestArrayItem = Expression.Convert(Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(typeof(object)), Expression.Convert(Expression.Property(pathToTarget, "Value"), typeof(object[]))), typeof(Hashtable)); var itemIndexerGetter = typeof(Hashtable).GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(); var pathToDestArrayItemValue = Expression.Call(pathToDestArrayItem, itemIndexerGetter, Expression.Constant(pathToLeaf)); configurator.SetMutator(pathToDestArrayItemValue, EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(convertedValue))); var pathToTypeCodes = Expression.Property(pathToTarget, "TypeCodes"); var pathToItemTypeCode = Expression.Call(pathToTypeCodes, pathToTypeCodes.Type.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), Expression.Constant(pathToLeaf)); configurator.SetMutator(pathToItemTypeCode, EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(typeCode)))); if (value.Body is MemberExpression) { var member = ((MemberExpression)value.Body).Member; var customFieldAttribute = member.GetCustomAttributes(typeof(CustomFieldAttribute), false).SingleOrDefault() as CustomFieldAttribute; if (customFieldAttribute != null && customFieldAttribute.TitleType != null) { var pathToTitles = Expression.Property(pathToTarget, "Titles"); var pathToItemTitle = Expression.Call(pathToTitles, pathToTitles.Type.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), Expression.Constant(pathToLeaf)); configurator.SetMutator(pathToItemTitle, EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(StaticMultiLanguageTextCache.Get(customFieldAttribute.TitleType))))); } } } else { var pathToTarget = pathToDestChild.Merge(Expression.Lambda(Expression.Call(pathToDestCustomFieldsContainer.Body, indexerGetter, Expression.Constant(path)), destParameter)).Body; configurator.SetMutator(Expression.Property(pathToTarget, "TypeCode"), EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(typeCode)))); configurator.SetMutator(Expression.Property(pathToTarget, "IsArray"), EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(convertedValue.Body.Type.IsArray)))); configurator.SetMutator(Expression.Property(pathToTarget, "Value"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(convertedValue))); if (titleType != null) { configurator.SetMutator(Expression.Property(pathToTarget, "Title"), EqualsToConfiguration.Create <TDest>(converterType, Expression.Lambda(Expression.Constant(StaticMultiLanguageTextCache.Get(titleType))))); } } } } else if (destCustomFields.Length > 0) { if (destCustomFieldsContainer != null) { throw new InvalidOperationException(); } if (sourceCustomFieldsContainer == null) { return; } var sourceParameter = pathToSourceCustomFieldsContainer.Parameters.Single(); var indexerGetter = sourceCustomFieldsContainer.PropertyType.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(); foreach (var customField in destCustomFields) { var path = customField.Path; var rootProperty = customField.RootProperty; var pathToTarget = pathToDestChild.Merge(customField.Value).Body; if (rootProperty.PropertyType.IsArray && !IsALeaf(rootProperty.PropertyType.GetElementType())) { var delimiterIndex = path.IndexOf('ё'); var pathToArray = path.Substring(0, delimiterIndex); var pathToLeaf = path.Substring(delimiterIndex + 1, path.Length - delimiterIndex - 1); Expression value = Expression.Property(Expression.Call(pathToSourceCustomFieldsContainer.Body, indexerGetter, Expression.Constant(pathToArray)), "Value"); value = Expression.Convert(Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(typeof(object)), Expression.Convert(value, typeof(object[]))), typeof(Hashtable)); value = Expression.Call(value, typeof(Hashtable).GetProperty("Item", BindingFlags.Public | BindingFlags.Instance).GetGetMethod(), Expression.Constant(pathToLeaf)); var convertedValue = MakeConvert(value, pathToTarget.Type); configurator.SetMutator(pathToTarget, EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(convertedValue, sourceParameter)))); } else { Expression value = Expression.Property(Expression.Call(pathToSourceCustomFieldsContainer.Body, indexerGetter, Expression.Constant(path)), "Value"); var convertedValue = MakeConvert(value, pathToTarget.Type); configurator.SetMutator(pathToTarget, EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(convertedValue, sourceParameter)))); } } } else { if (sourceCustomFieldsContainer == null || destCustomFieldsContainer == null) { return; } var destParameter = pathToDestCustomFieldsContainer.Parameters.Single(); var sourceParameter = pathToSourceCustomFieldsContainer.Parameters.Single(); Expression pathToDestCustomContainer = Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(destCustomFieldsContainer.PropertyType.GetItemType()), pathToDestCustomFieldsContainer.Body); Expression pathToSourceCustomContainer = Expression.Call(MutatorsHelperFunctions.EachMethod.MakeGenericMethod(sourceCustomFieldsContainer.PropertyType.GetItemType()), pathToSourceCustomFieldsContainer.Body); var pathToTarget = pathToDestChild.Merge(Expression.Lambda(Expression.Property(pathToDestCustomContainer, "Key"), destParameter)).Body; Expression value = Expression.Property(pathToSourceCustomContainer, "Key"); configurator.SetMutator(pathToTarget, EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(value, sourceParameter)))); value = Expression.Property(pathToSourceCustomContainer, "Value"); pathToTarget = pathToDestChild.Merge(Expression.Lambda(Expression.Property(pathToDestCustomContainer, "Value"), destParameter)).Body; configurator.SetMutator(Expression.Property(pathToTarget, "TypeCode"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "TypeCode"), sourceParameter)))); configurator.SetMutator(Expression.Property(pathToTarget, "TypeCodes"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "TypeCodes"), sourceParameter)))); configurator.SetMutator(Expression.Property(pathToTarget, "Titles"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "Titles"), sourceParameter)))); configurator.SetMutator(Expression.Property(pathToTarget, "IsArray"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "IsArray"), sourceParameter)))); configurator.SetMutator(Expression.Property(pathToTarget, "Value"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "Value"), sourceParameter)))); configurator.SetMutator(Expression.Property(pathToTarget, "Title"), EqualsToConfiguration.Create <TDest>(converterType, pathToSourceChild.Merge(Expression.Lambda(Expression.Property(value, "Title"), sourceParameter)))); } }