我有一个单独的类,用于在源和目标之间进行映射。例如。
RegistrationMapping:
public class RegistrationMapping
{
public static void Map(IProfileExpression profile)
{
profile.CreateMap<DB_Registration, Registration>()
.ForMember(x => x.EMP_ID, map => map.MapFrom(c => c.employeeID))
.ForMember(x => x.MOB_NO, map => map.MapFrom(c => c.Mobile))
.ForMember(x => x.EMAIL_ID, map => map.MapFrom(c => c.EmailID))
}
}
以类似的方式,我也有用于其他映射的类。
现在在我的存储库中,我想这样使用
// I want to achieve below code in a generic way.
var config = new MapperConfiguration(cfg => cfg.AddProfile(/*RegistrationMapping goes here*/));
var mappedConfigurations = config.GetAllTypeMaps(); // This line of code is needed for my other purpose(get unmapped properties)
任何帮助将不胜感激。
答案 0 :(得分:1)
AutoMapper允许通过传递程序集,程序集名称或程序集中包含的类型来加载配置文件。
您的RegistrationMapping类和其他类必须从AutoMapper.Profile继承。
您可以这样加载个人资料:
按类型:
Mapper.Initialize(x => x.AddProfile<RegistrationMapping>());
Mapper.Initialize(x => x.AddProfile(typeof(RegistrationMapping)));
按实例:
Mapper.Initialize(x => x.AddProfiles(new List<Profile> { new RegistrationMapping() }));
Mapper.Initialize(x => x.AddProfile(new RegistrationMapping()));
通过程序集名称:
Mapper.Initialize(x => x.AddMaps("MyApplication.RegistrationMapping"));
Mapper.Initialize(x => x.AddMaps(new string[] {"MyApplication.RegistrationMapping"}));
通过组装:
Mapper.Initialize(x => x.AddMaps(Assembly.GetExecutingAssembly()));
如果要从已加载的程序集中加载所有概要文件:
var profiles = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes().Where(type => typeof(Profile).IsAssignableFrom(type)));
Mapper.Initialize(x => x.AddMaps(profiles));
有关更多信息,请参见AutoMapper Configuration文档和source code。