O'Neil Tomlinson
O'Neil Tomlinson

Reputation: 888

AutoMapper with Array and JsonApiSerializer.JsonApi.Relationship

I have an AppService solution with the following Classes and i want to map from the SourceObject to the DestinationObject

Source Classes

 public class SourceObject
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public JsonApiSerializer.JsonApi.Relationship<SourceChildObject[]> childObjects { get; set; }
    }

 public class SourceChildObject
    {
        public string Id { get; set; }
        public string type { get; set; }
    }

Destination Classes

public class DestinationObject
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public JsonApiSerializer.JsonApi.Relationship<DestinationChildObject[]> childObjects { get; set; }
    }

 public class DestinationChildObject
    {
        public string Id { get; set; }
        public string type { get; set; }
    }

Auto mapper is setup in the sartup class

services.AddAutoMapper(typeof(EntityMappingProfile));

And my mapping class loos like this

 public class EntityMappingProfile : Profile
    {
        public EntityMappingProfile()
        {
            CreateMap<SourceObject, DestinationObject>();
            CreateMap<Relationship<SourceChildObject[]>, Relationship<DestinationChildObject[]>>();
         }
}

When i execute the solution all fields are mapped apart form the array field of type JsonApiSerializer.JsonApi.Relationship. The destination field is null. What am i doing wrong?

Upvotes: 0

Views: 98

Answers (1)

Prolog
Prolog

Reputation: 3374

You forgot about creating a map between SourceChildObject and DestinationChildObject. Add this line to your EntityMappingProfile.

CreateMap<SourceChildObject, DestinationChildObject>();

And one more thing, when you are mapping generic types, you can enable mapping for all types with:

CreateMap(typeof(Relationship<>), typeof(Relationship<>));

instead of creating a map with concrete use case of a generic type.

Upvotes: 2

Related Questions