Пример #1
0
        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);
        }
Пример #2
0
        public void TestTargetNotNullableSourceNotNullable()
        {
            var from1 = new FromNotNullProp()
            {
                Id   = 2,
                Name = "2005",
                InM  = new InClassMapper()
                {
                    Owner = "紫妈",
                    Map   = new StructMapper()
                    {
                        Owner = "dld"
                    }
                }
            };
            var to1  = EasyMapper.Map <FromNotNullProp, ToNotNullProp>(from1);
            var to10 = new ToNotNullProp();
            var to11 = EasyMapper.Map <FromNotNullProp, ToNotNullProp>(from1, to10);

            var to2 = EasyMapper.Map <FromNotNullProp, ToNotNullField>(from1);

            var from2 = new FromNotNullField()
            {
                Id = 5, Name = "2005"
            };

            to1 = EasyMapper.Map <FromNotNullField, ToNotNullProp>(from2);
            to2 = EasyMapper.Map <FromNotNullField, ToNotNullField>(from2);
        }
Пример #3
0
        /// <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));
        }
Пример #4
0
        private AppUser GetUser(string userName, IPermissionQuery query)
        {
            var u = query.GetEmployeeUsingName(userName);

            if (u == null)
            {
                return(null);
            }

            switch (u.GroupSort)
            {
            case GroupSort.Muggle:
            {
                u.Resources = (query.GetEmployeeResource(u.AggregateId) ?? new ResourceModel[] { }).ToActionDesciptors();
            }
            break;

            case GroupSort.Leader:
            {
                u.Resources = ((query.GetEmployeeResource(u.AggregateId) ?? new ResourceModel[] { }).Union(query.GetAllResource(GroupSort.Leader) ?? new ResourceModel[] { })).ToActionDesciptors();
            }
            break;

            case GroupSort.Super:
            {
                u.Resources = (query.GetAllResource(new[] { GroupSort.Muggle, GroupSort.Leader, GroupSort.Super }) ?? new ResourceModel[] { }).ToActionDesciptors();
            }
            break;
            }

            return(EasyMapper.Map(u, new AppUser(), (x, y) => { y.Resources = x.Resources; }));
        }
Пример #5
0
 public void TestEnumParse()
 {
     var from = new TestFromMappeEnum()
     {
         Value = MapperEnum.A | MapperEnum.B
     };
     var to = EasyMapper.Map(from, new TestToMappeEnum());
 }
Пример #6
0
        public void CreateDataTable_Test()
        {
            var mapper    = new EasyMapper();
            var testData  = GetTestEmployees();
            var datatable = mapper.CreateDataTable(testData);

            Assert.IsTrue(datatable.Columns.Contains("Id"));
        }
Пример #7
0
        public static TViewModel GetById <TEntity, TViewModel>(IService service, int id)
            where TEntity : PocoBase
            where TViewModel : class, new()
        {
            var item = service.GetById <TEntity>(id);

            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return(EasyMapper.Map <TEntity, TViewModel>(item));
        }
Пример #8
0
        public void TestStruct()
        {
            var from = new StructMapper()
            {
                Owner = "lxl"
            };

            var to = EasyMapper.Map <StructMapper, ClassMapper>(from);

            var a = new AInfoAmount()
            {
                Amount = 200
            };
            var b = EasyMapper.Map(a, new BInfoAmount());
        }
Пример #9
0
        public void TestTargetNotNullableSourceNullable()
        {
            var from1 = new FromNullProp()
            {
                Id = 2, Name = "2005"
            };
            var to1 = EasyMapper.Map <FromNullProp, ToNotNullProp>(from1);
            var to2 = EasyMapper.Map <FromNullProp, ToNotNullField>(from1);

            var from2 = new FromNullField()
            {
                Id = 5, Name = "2005"
            };

            to1 = EasyMapper.Map <FromNullField, ToNotNullProp>(from2);
            to2 = EasyMapper.Map <FromNullField, ToNotNullField>(from2);
        }
Пример #10
0
        /// <summary>
        /// 查询该任务所有的详情节点
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="withAllMessage">是否需要所有消息字段</param>
        /// <returns></returns>
        public virtual ExecutingNode[] GetAll(Guid taskId, bool withAllMessage = false)
        {
            var target = default(MyExecutingNode);

            if (withAllMessage)
            {
                using (var sql = this.Open())
                {
                    target = sql.QueryForObject <MyExecutingNode>(string.Format("select TaskId,StepCount,StartTime,FinishTime,FailTimes,WaitTimes,StepType,CoordinationMode,ExecutingMessage,ResultMessage,CreateDate,EditDate,Version from {0}task where taskId = @TaskId;", this.TablePrefixName), new { @TaskId = taskId });
                }
            }
            else
            {
                using (var sql = this.Open())
                {
                    target = sql.QueryForObject <MyExecutingNode>(string.Format("select TaskId,StepCount,StartTime,FinishTime,FailTimes,WaitTimes,StepType,CoordinationMode,ResultMessage,CreateDate,EditDate,Version from {0}task where taskId = @TaskId;", this.TablePrefixName), new { @TaskId = taskId });
                }
            }

            if (target == null)
            {
                return(new ExecutingNode[0]);
            }

            if (target.StepCount <= 0 || target.StepType.IsNullOrWhiteSpace())
            {
                return(new ExecutingNode[0]);
            }

            var list  = new List <ExecutingNode>(target.StepCount);
            var steps = target.StepType.Split(',');
            var modes = target.CoordinationMode.Split(',');

            for (int i = 0; i < target.StepCount && i < steps.Length && i < modes.Length; i++)
            {
                var node = EasyMapper.Map(target, new ExecutingNode());
                node.StepCoordinationMode = modes[i].AsEnum <CoordinationMode>();
                node.StepType             = steps[i];
                node.OrderNo = (i + 1);
                list.Add(node);
            }

            return(list.ToArray());
        }
Пример #11
0
        public void TestTargetNullableSourceNotNullable()
        {
            var from1 = new FromNotNullProp()
            {
                Id = 2, Name = "2005",
            };
            var to1  = EasyMapper.Map <FromNotNullProp, ToNullProp>(from1);
            var to10 = new ToNullProp()
            {
                InM = new InClassMapper()
            };
            var to11 = EasyMapper.Map <FromNotNullProp, ToNullProp>(from1, to10);

            var to2 = EasyMapper.Map <FromNotNullProp, ToNullField>(from1);

            var from2 = new FromNotNullField()
            {
                Id = 5, Name = "2005"
            };

            to1 = EasyMapper.Map <FromNotNullField, ToNullProp>(from2);
            to2 = EasyMapper.Map <FromNotNullField, ToNullField>(from2);
        }
Пример #12
0
 /// <summary>
 /// 自动映射对象
 /// </summary>
 /// <typeparam name="From">源来对象类型</typeparam>
 /// <typeparam name="To">目标对象类型</typeparam>
 /// <param name="from">源来</param>
 /// <param name="target">目标</param>
 /// <param name="callBack">回调</param>
 /// <returns></returns>
 protected virtual To Map <From, To>(From from, To target, Action <From, To> callBack)
 {
     return(EasyMapper.Map(from, target, callBack));
 }
Пример #13
0
 public override TTarget Map <TTarget>(object source)
 {
     return((TTarget)GenericHelper.RewriteAndInvoke(
                () => EasyMapper.Map <X1>(source), typeof(TTarget)
                ));
 }