Пример #1
0
        public static TPostData GetPostData <TPostData>() where TPostData : class, new()
        {
            ObjectsMapper <NameValueCollection, TPostData> mapper
                = new ObjectMapperManager().GetMapper <NameValueCollection, TPostData>(new FormCollectionMapConfig());

            return(mapper.Map(HttpContext.Current.Request.Form));
        }
Пример #2
0
        protected virtual TEntity ToEntity(TDto dto)
        {
            //默认的成员对应
            var     mapper = new ObjectMapperManager().GetMapper <TDto, TEntity>(new DefaultMapConfig());
            TEntity entity = mapper.Map(dto);

            return(entity);
        }
Пример #3
0
        protected virtual TDto ToDto(TEntity entity)
        {
            //默认的成员对应
            var  mapper = new ObjectMapperManager().GetMapper <TEntity, TDto>(new DefaultMapConfig());
            TDto dto    = mapper.Map(entity);

            return(dto);
        }
Пример #4
0
        //protected override MoneyOutDto ToDto(MoneyOut entity)
        //{
        //    return null;
        //}

        protected override MoneyOut ToEntity(MoneyOutDto dto)
        {
            var mapper = new ObjectMapperManager().
                         GetMapper <MoneyOutDto, MoneyOut>(new DefaultMapConfig().ConvertUsing <MoneyOutDto, MoneyOut>(value => new MoneyOut(value.MoneyOutId)
            {
                KindType = value.Id,
                OutTime  = value.OutTime,
                Quality  = value.Quality,
                Remark   = value.Remark,
            }));
            MoneyOut entity = mapper.Map(dto);

            return(entity);
        }
Пример #5
0
        protected override SportRecord ToEntity(SportRecordDto dto)
        {
            var mapper = new ObjectMapperManager().
                         GetMapper <SportRecordDto, SportRecord>(new DefaultMapConfig().ConvertUsing <SportRecordDto, SportRecord>(value => new SportRecord(value.Id)
            {
                Id           = value.Id,
                ActivityKind = Convert.ToInt16(dto.SportName),
                ActivityTime = value.ActivityTime,
                Numbers      = value.Numbers,
                Remark       = value.Remark,
            }));
            SportRecord entity = mapper.Map(dto);

            return(entity);
        }
Пример #6
0
        private void Use()
        {
            Sourse src = new Sourse
            {
                A = 1,
                B = 0M,
                C = "2011/9/21 0:00:00",
                D = new Inner
                {
                    D2 = Guid.NewGuid()
                },
                E = "test"
            };

            ObjectsMapper <Sourse, Dest> mapper1 =
                new ObjectMapperManager().GetMapper <Sourse, Dest>(
                    new DefaultMapConfig()
                    //忽略
                    .IgnoreMembers <Sourse, Dest>(new string[] { "A" })
                    //为空赋值
                    .NullSubstitution <decimal?, decimal>((value) => - 1M)
                    //两个实体之间名字不一致,需要映射
                    .MatchMembers((x, y) =>
            {
                if (x == "address" && y == "userAddress")
                {
                    return(true);
                }
                return(x == y);
            })

                    //有外键关联,需要映射出外键所带名字
                    .ConvertUsing <Inner, Inner2>(value => new Inner2 {
                D12 = value.D1, D22 = value.D2
            })
                    //需要对某一个字段进行特殊处理
                    .PostProcess <Dest>((value, state) =>
            {
                value.F = "nothing";
                return(value);
            })
                    );

            Dest dst = mapper1.Map(src);
        }
Пример #7
0
        public void MapperMethod() {

            AutoMapperInit();

            StudentDo xiaoming = new StudentDo() { StudentName = "小明", Age = 10 };
            SchoolDo xiaoxue = new SchoolDo() { SchoolName = "义津小学", Address = "安徽安庆枞阳" };
            ClassDo banji_01 = new ClassDo() { ClassName = "401班", ClassLevel = 1, StudentNum = 50 };
            xiaoxue.ClassInfo = banji_01;

            SchoolDo zhongxue = new SchoolDo() { SchoolName = "义津中学", Address = "安徽安庆枞阳" };
            ClassDo banji_02 = new ClassDo() { ClassName = "501班", ClassLevel = 2, StudentNum = 50 };
            zhongxue.ClassInfo = banji_02;

            xiaoming.SmallSchool = xiaoxue;
            xiaoming.MiddleSchool = zhongxue;

            Stopwatch watcher = new Stopwatch();
            watcher.Start();
            for (var i = 0; i < 100000; i++) {
                var xiaohong = AutoMapper.Mapper.Map<StudentDo, StudentDto>(xiaoming);
            }
            watcher.Stop();
            long time1 = watcher.ElapsedMilliseconds;
            Console.WriteLine("共花费{0}毫秒", time1);

            watcher.Reset();
            watcher.Restart();

            ObjectsMapper<StudentDo, StudentDto> mapper = new ObjectMapperManager().GetMapper<StudentDo, StudentDto>(new DefaultMapConfig());
            StudentDto xiaofang = null;
            for (var i = 0; i < 1000000; i++) {
                xiaofang = mapper.Map(xiaoming);
            }
            long time2 = watcher.ElapsedMilliseconds;
            Console.WriteLine("共花费{0}毫秒", time2);

            System.Threading.Thread.Sleep(5000);

            Console.ReadLine();

        }
Пример #8
0
        public void MapperMethod()
        {
            AutoMapperInit();

            StudentDo xiaoming = new StudentDo()
            {
                StudentName = "小明", Age = 10
            };
            SchoolDo xiaoxue = new SchoolDo()
            {
                SchoolName = "义津小学", Address = "安徽安庆枞阳"
            };
            ClassDo banji_01 = new ClassDo()
            {
                ClassName = "401班", ClassLevel = 1, StudentNum = 50
            };

            xiaoxue.ClassInfo = banji_01;

            SchoolDo zhongxue = new SchoolDo()
            {
                SchoolName = "义津中学", Address = "安徽安庆枞阳"
            };
            ClassDo banji_02 = new ClassDo()
            {
                ClassName = "501班", ClassLevel = 2, StudentNum = 50
            };

            zhongxue.ClassInfo = banji_02;

            xiaoming.SmallSchool  = xiaoxue;
            xiaoming.MiddleSchool = zhongxue;

            Stopwatch watcher = new Stopwatch();

            watcher.Start();
            for (var i = 0; i < 100000; i++)
            {
                var xiaohong = AutoMapper.Mapper.Map <StudentDo, StudentDto>(xiaoming);
            }
            watcher.Stop();
            long time1 = watcher.ElapsedMilliseconds;

            Console.WriteLine("共花费{0}毫秒", time1);

            watcher.Reset();
            watcher.Restart();

            ObjectsMapper <StudentDo, StudentDto> mapper = new ObjectMapperManager().GetMapper <StudentDo, StudentDto>(new DefaultMapConfig());
            StudentDto xiaofang = null;

            for (var i = 0; i < 1000000; i++)
            {
                xiaofang = mapper.Map(xiaoming);
            }
            long time2 = watcher.ElapsedMilliseconds;

            Console.WriteLine("共花费{0}毫秒", time2);

            System.Threading.Thread.Sleep(5000);

            Console.ReadLine();
        }