I had defined in my project a global Automapper configuration that would allow me to use Mapper.Map<targetType>(sourceObject);
in my code. (See my configuration below.)
I updated the NuGet package, and I see the message that Mapper.Map is obsolete/depreciated. I went back to Automapper on GitHub and see examples like this:
[Test]
public void Example()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source1, SubDest1>().FixRootDest();
cfg.CreateMap<Source2, SubDest2>().FixRootDest();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var subDest1 = mapper.Map<Source1, SubDest1>(new Source1 {SomeValue = "Value1"});
var subDest2 = mapper.Map<Source2, SubDest2>(new Source2 {SomeOtherValue = "Value2"});
subDest1.SomeValue.ShouldEqual("Value1");
subDest2.SomeOtherValue.ShouldEqual("Value2");
}
Am I going to have to create a configuration in EVERY method that uses a mapping?
My current global configuration:
namespace PublicationSystem.App_Start
{
public class AutoMapperConfig
{
public static void CreateMaps()
{
CreateProjectMaps();
}
private static void CreateProjectMaps()
{
Mapper.CreateMap<Project, ProjectCreate>();
Mapper.CreateMap<Project, ProjectSelectable>();
//...
}
}
}
UPDATE: Thanks to some coaching from Scott Chamberlain I have created a class like this:
public class MkpMapperProfile : AutoMapper.Profile
{
protected override void Configure()
{
this.CreateMap<Project, ProjectCreate>();
this.CreateMap<Project, ProjectSelectable>();
this.CreateMap<Project, ProjectDetails>();
// Many Many other maps
}
}
I'm thinking I should have the 'MapperConfiguration' in my BaseController class. I started to do something like this:
public partial class BaseController : Controller
{
private MapperConfiguration mapConfig;
public BaseController()
{
db = new MkpContext();
SetMapperConfig();
}
private void SetMapperConfig()
{
mapConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MkpMapperProfile>();
});
}
public BaseController(MapperConfiguration config)
{
db = new MkpContext();
this.mapConfig = config;
}
}
Am I on the right track?
答案 0 :(得分:37)
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:
calc()
Then, wherever dependencies are wired-up (ex: Global.asax or Startup), create a MapperConfiguration and then use it to create an IMapper.
internal class MappingProfile : Profile
{
protected override void Configure()
{
CreateMap<Project, ProjectCreate>();
}
}
Then, use the configuration to generate an IMapper:
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});
Then, register that mapper with the dependency builder (I'm using Autofac here)
var mapper = mapperConfiguration.CreateMapper();
Now, wherever you need to map stuff, declare a dependency on IMapper:
builder.RegisterInstance(mapper).As<IMapper>();
答案 1 :(得分:10)
我正在使用5.2.0版,支持在构造函数中创建映射,而不是覆盖configure。
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Project, ProjectDto>();
}
}
在Global.asax.cs中调用如:
Mapper.Initialize(c=>c.AddProfile<MappingProfile>());
希望得到这个帮助。
答案 2 :(得分:7)
这是AutoMapper 4.2中的新功能。 Jimmy Bogard就此发表了一篇博文:Removing the static API from AutoMapper。它声称
IMapper接口要轻得多,底层类型现在只关心执行地图,消除了很多线程问题......
新语法:(粘贴自博客文章)
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<User, UserDto>();
});
如果你只是想要这样做的“老方法”。最新的版本4.2.1 带回了一些传统。只需使用
CreateMap<Project, ProjectCreate>();
而不是
Mapper.CreateMap<Project, ProjectCreate>();
旧代码可以正常工作。
答案 3 :(得分:2)
You can find configured AutoMapper 4.2 in my ASP.NET MVC Template project here: https://github.com/NikolayIT/ASP.NET-MVC-Template
Create these classes: https://github.com/NikolayIT/ASP.NET-MVC-Template/tree/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web.Infrastructure/Mapping
Annotate view models with <input onchange="myf();" type="number" name="font_size_number" id="mycontroller">
<script>
function myf(){
document.getElementById('mytext').style.fontSize='"'+document.getElementById('mycontroller').value+'px"';
}
</script>
<text id="mytext">tarara</text>
: https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20Core/Web/AspNetCoreTemplate.Web/ViewModels/Settings/SettingViewModel.cs
Use it as IMapFrom<>
. Example: https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web/Controllers/HomeController.cs#L27
答案 4 :(得分:1)
Mapper.Initialize(cfg => {
cfg.CreateMap<Source, Dest>();
});