当前位置: 移动技术网 > IT编程>开发语言>.net > NetCore+AutoMapper多个对象映射到一个Dto对象

NetCore+AutoMapper多个对象映射到一个Dto对象

2019年09月19日  | 移动技术网IT编程  | 我要评论

打印机驱动装不上,心云简谱,英语四级真题

 

目录

一、定义源映射类和被映射类dto

二、注入automapper

三、配置映射

四、调用automapper完成赋值

五、运行测试

 

一、定义源映射对象

为了体现automapper映射特性,在socialattribute中的name属性没有定义在people中,people的ear属性也不存在与socialattribute和physicalattribute中。

 

 

 代码如下:

    /// <summary>
    /// 身体属性
    /// </summary>
    public class physicalattribute
    {
        public string eye { get; set; }
        public string mouth { get; set; }
    }
    /// <summary>
    /// 社会属性
    /// </summary>
    public class socialattribute
    {
        public int age { get; set; }
        public bool ismarried { get; set; }
        public string name { get; set; }
    }
    public class peopledto
    {
        public string eye { get; set; }
        public string mouth { get; set; }
        public string ear { get; set; }
        public int age { get; set; }
        public bool ismarried { get; set; }
    }

 

二、注入automapper

例子中使用的ioc容器是autofac,不使用autofac的话,仅用netcore框架集成的ioc容器也可以实现。

注册automapper必要组件:

    public static class automapperinjection
    {
        public static containerbuilder loadautomapper(this containerbuilder builder)
        {
            builder.registertype<mapperconfigurationexpression>().singleinstance();
            builder.register(m => {
                var mapperconfigurationexpression = m.resolve<mapperconfigurationexpression>();
                var instance = new mapperconfiguration(mapperconfigurationexpression);
                return instance;
            });
            builder.register(m => {
                var mapperconfiguration = m.resolve<mapperconfiguration>();
                return mapperconfiguration.createmapper();
            });
            return builder;
            
        }
    }

 

三、配置映射

formember:映射两个类之间的属性关系。

people类中的ear属性并不存在于任何映射源类中,我们可以使用formember(m => m.ear, n => n.ignore())忽略该属性,当然也可以不写这段代码,对automapper不会有任何影响,但是为了后期维护更方便,我比较习惯将dto类的属性写全。

socialattribute类中的name属性不存在与people类中,直接忽略它,name也不会被automapper赋值。

    public class automapperprofile: profile
    {
        public void mapping(ilifetimescope scope) {

            var expression = scope.resolve<mapperconfigurationexpression>();
            expression.createmap<physicalattribute, peopledto>()
            .formember(m => m.eye, n => n.mapfrom(s => s.eye))
            .formember(m => m.mouth, n => n.mapfrom(s => s.mouth));
            //.formember(m => m.ear, n => n.ignore());
            expression.createmap<socialattribute, peopledto>()
                .formember(m => m.age, n => n.mapfrom(s => s.age))
                .formember(m => m.ismarried, n => n.mapfrom(s => s.ismarried));
        }
    }

四、调用automapper完成赋值

调用automapper,将physicalattribute和socialattribute的值赋给peopledto

    public class dtohelper
    {
        private imapper mapper;
        public dtohelper(imapper _mapper) {
            mapper = _mapper;
        }
        public peopledto getdto(physicalattribute physical,socialattribute social) {
            peopledto peopledto = new peopledto();
            mapper.map(social, mapper.map(physical, peopledto));
            return peopledto;
        }
    }

 

五、运行测试

测试框架使用的xunit

    public class dtohelpertest
    {
        [fact]
        public void getdto() {
            //moke
            containerbuilder builder = new containerbuilder();
            builder.loadautomapper();
            builder.registertype<automapperprofile>();
            icontainer container = builder.build();
            using (var scope = container.beginlifetimescope())
            {
                scope.resolve<automapperprofile>().mapping(scope);
                peopledto result = new peopledto() { eye = "双眼皮", mouth = "红润", age = 18, ismarried = false };
                physicalattribute physical = new physicalattribute() { eye = "双眼皮", mouth = "红润" };
                socialattribute social = new socialattribute() { name = "张三", ismarried = false, age = 18 };
                peopledto output = new dtohelper(scope.resolve<imapper>()).getdto(physical, social);
                //assert.same(result, output);
                assert.equal(jsonconvert.serializeobject(result), jsonconvert.serializeobject(output));
            }

        }
    }

通过测试!

 

 参考项目:https://github.com/fb208/codespace/tree/master/codespace.csharp/webmvc/democlass/automapperdemo

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网