如何使用Automapper映射此复杂类型

时间:2015-06-11 09:31:01

标签: c# automapper

我必须映射每个属性,因为属性名称不匹配。

我是如何手动完成的:

void method1(){
    SuperType randomInstance = getRandomInstance();
    if (randomInstance instanceof Stoppable) {
        Stoppable sInstance = (Stoppable) randomInstance;
        if(sInstance.getStop() == ...) //do something
    }
}

这是我尝试使用AutoMapper

        var api = new MovieApi();
        var movie = await api.GetMovieAsync(id);

        var images = await api.GetMovieImages(movie.id);
        var configuration = await api.GetConfiguration();

            //Start the main mapping
            var model = new MovieViewModel()
            {
                Title = movie.title,
                Overview = movie.overview,
                ReleaseDate = movie.release_date,
                Status = movie.status,
                Tagline = movie.tagline,
                VoteAverage = movie.vote_average,
                VoteCount = movie.vote_count,
                Revenue = movie.revenue,
                Video = movie.video,
                TmdbId = movie.id,
                Images = new List<Images>(),
                Genre = new List<Genre>()
                {
                    //Map the Genre
                    new Genre()
                    {
                        Id = movie.genres[0].id,
                        Name = movie.genres[0].name
                    }
                }
            };

            //Add images to the model if they exist.
            if (images.backdrops != null)
            {
                foreach (var image in images.backdrops)
                {
                    model.Images.Add(new Images()
                    {
                        VoteAverage = image.vote_average,
                        VoteCount = image.vote_count,
                        AspectRatio = image.aspect_ratio,
                        FilePath = image.file_path,
                        Height = image.height,
                        Iso6391 = image.iso_639_1,
                        Width = image.width,
                        FullImageUri =
                            new Uri(configuration.images.base_url + configuration.images.backdrop_sizes[3] +
                                    image.file_path)
                    });
                }
            }

我得到了例外:

       public MovieViewModel MapForDetails(MovieContent movie, Backdrop images, Configuration config)
    {
        var model = new MovieViewModel();

        Mapper.CreateMap<MovieContent, MovieViewModel>()
            .ForMember(d => d.Title, o => o.MapFrom(s => s.title))
            .ForMember(d => d.Overview, o => o.MapFrom(s => s.overview))
            .ForMember(d => d.TmdbId, o => o.MapFrom(s => s.id))
            .ForMember(d => d.ReleaseDate, o => o.MapFrom(s => s.release_date))
            .ForMember(d => d.Popularity, o => o.MapFrom(s => s.popularity))
            .ForMember(d => d.VoteAverage, o => o.MapFrom(s => s.vote_average))
            .ForMember(d => d.VoteCount, o => o.MapFrom(s => s.vote_count))
            .ForMember(d => d.Status, o => o.MapFrom(s => s.status))
            .ForMember(d => d.Tagline, o => o.MapFrom(s => s.tagline))
            .ForMember(d => d.Revenue, o => o.MapFrom(s => s.revenue))
            .ForMember(d => d.Video, o => o.MapFrom(s => s.video))
            .ForMember(d => d.TmdbId, o => o.MapFrom(s => s.id))
            //.ForMember(d => d.Genre, o => o.MapFrom(s => s.genres))
            .ForMember(d => d.Images, o => o.MapFrom(s => new List<MovieImages>()));

        model = Mapper.Map<MovieContent, MovieViewModel>(movie);

        Mapper.CreateMap<Backdrop, MovieViewModel>()
            .ForMember(d => d.Images.Select(x => x.Width), o => o.MapFrom(s => s.width))
            .ForMember(d => d.Images.Select(x => x.Iso6391), o => o.MapFrom(s => s.iso_639_1))
            .ForMember(d => d.Images.Select(x => x.VoteAverage), o => o.MapFrom(s => s.vote_average))
            ;

        model.Images = (Mapper.Map<Backdrop, MovieViewModel>(images).Images);

        return model;
    }

我不确定如何将其映射到AutoMapper.AutoMapperConfigurationException : Custom configuration for members is only supported for top-level individual members on a type. 对象?

1 个答案:

答案 0 :(得分:1)

配置:

Mapper.CreateMap<MovieContent, MovieViewModel>()
            .ForMember(d => d.Title, o => o.MapFrom(s => s.title))
            .ForMember(d => d.Overview, o => o.MapFrom(s => s.overview))
            .ForMember(d => d.TmdbId, o => o.MapFrom(s => s.id))
            .ForMember(d => d.ReleaseDate, o => o.MapFrom(s => s.release_date))
            .ForMember(d => d.Popularity, o => o.MapFrom(s => s.popularity))
            .ForMember(d => d.VoteAverage, o => o.MapFrom(s => s.vote_average))
            .ForMember(d => d.VoteCount, o => o.MapFrom(s => s.vote_count))
            .ForMember(d => d.Status, o => o.MapFrom(s => s.status))
            .ForMember(d => d.Tagline, o => o.MapFrom(s => s.tagline))
            .ForMember(d => d.Revenue, o => o.MapFrom(s => s.revenue))
            .ForMember(d => d.Video, o => o.MapFrom(s => s.video))
            .ForMember(d => d.TmdbId, o => o.MapFrom(s => s.id))
            .ForMember(d => d.Genre, o => o.Ignore())
            .ForMember(d => d.Images, o => o.Ignore());

Mapper.CreateMap<Backdrop, Images>()
            .ForMember(d => d.Width, o => o.MapFrom(s => s.width))
            .ForMember(d => d.Iso6391, o => o.MapFrom(s => s.iso_639_1))
            .ForMember(d => d.VoteAverage, o => o.MapFrom(s => s.vote_average));

映射代码:

var model = new MovieViewModel();
model = Mapper.Map<MovieContent, MovieViewModel>(movie);

//Check if Null
if (images.backdrops != null)
{
    model.Images = Mapper.Map<IEnumerable<Backdrop>, IEnumerable<Images>>(images.backdrops);
}