public static Action<object, object, TypeMappingContext> GetComplexEnumerablePropertiesMapper( Type toEnumerableType, Func<object, object> fromEnumerableGetter, Action<object, object> toEnumerableSetter, ObjectFactory objectFactory, MapperCollection mappers) { return delegate(object from, object to, TypeMappingContext context) { object obj = fromEnumerableGetter(from); if (obj != null) { object targetObject = objectFactory.CreateTargetObject(obj, toEnumerableType, context.MappingContext); var ctx = new TypeMappingContext { From = obj, To = targetObject, MappingContext = context.MappingContext }; Mappers.MapComplexEnumerables(ctx, mappers, objectFactory); toEnumerableSetter(to, targetObject); } }; }
public static Action<object, object, TypeMappingContext> GetComplexPropertiesMapper( Func<object, object> fromPropertyGetter, Action<object, object> toPropertySetter, MapperCollection mappers, ObjectFactory objectFactory) { return delegate(object from, object to, TypeMappingContext context) { object obj = fromPropertyGetter(from); if (obj != null) { var type = obj.GetType(); Type targetType = null; var bySourceType = mappers.GetBySourceType(type, out targetType); Error.MappingException_IfMapperIsNull(bySourceType, type); object toObject = objectFactory.CreateTargetObject(obj, targetType, context.MappingContext); var arg = new TypeMappingContext { From = obj, To = toObject, MappingContext = context.MappingContext }; bySourceType(obj, toObject, arg); toPropertySetter(to, toObject); } }; }
public void Clear_Always_Succeeds() { //Arrange var mapperCollection = new MapperCollection(); mapperCollection.Add(typeof(Type), typeof(Type), delegate(object source, object dest, TypeMappingContext context) { }); //Act mapperCollection.Clear(); //Assert Assert.IsNull(mapperCollection.Get(typeof(Type), typeof(Type))); }
public void Add_GoodValues_Succeeds() { //Arrange var mapperCollection = new MapperCollection(); //Act mapperCollection.Add(typeof(Type), typeof(Type), delegate(object source, object dest, TypeMappingContext context) { }); //Assert Assert.IsNotNull(mapperCollection.Get(typeof(Type), typeof(Type))); Assert.IsNull(mapperCollection.Get(typeof(string), typeof(Type))); Assert.IsNull(mapperCollection.Get(typeof(string), typeof(string))); }
public void GetBySourceType_GoodValues_ReturnsMapper() { //Arrange Type actual = null; var mapperCollection = new MapperCollection(); mapperCollection.Add(typeof(MainEntity), typeof(MainEntityModel), delegate(object source, object dest, TypeMappingContext context) { }); //Act var bySourceType = mapperCollection.GetBySourceType(typeof(MainEntity), out actual); //Assert Assert.IsNotNull(bySourceType); Assert.AreEqual<System.Type>(typeof(MainEntityModel), actual); }
public static Action<object, object, TypeMappingContext> GetMapperFromRootType( Type toPropertyType, Action<object, object> toPropertySetter, MapperCollection mappers, ObjectFactory objectFactory) { return delegate(object from, object to, TypeMappingContext context) { var action = mappers.Get(from.GetType(), toPropertyType); Error.MappingException_IfMapperIsNull(action, from.GetType()); object obj = objectFactory.CreateTargetObject(from, toPropertyType, context.MappingContext); var arg = new TypeMappingContext { From = from, To = obj, MappingContext = context.MappingContext }; action(from, obj, arg); toPropertySetter(to, obj); }; }
public static void MapComplexEnumerables(TypeMappingContext context, MapperCollection mappers, ObjectFactory objectFactory) { Type fromType = context.From.GetType(); Type toType = context.To.GetType(); var addItemMethod = ReflectionHelper.GetAddItemMethod(toType); var addItemMethodInfo = ReflectionHelper.GetAddItemMethodInfo(toType); int num = 0; foreach (object current in context.From as IEnumerable) { if (current == null) { addItemMethod(context.To, null, num, addItemMethodInfo); num++; } else { Type targetType = null; Type currentType = current.GetType(); var bySourceType = mappers.GetBySourceType(currentType, out targetType); Error.MappingException_IfMapperIsNull(bySourceType, currentType); object obj = objectFactory.CreateTargetObject(current, targetType, context.MappingContext); var arg = new TypeMappingContext { From = current, To = obj, MappingContext = context.MappingContext }; bySourceType(current, obj, arg); addItemMethod(context.To, obj, num, addItemMethodInfo); num++; } } }
public static Action<object, object, TypeMappingContext> GetMapperToRootType( Func<object, object> fromPropertyGetter, MapperCollection mappers) { return delegate(object from, object to, TypeMappingContext context) { object obj = fromPropertyGetter(from); if (obj != null) { var action = mappers.Get(obj.GetType(), context.To.GetType()); Error.MappingException_IfMapperIsNull(action, obj.GetType()); var arg = new TypeMappingContext { From = obj, To = to, MappingContext = context.MappingContext }; action(obj, to, arg); } }; }
public void Get_GoodValues_ReturnsMapper() { //Arrange var mapperCollection = new MapperCollection(); mapperCollection.Add(typeof(Type), typeof(Type), delegate(object source, object dest, TypeMappingContext context) { }); //Act var value = mapperCollection.Get(typeof(Type), typeof(Type)); var value2 = mapperCollection.Get(typeof(string), typeof(Type)); var value3 = mapperCollection.Get(typeof(string), typeof(string)); //Assert Assert.IsNotNull(value); Assert.IsNull(value2); Assert.IsNull(value3); }
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; } } } } }