Advanced Mapping with IHaveCustomMappings
Implementing Custom LINQ Expressions, Reverse Mapping, and Complex Data Transformations in AUA
The Power of IHaveCustomMappings
Unlike standard name-matching approaches, IHaveCustomMappings provides unmatched flexibility by allowing source model fields to be explicitly mapped using custom LINQ commands. Configurations are neatly defined at the model level, empowering developers to transform complex structures—such as mapping user entities alongside roles and access levels or handling advanced reporting queries.
LINQ-Powered Configurations
Any mapping operation or data projection that can be expressed via LINQ commands for an entity can be cleanly implemented using this interface, making it ideal for reporting and relational projections.
AppUser to AppUserDto while incorporating granular access levels and roles.
Reverse Mapping with CreateMap
When you need to reverse the mapping flow—transforming data from a DTO back to an Entity—you can easily utilize CreateMap configurations interchangeably.
2. Custom Mapping with LINQ Expressions (IHaveCustomMappings)
For complex scenarios involving LINQ, nested collections, or custom property transformations, implement IHaveCustomMappings and configure AutoMapper rules in ConfigureMapping.
public class AppUserDto : BaseEntityDto<long>, IHaveCustomMappings
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<UserRole> UserRoles { get; set; }
public void ConfigureMapping(Profile configuration)
{
configuration.CreateMap<AppUser, AppUserDto>()
.ForMember(p => p.UserRoles, opt => opt.MapFrom(q => q.UserRoles))
.ReverseMap();
}
}
"If you think good architecture is expensive, try bad architecture."AUA Framework Philosophy