首页 > 解决方案 > 如何在 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”吗?

标签: automapperasp.net-mvc-5.2

解决方案


推荐阅读