/LinkIt.AutoMapperExtensions

LinkIt extensions for AutoMapper can be used to map linked sources to DTOs by conventions.

Primary LanguageC#MIT LicenseMIT

LinkIt AutoMapper Extensions

LinkIt extensions for AutoMapper can be used to map linked sources to DTOs by conventions. For more information about LinkIt, see the project page.

Getting started

For example, let's say you wish to map to those DTOs:

public class MediaDto: IMultimediaContentDto{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<Tag> Tags { get; set; }
}

public class TagDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Remember our models

public class Media
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IEnumerable<int> TagIds { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
}

and our linked source

public class MediaLinkedSource
{
    public Media Model { get; set; }
    public List<Tag> Tags { get; set; }
}

Map DTOs using our MapLinkedSource() extension method:

Mapper.CreateMap<MediaLinkedSource, MediaDto>().MapLinkedSource();

What this does is, for all the properties of the DTO, map them to matching properties from the linked source, or if none exists, map them to matching properties from the model. It is the equivalent of this.

Mapper.CreateMap<MediaLinkedSource, MediaDto>()
    .ForMember(dto => dto.Id, opt => opt.MapFrom(source => source.Model.Id))
    .ForMember(dto => dto.Title, opt => opt.MapFrom(source => source.Model.Title))

Of course, in this example, you still need to map the other DTOs, such as TagDto. In this case, that object is quite simple, so AutoMapper's default convention works great.

Mapper.CreateMap<Tag, TagDto>();

We are done, we can leverage AutoMapper to perform complex projections on our linked sources!

Read more

See also