Profiles permit the programmer to organize maps into classes, enhancing code readability and maintainability. Any number of profiles can be created, and added to one or more configurations as needed. Profiles can be used with both the static and instance-based APIs.
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
public class UserViewModel
{
public string DisplayName { get; set; }
public string Email { get; set; }
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<User, UserViewModel>();
}
}
public class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => {
cfg.AddProfile<MappingProfile>();
//cfg.AddProfile(new MappingProfile()); // Equivalent to the above
});
var user = new User()
{
Id = 1,
Username = "jdoe",
Password = "password",
DisplayName = "John Doe",
Email = "[email protected]",
PhoneNumber = "555-123-4567"
};
var userVM = Mapper.Map<UserViewModel>(user);
Console.WriteLine("DisplayName: {0}\nEmail: {1}", userVM.DisplayName, userVM.Email);
}
}