/// <summary> /// This method converts your <paramref name="sourceObject"/> to a new instance of <typeparamref name="TTargetType"/> /// according to defined by you or default mapping specifications. The default mapping specifications are created /// by the name similarity of properties in these types. /// </summary> /// <typeparam name="TTargetType"></typeparam> /// <param name="sourceObject"></param> /// <returns>An instance of <typeparamref name="TTargetType"/></returns> public TTargetType MapTo <TTargetType>(object sourceObject) where TTargetType : new() { TTargetType targetTypeInstance = new TTargetType(); Map map = this._mapTable.FindMap(typeof(TTargetType), sourceObject.GetType()); if (map != null) { int specCount = map.Specifications.Length; for (int i = 0; i < specCount; i++) { MapSpecification mapSpecification = map.Specifications[i]; mapSpecification.AssignmentAction( mapSpecification.TargetPropertyInfo , targetTypeInstance , mapSpecification.SourcePropertyInfo , sourceObject); } } else { PropertyInfo[] targetProperties = typeof(TTargetType).GetProperties(); Type sourceType = sourceObject.GetType(); foreach (PropertyInfo targetPropertyInfo in targetProperties) { PropertyInfo sourcePropertyInfo = sourceType.GetProperty(targetPropertyInfo.Name); if (sourcePropertyInfo != null) { targetPropertyInfo.SetValue(targetTypeInstance, sourcePropertyInfo.GetValue(sourceObject)); } } } return(targetTypeInstance); }
/// <summary> /// Prepare specification for <typeparamref name="TPropertyType"/> of <typeparamref name="TTargetType"/> /// </summary> /// <typeparam name="TPropertyType"></typeparam> /// <param name="target"></param> /// <returns><see cref="IMapSpecificationDefinition{TTargetType, TSourceType, TPropertyType}"/></returns> public IMapSpecificationDefinition <TTargetType, TSourceType, TPropertyType> For <TPropertyType>(Expression <Func <TTargetType, TPropertyType> > target) { MapSpecification mapSpecification = new MapSpecification(); MemberExpression targetMemberExpression = (MemberExpression)target.Body; mapSpecification.TargetPropertyInfo = (PropertyInfo)targetMemberExpression.Member; IMapSpecificationDefinition <TTargetType, TSourceType, TPropertyType> mapSpecificationDefinition = new MapSpecificationDefinition <TTargetType, TSourceType, TPropertyType>(mapSpecification); this._specification.Add(mapSpecification); return(mapSpecificationDefinition); }
/// <summary> /// /// </summary> /// <typeparam name="TTargetType"></typeparam> /// <typeparam name="TSourceType"></typeparam> /// <param name="specifications"></param> /// <param name="mapSpecifications"></param> private void HandleUserDefinedSpecifications <TTargetType, TSourceType>(Action <MapSpecificationsDefinition <TTargetType, TSourceType> > specifications, List <MapSpecification> mapSpecifications) { MapSpecificationsDefinition <TTargetType, TSourceType> specificationsDefinition = new MapSpecificationsDefinition <TTargetType, TSourceType>(); specifications(specificationsDefinition); List <MapSpecification> userDefinedMapSpecifications = specificationsDefinition.Specifications; int userDefinedMapSpecificationsCount = userDefinedMapSpecifications.Count; for (int i = 0; i < userDefinedMapSpecificationsCount; i++) { MapSpecification userDefinedMapSpecification = userDefinedMapSpecifications[i]; int existSpecificationIndex = mapSpecifications.FindIndex(mapSpecification => mapSpecification.TargetPropertyInfo == userDefinedMapSpecification.TargetPropertyInfo); if (existSpecificationIndex != -1) { mapSpecifications.RemoveAt(existSpecificationIndex); } mapSpecifications.Add(userDefinedMapSpecification); } }
/// <summary> /// This method creates default specifications for defined type couple according to name conversion /// </summary> /// <param name="targetType"></param> /// <param name="sourceType"></param> /// <returns></returns> private List <MapSpecification> CreateDefaultAssignmentSpecifications(Type targetType, Type sourceType) { List <MapSpecification> mapSpecifications = new List <MapSpecification>(); PropertyInfo[] targetPropertyInfos = targetType.GetProperties(); foreach (PropertyInfo targetPropertyInfo in targetPropertyInfos) { PropertyInfo sourcePropertyInfo = sourceType.GetProperty(targetPropertyInfo.Name); if (sourcePropertyInfo != null) { MapSpecification defaultSpecification = new MapSpecification { TargetPropertyInfo = targetPropertyInfo, SourcePropertyInfo = sourcePropertyInfo, AssignmentAction = this.AssignmentAction }; mapSpecifications.Add(defaultSpecification); } } return(mapSpecifications); }
/// <summary> /// /// </summary> public MapSpecificationDefinition(MapSpecification mapSpecification) { this._mapSpecification = mapSpecification; }