小编seb*_*ebd的帖子

在Automapper中使用配置文件以不同的逻辑映射相同的类型

我在我的ASP.NET MVC网站中使用AutoMapper将我的数据库对象映射到ViewModel对象,我试图使用几个配置文件来映射相同的类型,但使用另一个逻辑.我想通过阅读Matt的博客文章这样做,他说:

真正关键的部分是AutoMapper配置文件.您可以使用配置文件对配置进 也许在一个配置文件中,您可以通过一种方式格式化日期,而在另一种配置文 我在这里只使用一个配置文件.

所以我为一个案例创建了一个配置文件:

public class MyProfile : Profile
{
    protected override string ProfileName
    {
        get
        {
            return "MyProfile";
        }
    }

    protected override void Configure()
    {
        CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>();
    }
}

public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String>
{
    public string Convert(DateTime source)
    {
        return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
    }
}
Run Code Online (Sandbox Code Playgroud)

还有另一个案例:

public class MyProfile2 : Profile
{
    protected override string ProfileName
    {
        get
        {
            return "MyProfile2";
        }
    }

    protected override void Configure()
    {
        CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>();
    }
}

public class …
Run Code Online (Sandbox Code Playgroud)

c# automapper

37
推荐指数
1
解决办法
4万
查看次数

标签 统计

automapper ×1

c# ×1