5

我是 ASP.NET BoilerPlate (ABP) 的新手,我正在尝试了解如何使用 AutoMapper 创建自定义映射,也许还有 ABP automapper 属性:AutoMap、AutoMapFrom、AutoMapTo。

使用 ABP,我可以通过这种方式映射两个类:

[AutoMapTo(typeof(DestClass)]
public class SourceClass {
    public string A { get; set; }
    public string B { get; set; }
}

public class DestClass {
    public string A { get; set; }
    public string B { get; set; }
}

但是,如果我有两个像下面这样的类,我希望将属性 AB 自动映射为 A 和 B 的连接:

[AutoMapTo(typeof(DestClass)]
public class SourceClass {
    public string A { get; set; }
    public string B { get; set; }
}

public class DestClass {
    public string AB { get; set; }
}

ABP 有一些属性吗?还是我需要使用“经典”AutoMapper 代码:

Mapper.CreateMap<SourceClass, DestClass>()
    .ForMember(dest => dest.AB,
        opts => opts.MapFrom(src => (src.A + ", " + src.B)));

我必须在哪里放置这样的初始化代码?

4

2 回答 2

2

无需列出模块上的所有客户映射。只需告诉模块查找所有扩展 AutoMapper.Profile 的类:

Assembly thisAssembly = typeof(AbpProjectNameApplicationModule).GetAssembly();
IocManager.RegisterAssemblyByConvention(thisAssembly);
cfg.AddProfiles(thisAssembly);
于 2017-08-18T04:10:20.067 回答
2

我找到了我在这里分享的解决方案。

  1. 在“MyProject.Application”项目中,我定义了我的自动映射器自定义(我使用了配置文件):
   public class MyProjectAutoMapperProfile : AutoMapper.Profile {

        protected override void Configure() {
            CreateMap<SourceClass, DestClass>()
                .ForMember(dest => dest.AB,
                           opts => opts.MapFrom(src => (src.A + ", " + src.B)));
            // other customs here...
        }
  1. 然后我在 MyProjectApplicationModule 类的 Initialize 方法中注册它以进行注入:
    [DependsOn(typeof(MyProjectCoreModule), typeof(AbpAutoMapperModule))]
    public class MyProjectApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            // --- MY CODE for registering custom automapping
            var mapperConfiguration = new MapperConfiguration(cfg => {
                cfg.AddProfile(new MyProjectMapperProfile());  // <= here my custom mapping
            });

            var mapper = mapperConfiguration.CreateMapper();
            IocManager.IocContainer.Register(
                    Castle.MicroKernel.Registration.Component.For<IMapper>().Instance(mapper)
                );
            // --- MY CODE
        }
    }

请注意,我直接使用了 Castle IOC 注册方法,因为在 ABP 中我没有找到任何有用的对象注册方法。你认识一个吗?

  1. 最后,我在我的应用程序服务中使用我的自定义映射作为注入并直接使用它:
    public class MyAppService : MyNewHouseAppServiceBase, IMyAppService {
        // ...

        public MyAppService(IRepository<SourceClass, long> myRepository, AutoMapper.IMapper mapper) {
            _myRepo = myRepository;
            _mapper = mypper;
        }

        public async Task<DestClass> GetSource(long id) {
            var source = await _myRepo.Find(id);

            // USE THE INJECTED MAPPER
            return _mapper.Map<DestClass>(source);
        }

        public async Task<ListResultOutput<DestClass>> GetSources() {
            var sources = await _myRepo.GetAllListAsync();

            return new ListResultOutput<DestClass>(
                           // USE THE INJECTED MAPPER
                          _mapper.Map<List<DestClass>>(sources)
                       );
        }
   }
于 2016-05-29T09:40:16.607 回答