从src映射构造函数参数对象的字段

时间:2015-12-27 17:52:13

标签: c# stream automapper

我正在尝试映射对象包含ctor参数我需要从src对象设置哪些字段(从Image类到ImageDownloadResponse类):

public class Image
{
    public Guid Id { get; set; }
    public byte[] ImageData { get; set; }
    public string ImageMimeType { get; set; }
}

public class ImageDownloadResponse
{
    public ImageDownloadResponse(ImageMetaData metaData, Stream stream)
    {
        DownloadedImageMetadata = metaData;
        ImageByteStream = stream;
    }
    public ImageMetaData DownloadedImageMetadata;
    public Stream ImageByteStream;
}

public class ImageMetaData
{
    public ImageMetaData(Guid imageId, string imageMimeType)
    {
        ImageId = imageId;
        ImageMimeType = imageMimeType;
    }
    public Guid ImageId;
    public string ImageMimeType;
}

这是我在映射中得到的结果(不是有效代码'因为我无法在after参数后构造新对象):

Mapper.CreateMap<Image, ImageDownloadResponse>()
    .ConstructUsing(x => new ImageDownloadResponse(new ImageMetaData(x.Id, x.ImageMimeType), Stream.Null ))
    .ForMember(dest => dest.DownloadedImageMetadata,
                opt => new ImageMetaData(opt.MapFrom(src => src.Id), opt.MapFrom(src => src.ImageMimeType)))
    .AfterMap((image, response) =>
    {
        response.ImageByteStream = new MemoryStream(image.ImageData);
    });

此外,我不确定使用AfterMap来设置来自字节数组的流是非常好的。有人可以纠正我映射这些对象的方式吗?

0 个答案:

没有答案