automapper - 如何在 MVC 5 中设置 Atommaper v 9.0?
问题描述
在之前的项目中,我使用 Automapper v7.0.1,一切都很好。
现在我正在开发一个新项目,我再次使用 MVC 5 和 .Net framework 4.8 制作了我的架构,但我发现在新版本中我使用 PROFILE 的配置无法正常工作,我已经在网上尝试了一些东西,但是我无法让它正常工作。
您能帮我配置一下以及如何在这个新版本中使用“PROFILE”吗?
谢谢..
使用 V7.0.1 进行设置
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<ViewModelToDomainMappingProfile>();
x.AddProfile<DomainToViewModelMappingProfile>();
});
}
}
“简介”1
public class ViewModelToDomainMappingProfile : Profile
{
public ViewModelToDomainMappingProfile()
{
CreateMap<ListAccountUsersViewModel, ApplicationUser>();
}
}
“简介”2
public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
ConfigureMappings();
}
/// <summary>
/// Creates a mapping between source (Domain) and destination (ViewModel)
/// </summary>
private void ConfigureMappings()
{
CreateMap<ApplicationUser, ListAccountUsersViewModel>();
}
}
当我在“控制器”中使用它时,它工作正常:
var listUserModel = accountUserService.GetUserList();
var mapperListUserModel = Mapper.Map<IEnumerable<ApplicationUser>, IEnumerable<ListAccountUsersViewModel>>(listUserModel);
现在,使用 v9.0 进行配置
public class AutoMapperConfiguration
{
public static IMapper Mapper { get; private set; }
public static MapperConfiguration MapperConfiguration { get; private set; }
public static void Configure()
{
MapperConfiguration = new MapperConfiguration(x =>
{
x.AddProfile(new ViewModelToDomainMappingProfile());
x.AddProfile(new DomainToViewModelMappingProfile());
});
Mapper = MapperConfiguration.CreateMapper();
}
}
“PROFILE 1”和“PROFILE 2”它们保持不变
当我在带有 Autofac 的 Bootstrapper 中使用它时:
var assemblyNames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
var assembliesTypes = assemblyNames
.Where(a => a.Name.Equals("Seccion.Web.Mappings", StringComparison.OrdinalIgnoreCase))
.SelectMany(an => Assembly.Load(an).GetTypes())
.Where(p => typeof(Profile).IsAssignableFrom(p) && p.IsPublic && !p.IsAbstract)
.Distinct();
var autoMapperProfiles = assembliesTypes
.Select(p => (Profile)Activator.CreateInstance(p)).ToList();
builder.Register(ctx => new MapperConfiguration(cfg =>
{
foreach (var profile in autoMapperProfiles)
{
cfg.AddProfile(profile);
}
}));
builder.Register(ctx => ctx.Resolve<MapperConfiguration>().CreateMapper()).As<IMapper>().InstancePerLifetimeScope();
因为我在“控制器”中使用它并且不工作:
private readonly MapperConfiguration config;
//CTOR
public AccountUserController(IAccountUserService accountUserService, MapperConfiguration config)
{
this.accountUserService = accountUserService;
this.config = config;
}
// ActionResult
var mapper = config.CreateMapper();
var listUserModel = accountUserService.GetUserList();
//Here send Error: Missing type map configuration or unsupported mapping.
var mapperListUserModel = mapper.Map<IEnumerable<ApplicationUser>, IEnumerable<ListAccountUsersViewModel>>(listUserModel);
您能帮我配置一下以及如何在这个新版本中使用“PROFILE”吗?
解决方案
推荐阅读
- python - 为什么 np.convolve 将结果信号移动 1
- javascript - React-Native Javascript 搜索数组
- raku - 如果在命令行上没有指定文件,实现使用 STDIN 的标准 Unix 行为的惯用方法?
- java - Android:屏幕旋转后,活动方向并不总是改变
- angularjs - 如何在 angularjs 1.5 中动态更改 ng-reapet 中的对象名称
- rust - 如何使用 serde 读取 JSON 文件?
- html - 内联显示 div
- arrays - 如何根据来自restapi的传入值显示数组项的属性?
- php - 基本 PHP 文件上传,适用于本地主机但不适用于服务器
- firefox - 如何为 Puppeteer 安装 Firefox?