Automapper says Mapper.Map is obsolete, global mappings?

后端 未结 5 1211
孤街浪徒
孤街浪徒 2020-12-12 21:01

I had defined in my project a global Automapper configuration that would allow me to use Mapper.Map(sourceObject); in my code. (See my configu

相关标签:
5条回答
  • 2020-12-12 21:34

    This is new in AutoMapper 4.2. There is a blog post by Jimmy Bogard on this: Removing the static API from AutoMapper. It claims that

    The IMapper interface is a lot lighter, and the underlying type is now just concerned with executing maps, removing a lot of the threading problems...

    The new syntax: (pasted from the blog post)

    var config = new MapperConfiguration(cfg => {
      cfg.CreateMap<User, UserDto>();
    });
    

    If you just want the "old way" of doing this. The latest version 4.2.1 has brought back some tradition. Just use

    CreateMap<Project, ProjectCreate>();
    

    instead of

    Mapper.CreateMap<Project, ProjectCreate>();
    

    The old code will work just fine.

    0 讨论(0)
  • 2020-12-12 21:41

    I'm using version 5.2.0, support to create maps in constructors instead of override configure.

    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<Project, ProjectDto>();
        }
    }
    

    In Global.asax.cs call like:

    Mapper.Initialize(c=>c.AddProfile<MappingProfile>());
    

    Hope this help.

    0 讨论(0)
  • 2020-12-12 21:46

    You can find configured AutoMapper 4.2 in my ASP.NET MVC Template project here: https://github.com/NikolayIT/ASP.NET-MVC-Template

    1. Create these classes: https://github.com/NikolayIT/ASP.NET-MVC-Template/tree/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web.Infrastructure/Mapping

    2. Annotate view models with IMapFrom<>: https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20Core/Web/AspNetCoreTemplate.Web/ViewModels/Settings/SettingViewModel.cs

    3. Use it as .To<SomeViewModel>(). Example: https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web/Controllers/HomeController.cs#L27

    0 讨论(0)
  • 2020-12-12 21:49

    This is how I've handled it.

    Create maps in a Profile, taking care to use the Profile's CreateMap method rather than Mapper's static method of the same name:

    internal class MappingProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Project, ProjectCreate>();
        }
    }
    

    Then, wherever dependencies are wired-up (ex: Global.asax or Startup), create a MapperConfiguration and then use it to create an IMapper.

    var mapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new MappingProfile());
        });
    

    Then, use the configuration to generate an IMapper:

    var mapper = mapperConfiguration.CreateMapper();
    

    Then, register that mapper with the dependency builder (I'm using Autofac here)

    builder.RegisterInstance(mapper).As<IMapper>();
    

    Now, wherever you need to map stuff, declare a dependency on IMapper:

    internal class ProjectService : IProjectService {
        private readonly IMapper _mapper;
        public ProjectService(IMapper mapper) {
             _mapper = mapper;
        }
        public ProjectCreate Get(string key) {
            var project = GetProjectSomehow(key);
            return _mapper.Map<Project, ProjectCreate>(project);
        }
    }
    
    0 讨论(0)
  • 2020-12-12 22:01
    Mapper.Initialize(cfg => {
        cfg.CreateMap<Source, Dest>();
    });
    
    0 讨论(0)
提交回复
热议问题