private MapCompiler.CompilationResult FirstNode(MapCompiler.CompilationContext currentNodeContext) { var properties = currentNodeContext.CurrentNode.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < properties.Length; i++) { var propertyInfo = properties[i]; if (nodeCompilers.ContainsKey(propertyInfo)) { var value = propertyInfo.GetValue(currentNodeContext.CurrentNode, null); if (value != null) { var compilationResult = nodeCompilers[propertyInfo](new MapCompiler.CompilationContext { Map = currentNodeContext.Map, ParentNode = currentNodeContext.CurrentNode, CurrentNode = value, ContextualName = currentNodeContext.ContextualName }); if (compilationResult != null) { return compilationResult; } } } } return null; }
private MapCompiler.CompilationResult TypeMapWithPropertyMaps(MapCompiler.CompilationContext context) { var enumerable = context.CurrentNode as IEnumerable<PropertyMapBase>; if (enumerable.Count<PropertyMapBase>() == 0) { return null; } else { var list = new List<Action<object, object, TypeMappingContext>>(); foreach (PropertyMapBase current in enumerable) { var compilationResult = FirstNode(new MapCompiler.CompilationContext { Map = context.Map, ContextualName = context.ContextualName, ParentNode = enumerable, CurrentNode = current }); list.Add(compilationResult.Mapper); } return new MapCompiler.CompilationResult { Mapper = Mappers.GetMapperWithPropertyMappers(list) }; } }
private MapCompiler.CompilationResult TypeMap(MapCompiler.CompilationContext context) { var compilationResult = FirstNode(context); if (context.Map.Mapper != null) { return compilationResult; } else { TypeMap typeMap = context.Map as TypeMap; var list = new List<Action<object, object, TypeMappingContext>>(); var properties = typeMap.SourceType.GetProperties(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < properties.Length; i++) { var sourcePropertyInfo = properties[i]; if (ReflectionHelper.IsSimple(sourcePropertyInfo.PropertyType) || ReflectionHelper.IsSimpleEnumerable(sourcePropertyInfo.PropertyType)) { if (!typeMap.PropertyMaps.Any((PropertyMap pm) => pm.SourcePropertyInfo == sourcePropertyInfo)) { PropertyInfo propertyInfo = FindDestintationProperty(typeMap.DestinationType, sourcePropertyInfo, context.ContextualName); if (!(propertyInfo == null)) { var getter = GetGetter(typeMap.SourceType, sourcePropertyInfo); var setter = GetSetter(typeMap.DestinationType, propertyInfo); if (ReflectionHelper.IsSimple(sourcePropertyInfo.PropertyType) && ReflectionHelper.IsSimple(propertyInfo.PropertyType)) { list.Add(Mappers.GetSimplePropertiesMapper( sourcePropertyInfo, propertyInfo, converters, getter, setter)); } else { if (ReflectionHelper.IsSimpleEnumerable(sourcePropertyInfo.PropertyType) && ReflectionHelper.IsSimpleEnumerable(propertyInfo.PropertyType)) { list.Add(Mappers.GetSimpleEnumerablePropertiesMapper( sourcePropertyInfo, propertyInfo, objectFactory, converters, getter, setter)); } } } } } } compilationResult = compilationResult ?? new MapCompiler.CompilationResult(); var mapper = compilationResult.Mapper; compilationResult.Mapper = Mappers.GetMapperWithSimplePropertyMappers(mapper, list); return compilationResult; } }
private MapCompiler.CompilationResult ReversiveTypeMapWithMapper(MapCompiler.CompilationContext context) { var compilationResult = MapWithMapper(context); compilationResult.UnMapper = ((ReversiveTypeMap)context.ParentNode).UnMapper; return compilationResult; }
private MapCompiler.CompilationResult ReversiveTypeMap(MapCompiler.CompilationContext context) { var compilationResult = FirstNode(context); var reversiveTypeMap = context.Map as ReversiveTypeMap; if (reversiveTypeMap.Mapper != null && reversiveTypeMap.UnMapper != null) { return compilationResult; } else { var mappers = new List<Action<object, object, TypeMappingContext>>(); var unmappers = new List<Action<object, object, TypeMappingContext>>(); var properties = reversiveTypeMap.SourceType.GetProperties(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < properties.Length; i++) { var sourcePropertyInfo = properties[i]; if (ReflectionHelper.IsSimple(sourcePropertyInfo.PropertyType) || ReflectionHelper.IsSimpleEnumerable(sourcePropertyInfo.PropertyType)) { if (!reversiveTypeMap.PropertyMaps.Any((ReversivePropertyMap pm) => pm.SourcePropertyInfo == sourcePropertyInfo)) { var propertyInfo = FindDestintationProperty(reversiveTypeMap.DestinationType, sourcePropertyInfo, context.ContextualName); if (!(propertyInfo == null)) { var sourceGetter = GetGetter(reversiveTypeMap.SourceType, sourcePropertyInfo); var sourceSetter = GetSetter(reversiveTypeMap.SourceType, sourcePropertyInfo); var destinationGetter = GetGetter(reversiveTypeMap.DestinationType, propertyInfo); var destinationSetter = GetSetter(reversiveTypeMap.DestinationType, propertyInfo); if (ReflectionHelper.IsSimple(sourcePropertyInfo.PropertyType) && ReflectionHelper.IsSimple(propertyInfo.PropertyType)) { mappers.Add(Mappers.GetSimplePropertiesMapper(sourcePropertyInfo, propertyInfo, converters, sourceGetter, destinationSetter)); unmappers.Add(Mappers.GetSimplePropertiesMapper(propertyInfo, sourcePropertyInfo, converters, destinationGetter, sourceSetter)); } else { if (ReflectionHelper.IsSimpleEnumerable(sourcePropertyInfo.PropertyType) && ReflectionHelper.IsSimpleEnumerable(propertyInfo.PropertyType)) { mappers.Add(Mappers.GetSimpleEnumerablePropertiesMapper(sourcePropertyInfo, propertyInfo, objectFactory, converters, sourceGetter, destinationSetter)); unmappers.Add(Mappers.GetSimpleEnumerablePropertiesMapper(propertyInfo, sourcePropertyInfo, objectFactory, converters, destinationGetter, sourceSetter)); } } } } } } compilationResult = (compilationResult ?? new MapCompiler.CompilationResult()); var mapper = compilationResult.Mapper; var unMapper = compilationResult.UnMapper; compilationResult.Mapper = Mappers.GetMapperWithSimplePropertyMappers(mapper, mappers); compilationResult.UnMapper = Mappers.GetMapperWithSimplePropertyMappers(unMapper, unmappers); return compilationResult; } }
private MapCompiler.CompilationResult ReversivePropertyMapWithInheritanceMaps(MapCompiler.CompilationContext context) { var enumerable = context.CurrentNode as IEnumerable<TypeMapBase>; if (enumerable == null || enumerable.Count<TypeMapBase>() == 0) { return null; } else { var propertyMapBase = context.ParentNode as PropertyMapBase; var mapperCollection = new MapperCollection(); var unmapperCollection = new MapperCollection(); string text = context.ContextualName; if (propertyMapBase.DestinationPropertyInfo == null) { text += propertyMapBase.SourcePropertyInfo.Name; } foreach (TypeMapBase current in enumerable) { var compilationResult = Compile(current, text); mapperCollection.Add(current.SourceType, current.DestinationType, compilationResult.Mapper); unmapperCollection.Add(current.DestinationType, current.SourceType, compilationResult.UnMapper); } var compilationResult2 = new MapCompiler.CompilationResult(); var sourceGetter = GetGetter(context.Map.SourceType, propertyMapBase.SourcePropertyInfo); var sourceSetter = GetSetter(context.Map.SourceType, propertyMapBase.SourcePropertyInfo); if (propertyMapBase.DestinationPropertyInfo == null) { compilationResult2.Mapper = Mappers.GetMapperToRootType(sourceGetter, mapperCollection); compilationResult2.UnMapper = Mappers.GetMapperFromRootType(propertyMapBase.SourcePropertyInfo.PropertyType, sourceSetter, unmapperCollection, objectFactory); return compilationResult2; } else { var destinationGetter = GetGetter(context.Map.DestinationType, propertyMapBase.DestinationPropertyInfo); var destinationSetter = GetSetter(context.Map.DestinationType, propertyMapBase.DestinationPropertyInfo); if (ReflectionHelper.IsComplex(propertyMapBase.SourcePropertyInfo.PropertyType) && ReflectionHelper.IsComplex(propertyMapBase.DestinationPropertyInfo.PropertyType)) { compilationResult2.Mapper = Mappers.GetComplexPropertiesMapper(sourceGetter, destinationSetter, mapperCollection, objectFactory); compilationResult2.UnMapper = Mappers.GetComplexPropertiesMapper(destinationGetter, sourceSetter, unmapperCollection, objectFactory); return compilationResult2; } else { if (ReflectionHelper.IsComplexEnumerable(propertyMapBase.SourcePropertyInfo.PropertyType) && ReflectionHelper.IsComplexEnumerable(propertyMapBase.DestinationPropertyInfo.PropertyType)) { compilationResult2.Mapper = Mappers.GetComplexEnumerablePropertiesMapper(propertyMapBase.DestinationPropertyInfo .PropertyType, sourceGetter, destinationSetter, objectFactory, mapperCollection); compilationResult2.UnMapper = Mappers.GetComplexEnumerablePropertiesMapper(propertyMapBase.SourcePropertyInfo .PropertyType, destinationGetter, sourceSetter, objectFactory, unmapperCollection); return compilationResult2; } else { return null; } } } } }
private MapCompiler.CompilationResult MapWithMapper(MapCompiler.CompilationContext context) { var mapper = context.CurrentNode as Action<object, object, TypeMappingContext>; return new MapCompiler.CompilationResult { Mapper = mapper }; }