我在我的ASP.NET MVC网站中使用automapper将数据库对象映射到viewModel对象,我尝试使用多个配置文件映射相同的类型,但使用了另一个逻辑。我想通过阅读
Matt's blog post
他说:
真正关键的部分是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);
}
}
另一个例子:
public class MyProfile2 : Profile
{
protected override string ProfileName
{
get
{
return "MyProfile2";
}
}
protected override void Configure()
{
CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>();
}
}
public class AnotherStringFromDateTimeTypeConverter : ITypeConverter<DateTime, String>
{
public string Convert(DateTime source)
{
return source.ToString("mm - yyyy", CultureInfo.InvariantCulture);
}
}
但是,我找不到任何超负荷的
Mapper.Map<>()
方法来指定配置文件。我也看了一下
Configuration
没有运气的物体。
最后注册的配置文件始终优先。
有没有一种方法可以将配置文件用于此目的?