-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
When properties you want to map have the same names in target and source objects mark mapping interface with MapperAttribute
and that's it:
[Mapper]
public interface INotesMapper
{
NoteDto MapToDto(NoteDao source);
}
It will generate mapping class with default class name NotesMapper
for properties that names are the same for NoteDto
and NoteDao
classes.
When the names are different than use Source
and Target
names of the properties in the MappingAttribute
:
[Mapper(ClassName = "UserMapper")]
public interface IUserMapper
{
[Mapping(Source = nameof(UserDao.FirstName), Target = nameof(UserInfo.Name))]
UserInfo MapToDomainMoodel(UserDao userDao);
}
It will genetare us mapper class UserMapper
according to ClassName = "UserMapper"
code.
In a case when mapped object contains another objects, e.g.:
public class NoteDto
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public NoteDocumentDto Document { get; set; }
}
and
public class NoteDao
{
public long Id { get; set; }
public string PageTitle { get; set; }
public string Description { get; set; }
public NoteDocumentDao Document { get; set; }
}
add mapping method to mapper interface for these types and the code generation processor will match and generate mappings for
NoteDto MapToDto(NoteDao source)
method:
[Mapper]
public interface INotesMapper
{
[Mapping(Source = nameof(NoteDao.PageTitle), Target = nameof(NoteDto.Title))]
NoteDto MapToDto(NoteDao source);
[Mapping(Source = nameof(NoteDocumentDao.Metadata.CreatorFirstName), Target = nameof(NoteDocumentDto.Autor))]
NoteDocumentDto MapToDto(NoteDocumentDao source);
}
We have a couple of possibilities to map objects that contain collections.
For example mapping UserDao
:
public class UserDao
{
public long UserId { get; set; }
public AddressDao[] UserAddresses { get; set; }
}
to UserInfo
public class UserInfo
{
public int Id { get; set; }
public Address[] Addresses { get; set; }
}
can be achieved using abstract class mapper:
[Mapper(ClassName = "UserDataMapper")]
public abstract class UserMapper
{
[Mapping(Source = nameof(UserDao.UserAddresses), Target = nameof(UserInfo.Addresses), Expression = nameof(ConvertAddresses))]
[Mapping(Source = nameof(UserDao.UserId), Target = nameof(UserInfo.Id), Expression = nameof(ConvertUserId))]
public abstract UserInfoWithArray MapToDomainModel(UserWithArrayDao userWithArrayDao);
protected Address[] ConvertAddresses(AddressDao[] addresses)
{
return addresses.Select(a => MapAddress(a)).ToArray();
}
protected static int ConvertUserId(long id)
{
return Convert.ToInt32(id);
}
public abstract Address MapAddress(AddressDao addressDao);
}
or using interface mapper and overriding mappings in inherited class:
[Mapper(ClassName = "UserMapperBase")]
public interface IUserMapper
{
[Mapping(Source = nameof(UserDao.FirstName), Target = nameof(UserInfo.Name))]
UserInfo MapToDomainModel(UserDao userDao);
Address MapAddress(AddressDao addressDao);
Region MapRegion(RegionDao regionDao);
}
public class UserMapper: UserMapperBase
{
public override UserInfo MapToDomainModel(UserDao userDao)
{
var result = base.MapToDomainModel(userDao);
result.Addresses = userDao.UserAddresses.Select(addressDao => base.MapAddress(addressDao)).ToArray();
return result;
}
}
@alekshura SourceMapper 2021