AutoMapper 文件大小格式化

AutoMapper file size formatting

我写了一个 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();

现在如您所见,我有另一个 class 我想用它,但我不想创建一个新的 ValueResolver 来做同样的事情?

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

如果您查看 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();