Automapper and mapping complex collections - c#

Hi I have a question about automapper, thing is I have a model that has nested collection of other models, and models in that collection also has a collection of models something like (DB model):
public class Cabin
{
public uint Id { get; set; }
public string Name { get; set; }
public Rack[] Racks { get; set; }
}
public class Rack
{
public uint Id { get; set; }
public string RackName { get; set; }
public IPAddress IpAddress { get; set; }
public int Port { get; set; }
public Module[] Modules { get; set; }
}
public class Module
{
public uint Id { get; set; }
public string ModuleName { get; set; }
}
Well from Dto side I have something like:
public class CabinDto
{
public uint Id { get; set; }
public string Name { get; set; }
public RackDto[] Racks { get; set; }
}
public class RackDto
{
public uint Id { get; set; }
public string Name { get; set; }
public ModuleDto[] Modules{ get; set; }
}
public class ModuleDto
{
public string Name { get; set; }
}
So I want to map it all at once, but figure out a way to map a list object with different properties names.
For main class I have:
CreateMap<Db.Cabin, Dto.Cabin>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
// how to map nested list
I could just add some method that assigns values and map to this method, but it does not feel right. I looked in documentation and there are only examples with simple collection with same name lists.
Is there a way to do it?

You just need to add mapping for the type in the nested list and AutoMapper will take care of it.
CreateMap<Db.Module, Dto.Module>();
CreateMap<Db.Rack, Dto.Rack>();
Also when the name of properties in source and origin are the same, you don't need to call the ForMember() method.
So in your case you need it for the ModuleName to Name of the Module to ModuleDto mapping, and same for the Rack class, see this fiddle.

Related

How to flattening child object used repeatedly in C# with Automapper

Using the following entities
public class User
{
public Guid Id { get; set; }
public string Username { get; set; }
}
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedByUser { get; set; }
public User DeletedByUser { get; set; }
}
How do I flatten this to the GeneralEntityDto below?
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
I have tried setting up my mappings as seen below but it fails with a complaint about "CreatedByUsername" and "DeletedByUsername" not being mapped.
protected void Configure()
{
CreateMap<GeneralEntity, GeneralEntityDto>()
.ForMember(dest => dest.CreatedByUsername,
opt => opt.MapFrom(src => src.CreatedByUser.Username))
.ForMember(dest => dest.DeletedByUsername, opt =>
opt.MapFrom(src => src.DeletedByUser.Username));
}
You can use the naming convention that automapper provides.
Basically if you include the exact string of the property name of the source Object you do not have to add ForMember() automapper is clever enough to do it automatically.
That means for example :
public class GeneralEntity
{
public Guid Id { get; set; }
public User CreatedBy { get; set; } // renaming just for simplicity
public User DeletedBy { get; set; } // renaming just for simplicity
}
public class GeneralEntityDto
{
public Guid Id { get; set; }
public string CreatedByUsername { get; set; }
public string DeletedByUsername { get; set; }
}
Reference also to these:
http://docs.automapper.org/en/stable/Flattening.html
AutoMapper TwoWay Mapping with same Property Name

Ignoring a property of collection member AutoMapper C#

I have next enteties
public class TagDTO
{
public int Id { get; set; }
public string Description { get; set; }
public List<ImageDTO> Images { get; set; }
}
public class ImageDTO
{
public int Id { get; set; }
public List<TagDTO> Tags { get; set; }
}
And when I'm mapping from Image to ImageDTO i do not need List<Images> in TagDTO class. But in other cases this property is necessary. Can I configure in AutoMapper to Ignore List<ImagesDTO> Images, like this
CreateMap<Image, ImageDTO>().ForMember(x => x.Tags.Select(p=>p.Images),
opts => opts.Ignore());

Automapper - Cannot ignore a property when mapping to a list existing entities

I have two class TenantRestrictSourceEntity, TenantRestrictSource and mapper profile
CreateMap<TenantRestrictSourceEntity, TenantRestrictSource>()
.ForMember(dest => dest.Tenant, opt => opt.Ignore());
public class TenantRestrictSourceEntity
{
public string SourceType { get; set; }
public bool? Enable { get; set; }
public int TenantId { get; set; }
}
public sealed class TenantRestrictSource
{
public int Id { get; set; }
public string SourceType { get; set; }
public bool Enable { get; set; }
public int TenantId { get; set; }
public Tenant Tenant { get; set; }
}
When I map one TenantRestrictSourceEntity to one TenantRestrictSource with Mapper.Map(tenantRestrictSourceEntity, tenantRestrictSource). Everything works fine. Tenant property was ignored correctly.
But when I try to map a list TenantRestrictSourceEntity to a list TenantRestrictSource with Mapper.Map(tenantRestrictSourceEntities, tenantRestrictSources) Tenant property is always null.
How can I ignore that property when mapping a list object?

How to do nested mapping with AutoMapper?

I have an entity and model defined as this:
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Location Location { get; set; }
public string Gender { get; set; }
public string Skills { get; set; }
public bool isPrivate { get; set; }
}
public class Location
{
public int Id { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Location Location { get; set; }
public bool isPrivate { get; set; }
}
Then, I have setup a mapping profile defined as this:
public MappingProfile()
{
CreateMap<User, UserModel>()
.ReverseMap();
}
And the point is that this works to some extend, but the Location complex type is not mapped properly. I can always flatten it, by including LocationCity and LocationCountry inside the UserModel class, but that's not what I wanna do. I want Location to appear as a nested property in the returned result, as it is defined originally. How can I achieve this in AutoMapper?
You need to add a mapping between Location to Location too. Yep, the source and destination are identitic.
So your mapping profile should look like this:
public MappingProfile()
{
CreateMap<Location, Location>();
CreateMap<User, UserModel>()
.ReverseMap();
}
You could define your configuration like this:
CreateMap<User, UserModel>()
.ForMember(um => um.Location , opt => opt.MapFrom(u => u.Location))
.ReverseMap();
The ReverseMap can only create a simple mapping. For any complex type, you need to configure it manually, because AutoMaper don't know exactly how to instantiate the Location property and it will return null reference.

Mapping Source Collection's Id to Destination Objects Collection Id

Suppose in the source object I have classes:
// Source classes
class Source
{
public Source
{
things = new List<Thing>();
}
public Guid SourceId { get; set; }
public string Name { get; set; }
public virtual List<Thing> Things { get; set; }
}
class Thing
{
public Guid ThingId { get; set; }
public double Price { get; set; }
}
//Destination class
class Dest
{
public Guid DestId { get; set; }
public string Name { get; set; }
public virtual List<Guid> ThingsIds { get; set; }
}
How do I map Things -> ThingId (src) to ThingsIds (dest) using Automapper?
I would use the LINQ extension method .Select:
Mapper.CreateMap<Source, Dest>()
.ForMember(
dest => dest.ThingsIds,
opt => opt.MapFrom(src => src.Things.Select(th => th.ThingId)));

Categories