示例#1
0
        public void MapListPerformanceTest()
        {
            try
            {
                var times   = 1000000;
                var sources = Enumerable.Range(1, 100 * 100 * 100).Select(a => new Source {
                    Name = "1", Age = 1, Birth = DateTime.Now, NoMatch1 = "noMatchField", Week = DayOfWeek.Wednesday
                }).ToList();

                //MapperConfiguration cfg = new MapperConfiguration(c =>
                //{
                //    c.CreateMap<Source, Target>();
                //});
                //var autoMap = cfg.CreateMapper();


                //Stopwatch autoMapWatch = Stopwatch.StartNew();
                //var autoMapTargets = autoMap.Map<List<Source>, List<Target>>(sources);
                //autoMapWatch.Stop();


                Stopwatch expressionWatch    = Stopwatch.StartNew();
                var       expressionsTargets = EasyMap.MapToList <Source, Target>(sources);
                expressionWatch.Stop();
            }
            catch (Exception ex)
            {
                var a = ex;
            }
        }
示例#2
0
        public void MapToListTest()
        {
            try
            {
                var sourceType    = typeof(Source);
                var targetType    = typeof(Target);
                var includeFields = new List <string> {
                    "Name", "Age", "Birth", "Week", "ExceptField"
                };
                Expression <Func <Target, object> > includeSelector = t => new { t.Name, t.Age, t.Birth, t.Week };
                var exceptFields = new List <string> {
                    "ExceptField"
                };
                Expression <Func <Target, object> > exceptSelector = t => new { t.ExceptField };
                var sources = new List <Source> {
                    new Source {
                        Name = "zhoujing", Age = 30, Birth = new DateTime(1989, 1, 1), NoMatch1 = "未匹配的字段,不会赋值到target", Week = DayOfWeek.Wednesday, ExceptField = "这个是排除的字段", NotIncludeField = "这个是不包含字段"
                    }
                };
                Target target;


                var targets = EasyMap.MapToList <Source, Target>(sources);
                Assert.True(targets.All(target => IsMatch(sources[0], target, false, false)));

                targets.ForEach(target => target.Clear());
                targets = EasyMap.MapToList <Source, Target>(sources, includeFields);
                Assert.True(targets.All(target => IsMatch(sources[0], target, false, true)));

                targets.ForEach(target => target.Clear());
                targets = EasyMap.MapToList(sources, includeSelector, exceptSelector);
                Assert.True(targets.All(target => IsMatch(sources[0], target, true, true)));


                targets.ForEach(target => target.Clear());
                targets = EasyMap.MapToList <Source, Target>(sources, includeFields, exceptFields);
                Assert.True(targets.All(target => IsMatch(sources[0], target, true, true)));
            }
            catch (Exception ex)
            {
                throw;
            }
        }