/// <summary> /// Converts the field to destination property. /// </summary> /// <param name="container">The container.</param> /// <param name="destinationType">The destination type.</param> /// <param name="fieldDescription">The field description.</param> /// <returns> /// The conversion result. /// </returns> private static object ConvertFieldToDestinationProperty(DataContainer container, Type destinationType, Tuple<string, Type> fieldDescription) { if (container == null || container.Fields == null || string.IsNullOrEmpty(fieldDescription.Item1)) { return null; } object sourceValue; if (!container.Fields.TryGetValue(fieldDescription.Item1, out sourceValue) || sourceValue == null) { return null; } var sourceType = fieldDescription.Item2 ?? sourceValue.GetType(); return ReflectionUtils.ConvertValue(sourceValue, sourceType, destinationType); }
/// <summary> /// Converts the source property to fields. /// </summary> /// <param name="sourceType">Type of the property.</param> /// <param name="sourceValue">The property value.</param> /// <param name="container">The container.</param> /// <param name="fieldsDescription">The fields description.</param> private static void ConvertSourcePropertyToFields(object sourceValue, Type sourceType, DataContainer container, List<Tuple<string, Type>> fieldsDescription) { if (container == null || container.Fields == null) { return; } fieldsDescription.ForEach(fd => { if (container.Fields.ContainsKey(fd.Item1)) { return; } var value = ReflectionUtils.ConvertValue(sourceValue, sourceType, fd.Item2); if (value != null) { container.Fields.Add(fd.Item1, value); } }); }
public void DataContainerToEntityMappingTest() { var container = new DataContainer { Fields = new Dictionary<string, object> { { "order_name", "Name" }, { "order_id", Guid.NewGuid().ToString() }, { "order_number", 9345 }, { "order_number_2", 9345 }, { "order_price", decimal.MinValue }, { "UserName", "Peter" }, { "Time", DateTime.Now.ToString() } } }; Mapper.DataMapper.Initialize(new DomainMappingInitializator()); var entity = Mapper.Map<DataContainer, Entity>(container); Assert.AreEqual(container.Fields["order_id"].ToString(), entity.Id.ToString()); Assert.AreEqual(container.Fields["order_number"], entity.Number); Assert.AreEqual(container.Fields["order_number_2"], entity.Number); Assert.AreEqual(container.Fields["order_name"], entity.Name); Assert.AreEqual(container.Fields["order_price"], entity.Price); Assert.AreEqual(container.Fields["UserName"], entity.UserName); }