Package AutoMapper netcore 2.2

In the last chapter we inherit autoMapper the Profile class by creating a class

    public class Mappings : Profile
    {
        public Mappings()
        {
            CreateMap<UserDto, TbUser>();
        }
    }

Doing so there is a flaw, that is when the transfer object lot of time, also need to manually configure the mapping between one and maintain it also relatively trouble.

Package AutoMapper

Creating AutoMapperExtension extension class

    /// <summary>
    /// AutoMapper扩展类
    /// </summary>
    public static class AutoMapperExtension
    {
        ///  <Summary> 
        /// to map the source object to the target object
         ///  </ Summary> 
        ///  <typeParam name = "T"> entity type </ typeParam> 
        ///  <param name = "obj" > source object </ param> 
        ///  <Returns> entity after conversion </ returns>

        public static T MapTo<T>(this object obj)
        {
            if (obj == null) throw new Exception("异常");
            var config = new MapperConfiguration(ctx => ctx.CreateMap(obj.GetType(), typeof(T)));
            var mapper = config.CreateMapper();
            return mapper.Map<T>(obj);
        }

        ///  <Summary> 
        /// to map the source object to the target object
         ///  </ Summary> 
        ///  <typeParam name = "T"> entity type </ typeParam> 
        ///  <param name = "DTO" > source object </ param> 
        ///  <param name = "entity"> audience </ param> 
        ///  <Returns> entity after conversion </ Returns> 
        public  static T the Map <T> ( Object DTO, T entity)
        {
            var config = new MapperConfiguration(ctx => ctx.CreateMap(dto.GetType(), typeof(T)));
            var mapper = config.CreateMapper();
            var newModel = mapper.Map(dto, entity);
            return newModel;
        }
    }

Modify the business layer

        ///  <Summary> 
        /// Add User
         ///  </ Summary> 
        ///  <param name = "DTO"> physical transmission Object </ param> 
        ///  <Returns> </ Returns> 
        public  int the Add ( UserDto dto)
        {
            //var info = _mapper.Map<UserDto, TbUser>(dto);
            var info = dto.MapTo<TbUser>();
            info.AddTime = DateTime.Now;
            info.UserId = Guid.NewGuid().ToString("N");
            //var user = new TbUser
            //{
            //    UserId = Guid.NewGuid().ToString("N"),
            //    Email = dto.Email,
            //    UserName = dto.UserName,
            //    AddTime = DateTime.Now
            //};
            _dbContext.Add(info);
            return _dbContext.SaveChanges();
        }
        ///  <Summary> 
        /// edit user information
         ///  </ Summary> 
        ///  <param name = "DTO"> physical transmission Object </ param> 
        ///  <Returns> </ Returns> 
        public  int the Update (UserDto dto)
        {
            var User = _dbContext.TbUsers.Find (dto.UserId);
             IF (User == null ) the throw  new new Exception ( " get user information failed " );
             // user.username = dto.UserName;
             // user.email = DTO .Email; 
            AutoMapperExtension.Map (DTO, User);
             return _dbContext.SaveChanges ();
        }

Run the test

Pre-packaged with the same effect

Guess you like

Origin www.cnblogs.com/tenghao510/p/11978076.html