AutoMapper文件大小格式

时间:2015-01-28 14:00:24

标签: c# asp.net .net automapper

我已经写了一个ValueResolver来将文件大小转换为更好看的字符串版本。问题是,ValueResolver采用特定的对象类型。

有没有办法编写相同的代码,但是能够将它应用于我想要的任何数字属性吗?

这是ValueResolver

public class ImageSizeResolver : ValueResolver<Image, string>
{
    protected override string ResolveCore(Image source)
    {
        if (source.Size == 0)
        {
            return "n/a";
        }

        var exponent = (short)(Math.Min(Math.Floor(Math.Log(source.Size) / Math.Log(1024)), 8));
        var size = source.Size / Math.Pow(1024, exponent);
        var result = size.ToString("0.##");

        switch (exponent)
        {
            case 0:
                return result + "b";
            case 1:
                return result + "kb";
            case 2:
                return result + "mb";
            case 3:
                return result + "gb";
            case 4:
                return result + "tb";
            case 5:
                return result + "pb";
            case 6:
                return result + "eb";
            case 7:
                return result + "zb";
            case 8:
                return result + "yb";
        }

        return "unknown";
    }
}

在我的autoMapper配置文件中,我这样应用

Mapper.CreateMap<Image, ImageModel>()
    .ForMember(dest => dest.Size, opt => opt.ResolveUsing<ImageSizeResolver>())
    .ReverseMap();

现在你可以看到,我有另一个类我想要使用它,但我不想创建一个新的ValueResolver做同样的事情?

Mapper.CreateMap<Document, DocumentModel>()
    .ForMember(dest => dest.Size, opt => opt.ResolveUsing<ImageSizeResolver>())    <--- Will error currently
    .ReverseMap();

1 个答案:

答案 0 :(得分:1)

如果您查看wiki for value converters和最后一部分,有一个您正在寻找的示例,但简而言之,您为要更改的类型创建转换器,然后稍微改变它

public class SizeResolver : ValueResolver<int, string>
{
    protected override string ResolveCore(int source)
    {
        if (source == 0)
        {
            return "n/a";
        }

        var exponent = (short)(Math.Min(Math.Floor(Math.Log(source) / Math.Log(1024)), 8));
        var size = source/ Math.Pow(1024, exponent);
        var result = size.ToString("0.##");

        switch (exponent)
        {
            case 0:
                return result + "b";
            case 1:
                return result + "kb";
            case 2:
                return result + "mb";
            case 3:
                return result + "gb";
            case 4:
                return result + "tb";
            case 5:
                return result + "pb";
            case 6:
                return result + "eb";
            case 7:
                return result + "zb";
            case 8:
                return result + "yb";
        }

        return "unknown";
    }
}

然后用

调用此转换器
Mapper.CreateMap<Image, ImageModel>()
    .ForMember(dest => dest.Size, 
               opt => opt.ResolveUsing<SizeResolver>()
                         .FromMember(src=> src.Size))
    .ReverseMap();

Mapper.CreateMap<Document, DocumentModel>()
    .ForMember(dest => dest.Size, 
               opt => opt.ResolveUsing<SizeResolver>()
                         .FromMember(src=> src.Size))
    .ReverseMap();