cSt*_*off 5 c# automapper visual-studio-extensions
我有很多映射配置文件,如何从源代码中的特定映射操作中找到正确的映射配置?Resharper 或 Visual Studio 扩展可以提供帮助吗?
我想从这里跳:
var userDto = this.mapper.Map<User, UserDto>(user);
Run Code Online (Sandbox Code Playgroud)
对此:
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, UserDto>(MemberList.None);
/*etc...*/
});
Run Code Online (Sandbox Code Playgroud)
有可能的?我怎么能意识到呢?
将您的映射隔离在它自己的类中怎么样?
public class UserProfile: Profile
{
public UserProfile()
{
CreateMap<User, UserDto>(MemberList.None);
}
}
Run Code Online (Sandbox Code Playgroud)
您的个人资料注册可能有所不同,但以下是动态注册它们的方法。
public class MapperProvider
{
private readonly Container container;
public MapperProvider(Container container)
{
this.container = container;
}
public IMapper GetMapper()
{
var mce = new MapperConfigurationExpression();
mce.ConstructServicesUsing(container.GetInstance);
MapperConfiguration mc = GetMapperConfiguration(mce);
IMapper m = new Mapper(mc, t => container.GetInstance(t));
return m;
}
public static MapperConfiguration GetMapperConfiguration(MapperConfigurationExpression mce)
{
var profiles = typeof(ApplicationProfile).Assembly.GetTypes()
.Where(t => typeof(Profile).IsAssignableFrom(t))
.ToList();
mce.AddProfiles(profiles);
var mc = new MapperConfiguration(mce);
mc.AssertConfigurationIsValid();
return mc;
}
}
Run Code Online (Sandbox Code Playgroud)
这样,您就可以按名称搜索该配置文件类,并轻松导航到您的映射。
如果您知道用户映射在类中,它将起作用UserProfile。
| 归档时间: |
|
| 查看次数: |
5133 次 |
| 最近记录: |