Automapper: Flattening - c#

I've tried everything to map from Item class to ItemDto class (basically a flattening map) but I keep getting a null for ItemDto.NestedItemName:
public class Item
{
public NestedItem NestedItem{get;set;}
}
public class NestedItem
{
public string Name{get;set;}
}
public class ItemDto
{
public string NestedItemName{get;set;}
}
I would have thought this would work:
CreateMap<NestedItem, ItemDto>()
.ForMember(dest => dest.NestedItemName, opt => opt.MapFrom(src => src.Name));
but it returns null. Any ideas?
I'm using AutoMapper 7.0.1 in a .Net Core 2.1 app.

You are using the wrong mapping. More than likely it would be the item being converted to the dto so the map should be created using that
CreateMap<Item, ItemDto>()
.ForMember(
dest => dest.NestedItemName,
opt => opt.MapFrom(src => src.NestedItem.Name)
);
From comments
There is be no need for the custom mapping, the default naming conventions covers this

Related

EF Automapper Parents child is null

I'm trying to read data from our database using entity framework and as the project already uses Automapper to convert from entities to Dtos it would make sense use Automappers Queryable Extensions to make life a bit easier. I'm using Microsoft.EntityFrameworkCore version 3.1.9.0
The problem is the returned array of BundleMetaDataDt.child is always null.
The query below returns plenty of data, but every BundleMetaDataDtos child value is null.
I have tried:
.Include(b => b.ChildBundle) before Where statement
.ForMember(dest => dest.ChildBundle, opt => opt.MapFrom(src => src.ChildBundleId))
.MaxDepth(2)
Classes: (There is more fields than shown below)
public partial class Bundle
{
public Guid? ChildBundleId { get; set; }
public Bundle ChildBundle { get; set; }
}
public class BundleMetaDataDto
{
[DataMember(IsRequired = true, Order = 15)]
public BundleMetaDataDto ChildBundle { get; set; }
}
Map:
cfg.CreateMap<Bundle, BundleMetaDataDto>()
.ForMember(dest => dest.ChildBundle, opt => opt.MapFrom(src => src.ChildBundle))
Query:
var bundles = context.Bundles
.Where(bundle => bundle.ChildBundle != null)
.ProjectTo<BundleMetaDataDto>(EntityConverter.MapperConfiguration)
.ToArray();
Thanks to #LucianBargaoanu I got it working by adding:
cfg.Advanced.RecursiveQueriesMaxDepth = 1;

3rd Level Automapper Relationship

I'm integrating with a 3rd party API which is returning a complex data structure and in a part of it I have the following relationship.
public class Parent{
public List<SmartLink> SmartLink { get; set; }
}
The SmartLink object looks like below:
public class SmartLink {
public Address AddressInfo { get; set; }
}
I have tried to map it in several ways, one of them below, but I still get a null on the AddressInfo object.
cfg.CreateMap<Address, AddressInfo>();
cfg.CreateMap<Source, Parent>()
//This is not allowed since Automapper cannot map to 2nd level
.ForMember(d => d.SmartLink.AddressInfo, map => map.MapFrom(src => src.Smartlink.ToList().Select(addr => addr.Address)));
The line below works perfectly:
.ForMember(d => d.SmartLink, map => map.MapFrom(s => s.Smartlink.ToList()))
How can I map/flatten a 3rd level property with Automapper, any pointers?
I had overthought on it. I simply added the following mapping and it worked.
cfg.CreateMap<Address, AddressInfo>();
cfg.CreateMap<SmartlinkPart, SmartLink>(MemberList.Destination)
.ForMember(d => d.AddressInfo, map => map.MapFrom(s => s.Address));
The idea is that for the member AddressInfo, the first line above will provide it's mapping instruction.

Automapper create map from custom method

I have the following classes:
public class Entity
{
public string Name { get; set; }
}
public class SomethingDto
{
public string NameChanged { get; set; }
public void Mapping(Entity something)
{
NameChanged = something.Name;
}
}
I want to use the Mapping Method of the DTO to create the map as the following way:
conf.CreateMap<Entity, SomethingDto>().ForMember(t => t.NameChanged, opt => opt.MapFrom(t => t.Name));
There is a way in AutoMapper to create the maps with custom methods, who works with his projection?
You don't want to do it like that, because that makes the DTO aware of the entity and that would throw out the separation you'd get.
Now in this case, the line ForMember(t => t.NameChanged, opt => opt.MapFrom(t => t.Name)) will work because Name and NameChanged are both of type string. Say you'd like to do something along the lines of mapping identifier of type string with value '20180120-00123456' to two properties on the destination: a DateTime property and a ProductId property. You can do this two ways.
Simple
You would write two mapping functions in the class where you make the mapping and do it along the lines of:
ForMember(t => t.Date, opt => opt.MapFrom(t => RetrieveDate(t.Identifier)))
ForMember(t => t.ProductId, opt => opt.MapFrom(t => RetrieveProductId(t.Identifier)))
Complex
You would make a custom class OrderIdentifier (now I'm assuming the identifier is for an order) with only the Id property as string. Then you'd make two custom type converters, like the article describes.

Automapper. Map if source member is null

I have two classes and map one to other with Automapper. For instance:
public class Source
{
// IdName is a simple class containing two fields: Id (int) and Name (string)
public IdName Type { get; set; }
public int TypeId {get; set; }
// another members
}
public class Destination
{
// IdNameDest is a simple class such as IdName
public IdNameDest Type { get; set; }
// another members
}
Then I use Automapper to map Source to Destination:
cfg.CreateMap<Source, Destination>();
It works properly but sometimes member Type in class Source becomes null. In these cases I would like to map member Type in class Destination from TypeId property. That's what I want in a nutshel:
if Source.Type != null
then map Destination.Type from it
else map it as
Destination.Type = new IdNameDest { Id = Source.Id }
Is it possible with AutoMapper?
You can use the .ForMember() method while declaring the mapping.
Like so :
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type != null ? src.Type : new IdNameDest { Id = src.Id }));
While LeeeonTMs answer works fine AutoMapper provides a specialised mechanism to substitute null values. It "allows you to supply an alternate value for a destination member if the source value is null anywhere along the member chain" (taken from the AutoMapper manual).
Example:
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Value, opt => opt.NullSubstitute(new IdNameDest { Id = src.Id }));
With C# 6.0, the null-coalescing operator can be used.
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type ?? new IdNameDest { Id = src.Id }));
I managed to resolve it with Mapping Resolvers
public class SomeResolver : IValueResolver<Soruce, Dest, Guid>
{
public Guid Resolve(Source source, Dest destination, Guid, destMember, ResolutionContext context)
{
destination.Value= source.Value!=null ? source.Value:0;
return destination.MainGuid = Guid.NewGuid();
}
}
and then on mapping configuraiton
CreateMap<BioTimeEmployeeSummaryDTO, BioTimeEmployeeAttendanceSummary>()
.ForMember(dest => dest.MainGuid, opt => opt.MapFrom<YourResolverClass>())
.ReverseMap();

How to map Integer to String with AutoMapper 3 and Entity Framework

I am trying to use AutoMapper 3 to project a class with an Integer property to another class with a String property.
When the query is executed then I get the following exception:
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Here are the relevant parts of the code:
public partial class Lookup
{
public int LookupId { get; set; }
public int LookupTypeId { get; set; }
public string Value { get; set; }
public int SequencialOrder { get; set; }
public virtual LookupType LookupType { get; set; }
}
public class LookupProfile : Profile
{
protected override void Configure()
{
CreateMap<Lookup, SelectListItem>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.LookupId.ToString()))
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Value));
}
}
And the query looks like:
Provinces = _db.Lookups.Project().To<SelectListItem>().ToList()
Question:
Is there a way I could configure the LookupProfile to do the proper mapping and still work inside Linq To Entities?
Or is there another way I could make the projection work with Linq to Entities?
The solution was to use the SqlFunctions.StringConvert function.
Here is the modified profile code that made everything work:
public class LookupProfile : Profile
{
protected override void Configure()
{
CreateMap<Lookup, SelectListItem>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => SqlFunctions.StringConvert((double)src.LookupId)))
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Value));
}
}
I'll leave this answer here in case anyone else stumbles upon the same issue I had.
One problem with the current accepted answer is that if you're on an ASP.NET MVC project using client-side validation through helpers, you'll get a validation error for the ID field (if it's a number): The field [field] must be a number. That happens because the result from SqlFunctions.StringConvert returns a string with several leading spaces, so the unobtrusive validator doesn't see it as a number.
The way I solved this issue on my own was to create a generic SelectListItem<T> class that inherits from SelectListItem, hides the original Value property and implements its own Value setter:
public class SelectListItem<T> : SelectListItem
{
public new T Value {
set {
base.Value = value.ToString();
}
// Kind of a hack that I had to add
// otherwise the code won't compile
get {
return default(T);
}
}
}
Then on the Automapper profile I would map the items like so:
public class LookupProfile : Profile
{
protected override void Configure()
{
//Use whatever datatype is appropriate: decimal, int, short, etc
CreateMap<Lookup, SelectListItem<int>>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.LookupId))
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Value));
}
}
And finally on the Service layer, I would map the entities to the generic class and return an IEnumerable<SelectListItem>.
public IEnumerable<SelectListItem> GetList() {
return _db.Lookups.Project().To<SelectListItem<int>>().ToList();
}
This way you'll get the right value for the Value property without trailing spaces.

Categories

Resources