private static void AddErrorToModelState(ModelStateDictionary modelState, string propertyName, string message) { string actualPropertyName = propertyName; var prefix = string.Empty; var mapContext = EasyMapper.Context; if (mapContext.Exists) { var indexOfPrefix = propertyName.IndexOf(']'); if (indexOfPrefix != -1) { prefix = propertyName.Substring(0, indexOfPrefix + 2); propertyName = propertyName.Substring(indexOfPrefix + 2); } var viewModelPropertyPath = EasyMapper.GetMap(mapContext.TargetType, mapContext.SourceType, propertyName); //In debug it will throw to enforce developper to look at the issue //When view model are used everywhere... //Asserts<InvalidOperationException>.IsNotNull(viewModelPropertyPath, string.Format("Property '{0}' is not correctly mapped", propertyName)); //In release it will use the propertyname, the rendering will miss proper routing to the invalidated property but it wont crash if (viewModelPropertyPath != null) { actualPropertyName = prefix + viewModelPropertyPath.Path; } } modelState.AddModelError(actualPropertyName, message); }
/// <summary> /// Specifies the target type /// </summary> public IQueryable <TDest> To <TDest>(Mapping <TSource, TDest> mapping) where TDest : class { var key = GetCacheKey <TDest>(); var queryExpression = expressionCache_.GetOrAdd( key, x => { var destinationProperties = typeof(TDest).GetProperties().Where(dest => dest.CanWrite && ((!dest.PropertyType.IsGenericType || dest.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))); var parameterExpression = Expression.Parameter(typeof(TSource), SourceParameterName); var bindings = new List <MemberAssignment>(); foreach (var destinationProperty in destinationProperties) { var propertyPath = EasyMapper.GetMap <TDest, TSource>(destinationProperty.Name); if (propertyPath != null) { var member = propertyPath.propertyInfos_.Aggregate <PropertyInfo, Expression>(parameterExpression, Expression.Property); var binding = Expression.Bind(destinationProperty, member); //var binding = BuildBinding(parameterExpression, destinationProperty, sourceProperties); if (binding.Expression.ToString().Split(Dot).Skip(1).Count() > 1) { binding = NullCheck(binding, parameterExpression, destinationProperty); } bindings.Add(binding); } } var expression = Expression.Lambda <Func <TSource, TDest> >(Expression.MemberInit(Expression.New(typeof(TDest)), bindings), parameterExpression); return(expression); }) as Expression <Func <TSource, TDest> >; return(source_.Select(queryExpression)); }