I've been trying to use AutoMapper to save some time going from my DTOs to my domain objects, but I'm having trouble configuring the map so that it works, and I'm beginning to wonder if AutoMapper might be the wrong tool for the job.
Consider this example of domain objects (one entity and one value):
public class Person
{
public string Name { get; set; }
public StreetAddress Address { get; set; }
}
public class StreetAddress
{
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
My DTO (from a Linq-to-SQL object) is coming out looking roughly like this:
public class PersonDTO
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
I'd like to be able to do this in my repository:
return Mapper.Map<PersonDTO, Person>(result);
I've tried configuring AutoMapper every way I can figure, but I keep getting the generic Missing type map configuration or unsupported mapping error, with no details to tell me where I'm failing.
I've tried a number of different configurations, but here are a few:
Mapper.CreateMap<PersonDTO, Person>()
.ForMember(dest => dest.Address, opt => opt.MapFrom(Mapper.Map<Person, Domain.StreetAddress>));
and
Mapper.CreateMap<Person, Domain.Person>()
.ForMember(dest => dest.Address.Address1, opt => opt.MapFrom(src => src.Address))
.ForMember(dest => dest.Address.City, opt => opt.MapFrom(src => src.City))
.ForMember(dest => dest.Address.State, opt => opt.MapFrom(src => src.State));
I've read that flattening objects with AutoMapper is easy, but unflattening them isn't easy...or even possible. Can anyone tell me whether I'm trying to do the impossible, and if not what I'm doing wrong?
Note that my actual objects are a little more complicated, so it's possible I'm leaving out info that is the key to the error...if what I'm doing looks right I can provide more info or start simplifying my objects for testing.
This also seems to work for me:
Mapper.CreateMap<PersonDto, Address>();
Mapper.CreateMap<PersonDto, Person>()
.ForMember(dest => dest.Address, opt => opt.MapFrom( src => src )));
Basically, create a mapping from the dto to both objects, and then use it as the source for the child object.
Can't post a comment, so posting an answer. I guess there were some changes in AutoMapper implementation so answer https://stackoverflow.com/a/5154321/2164198 proposed by HansoS is no longer compilable. Though there is another method that can be used in such scenarios - ResolveUsing:
Mapper.CreateMap<Person, Domain.Person>()
.ForMember(dest => dest.Address, opt => opt.ResolveUsing( src => { return new Address() {Address1 = src.Address, City = src.City, State = src.State }; }))
In addition to sydneyos answer and according to Trevor de Koekkoek comment, two way mapping is possible this way
public class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
}
public class PersonViewModel
{
public string Name { get; set; }
public string AddressStreet { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
}
Automapper mappings
Mapper.Initialize(cfg => cfg.RecognizePrefixes("Address"));
Mapper.CreateMap<Person, PersonViewModel>();
Mapper.CreateMap<PersonViewModel, Address>();
Mapper.CreateMap<PersonViewModel, Person>()
.ForMember(dest => dest.Address, opt => opt.MapFrom( src => src )));
If you implement NameOf class, you can get rid of prefix magic string
Mapper.Initialize(cfg => cfg.RecognizePrefixes(Nameof<Person>.Property(x => x.Address)));
EDIT: In C# 6
Mapper.Initialize(cfg => cfg.RecognizePrefixes(nameof(Person.Address)));
use https://github.com/omuleanu/ValueInjecter, it does flattening and unflattening, and anything else you need, there is an asp.net mvc sample application in the download where all the features are demonstrated (also unit tests)
This might be late but you can solve this by using lambda expressions to create the object like this:
Mapper.CreateMap<Person, Domain.Person>()
.ForMember(dest => dest.Address, opt => opt.MapFrom( src => { return new Address() {Address1 = src.Address, City = src.City, State = src.State }; }))
I have another solution. The main idea is that AutoMapper know how to flatten nested objects when you name properly properties in flattened object: adding nested object property name as a prefix. For your case Address is prefix:
public class PersonDTO
{
public string Name { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
...
}
So creating familiar mapping from nested to flattened and then using ReverseMap method allows AutomMapper to understand how to unflatten nested object.
CreateMap<Person, PersonDTO>()
.ReverseMap();
That's all!
I'm using this
public static void Unflatten<TSource, TDestination, TMember>(this IMemberConfigurationExpression<TSource, TDestination, TMember> opt)
{
var prefix = opt.DestinationMember.Name;
var memberProps = typeof(TMember).GetProperties();
var props = typeof(TSource).GetProperties().Where(p => p.Name.StartsWith(prefix))
.Select(sourceProp => new
{
SourceProp = sourceProp,
MemberProp = memberProps.FirstOrDefault(memberProp => prefix + memberProp.Name == sourceProp.Name)
})
.Where(x => x.MemberProp != null);
var parameter = Expression.Parameter(typeof(TSource));
var bindings = props.Select(prop => Expression.Bind(prop.MemberProp, Expression.Property(parameter, prop.SourceProp)));
var resolver = Expression.Lambda<Func<TSource, TMember>>(
Expression.MemberInit(Expression.New(typeof(TMember)), bindings),
parameter);
opt.ResolveUsing(resolver.Compile());
}
Configuration
new MapperConfiguration(cfg =>
{
cfg.CreateMap<Person, PersonDTO>();
cfg.CreateMap<PersonDTO, Person>().ForMember(x => x.HomeAddress, opt => opt.Unflatten());
});
Models
public class Person
{
public string Name { get; set; }
public Address HomeAddress { get; set; }
}
public class Address
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
Following AutoMapper flattening conventions
public class PersonDTO
{
public string Name { get; set; }
public string HomeAddressLine1 { get; set; }
public string HomeAddressLine2 { get; set; }
public string HomeAddressCity { get; set; }
public string HomeAddressState { get; set; }
public string HomeAddressZipCode { get; set; }
}
Probably needs many improvements but it works...
Related
I want to map ProjectDto object to Project object.
So, ProjectDto class contains list of styles:
public class ProjectDto
{
public List<StyleDto>? Styles { get; set; }
// and other properties...
}
And it's a Project class:
public class Project
{
public virtual IEnumerable<StyleOfProject> StylesOfProject { get; set; }
// and other properties...
}
There is many-to-many relationship between Style and Project, which is represented in StyleOfProject class:
public class StyleOfProject
{
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
public int StyleId { get; set; }
public virtual Style Style { get; set; }
}
public class Style
{
public virtual IEnumerable<StyleOfProject> StyleOfProjects { get; set; }
// and other properties...
}
So, I tried to map like this:
CreateMap<ProjectDto, Project>().ForMember(dest => dest.StylesOfProject, opt => opt.MapFrom(src => src.Styles))
And I got empty StylesOfProject. I understand this is incorrect mapping way, but I don't have any right ideas how to map it.
So, I found a solution to my problem:
CreateMap<ProjectDto, Project>()
.ForMember(dest => dest.StylesOfProject,
opt => opt.MapFrom(src => src.Styles))
.AfterMap((_, dest) =>
{
foreach (var s in dest.StylesOfProject)
{
s.ProjectId = dest.Id;
s.Project = dest;
}
});
CreateMap<StyleDto, StyleOfProject>()
.ForMember(dest => dest.Style,
opt => opt.MapFrom(src => src))
.AfterMap((_, dest) =>
{
dest.StyleId = dest.Style.Id;
});
CreateMap<StyleDto, Style>();
AfterMap() is very useful thing in AutoMapper. You can find more information on this website.
I want to flatten my entity framework model data to dto for my ASP.NET Core REST Web Service.
My entity classes (simplified) look like:
public class DeliveryNoteEntity
{
public string VehicleNo { get; set; }
public int NotMapped { get; set; }
public List<DeliveryNoteSignature> Signatures { get; set; }
}
public class DeliveryNoteSignature
{
public string Signature { get; set; }
public SignedByRole SignedBy { get; set; }
}
public enum SignedByRole
{
Driver = 1,
Recipient = 2
}
My dto looks like
public class DeliveryNoteDto
{
public int Id { get; set; }
public string VehicleNo { get; set; }
public string DriverSignature { get; set; }
public string RecipientSignature { get; set; }
}
Then I can fill my entity like this
var sourceEntity = new DeliveryNoteEntity
{
VehicleNo = "VehicleNo20",
Signatures = new List<DeliveryNoteSignature> { new DeliveryNoteSignature { Signature = "Driver Mr. Pitz", SignedBy = SignedByRole.Driver} }
};
and map to a dto:
Mapper.Initialize(cfg => cfg.CreateMap<DeliveryNoteEntity, DeliveryNoteDto>()
.ForMember(dest => dest.DriverSignature, opt => opt.MapFrom(src => src.Signatures
.Where(x => x.SignedBy == SignedByRole.Driver)
.Select(x => x.Signature)
.FirstOrDefault()))
.ForMember(dest => dest.RecipientSignature, opt => opt.MapFrom(src => src.Signatures
.Where(x => x.SignedBy == SignedByRole.Recipient)
.Select(x => x.Signature)
.FirstOrDefault()))
.ReverseMap()
);
var dto = Mapper.Map<DeliveryNoteDto>(sourceEntity);
So my question is: how can I do a reverse mapping from dto to entity when it comes back to my service? So by hand I would do something like this:
var entityToSave = new DeliveryNoteEntity()
{
VehicleNo = dto.VehicleNo,
Signatures = new List<DeliveryNoteSignature>
{
new DeliveryNoteSignature {SignedBy = SignedByRole.Driver, Signature = dto.DriverSignature},
new DeliveryNoteSignature {SignedBy = SignedByRole.Recipient, Signature = dto.RecipientSignature}
}
};
It there any way to do it with AutoMapper?
EDIT: my real question is how can I do a mapping from XXXSignature properties in my dto to a list in my entity?
If you use AutoMapper's naming convention, ReverseMap is also done for you.
class CustomerClass
{
public string Name {get; set;}
public string Surname {get;set;}
}
class MainClass
{
public CustomerClass Customer {get; set;}
}
class MainClassDto
{
public string CustomerName {get; set;}
public string CustomerSurname {get; set;}
}
For this example you do not need to make any configuration for mapping and/or reverse mapping. AutoMapper handles it with its default configuration
I ended up doing that by code, so I can clearly detect and define add, delete and update operations. So I use AutoMapper only one way: entity -> dto.
I have a View Model such as
public class RootViewModel
{
public CreateCompanyViewModel Company { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public CreateUserTypeViewModel UserType { get; set; }
}
And CreateCompanyViewModel and CreateUserTypeViewModel are like
public class CreateCompanyViewModel
{
public string CompanyName { get; set; }
}
public class CreateUserTypeViewModel
{
public string UserTypeName { get; set; }
}
I want this RootVM to be flattened to multiple DTO's. The 3 DTO's for the above RootVM I have are like
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
public class CompanyDTO
{
public string CompanyName { get; set; }
}
public class UserTypeDTO
{
public string UserTypeName { get; set; }
}
NOTE : Note that CompanyDTO and UserTypeDTO are not nested object (part of) UserDTO unlike RootVM.
When I'm doing the mapping using AutoMapper RootVM properties gets mapped to UserDTO but CompanyDTO and UserTypeDTO are null as expected.
I tried mapping them by using ForMember function with MapFrom and ResolveUsing methods, but both of them shows error as
Custom configuration for members is only supported for top-level
individual members on a type.
UPDATE
Below is my mapping code
CreateMap<RootViewModel, CompanyDTO>();
CreateMap<RootViewModel, UserDTO>();
CreateMap<RootViewModel, UserTypeDTO>();
CreateMap<CreateCompanyViewModel, CompanyDTO>();
CreateMap<CreateUserTypeViewModel, UserTypeDTO>();
I'm using AutoMapper 5.2.0
UPDATE - Fix :
Well what I found is, either I have to use .ForMember for all the properties manually, else for automatic convention to work, I need to use https://github.com/AutoMapper/AutoMapper/wiki/Flattening or https://arnabroychowdhurypersonal.wordpress.com/2014/03/08/flattening-object-with-automapper/.
This is the only way to make it work.
Wish I could do .ForMember(d => d, s => s.MapFrom(x => x.Company)) and it'd map all the properties from CreateCompanyViewModel => CompanyDTO. This would have been very handy, but AutoMapper doesn't supports this.
Try following
CreateMap<CreateCompanyViewModel, CompanyDTO>();
CreateMap<CreateUserTypeViewModel, UserTypeDTO>();
CreateMap<RootViewModel, CompanyDTO>()
.ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.CompanyName));
CreateMap < RootViewModel, UserTypeDTO()
.ForMember(dest => dest.UserTypeName, opt => opt.MapFrom(src => src.UserType.UserTypeName));
CreateMap<RootViewModel, UserDTO>();
After getting automapper to work (previous question), I'm struggling with another problem (took it to another question, so the first one wouldn't be too complicated)...
I have next classes:
public class Model1
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDay { get; set; }
public int Gender { get; set; }
public string NickName { get; set; }
}
public class Model2
{
public bool Married { get; set; }
public int Children { get; set; }
public bool HasPet { get; set; }
}
public class Entity1
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDay { get; set; }
public int Gender { get; set; }
}
public class Entity2
{
public bool Married { get; set; }
public int Children { get; set; }
public bool HasPet { get; set; }
public string NickName { get; set; }
}
These objects are schematically similar to my original objects, except to the name and the complexity.
And AutoMapper configuration class (called from Global.asax):
public class AutoMapperConfig
{
public static MapperConfiguration MapperConfiguration { get; set; }
public static void Configure()
{
MapperConfiguration = new MapperConfiguration(cfg => {
cfg.AddProfile<Out>();
cfg.CreateMap<SuperModel, SuperEntity>();
});
MapperConfiguration.AssertConfigurationIsValid();
}
}
public class Out: Profile
{
protected override void Configure()
{
CreateMap<Model1, Entity1>();
CreateMap<Model2, Entity2>()
.ForMember(dest => dest.NickName, opt => opt.Ignore());
CreateMap<Model1, Entity2>()
.ForMember(dest => dest.Married, opt => opt.Ignore())
.ForMember(dest => dest.Children, opt => opt.Ignore())
.ForMember(dest => dest.HasPet, opt => opt.Ignore());
CreateMap<SuperModel, SuperEntity>()
.ForMember(dest => dest.Entity1, opt => opt.MapFrom(src => src.Model1))
.ForMember(dest => dest.Entity2, opt => opt.MapFrom(src => src.Model2));
}
}
When I need the object to be converted, I do next (at this point I have _superModel initialized and filled with data):
SuperEntity _superEntity = new SuperEntity();
AutoMapperConfig.MapperConfiguration.CreateMapper().Map<SuperModel, SuperEntity>(_superModel, _superEntity);
So, I map Model1 to Entity1 (witch is fine), and also Model2 to Entity2 (witch is also fine, except the Id property, which is ignored).
Main objects SuperModel and SuperEntity are mapped as well, and seems to work fine.
The problem happens, when I map Model1 to Entity2, to get the NickName (thought the rest of the properties are ignored). Some how It is always null!
Any ideas?
The problem is you want to map a destination property (Entity2) from multiples source property values (Children, Married and HasPet from Model2 and Nickname from Model1).
You can solve your situation using an Custom Resolver or with AfterMap method.
Using Custom Resolvers:
You have to create a class inhering from ValueResolver to define how you will map Entity2 from an SuperModel:
public class CustomResolver : ValueResolver<SuperModel, Entity2>
{
protected override Entity2 ResolveCore(SuperModel source)
{
return new Entity2
{
Children = source.Model2.Children,
HasPet = source.Model2.HasPet,
Married = source.Model2.Married,
NickName = source.Model1.NickName
};
}
}
Then, use it like this:
CreateMap<SuperModel, SuperEntity>()
.ForMember(dest => dest.Entity1, opt => opt.MapFrom(src => src.Model1))
.ForMember(dest => dest.Entity2, opt => opt.ResolveUsing<CustomResolver>());
Using AfterMap:
You can execute actions after the mapping, so pass the value from Model1.Nickname to Entity2.Nickname after the map:
CreateMap<SuperModel, SuperEntity>()
.ForMember(dest => dest.Entity1, opt => opt.MapFrom(src => src.Model1))
.ForMember(dest => dest.Entity2, opt => opt.MapFrom(src => src.Model2))
.AfterMap((m, e) => e.Entity2.NickName = m.Model1.NickName);
when I'm using this mapping
Mapper.CreateMap<DataSourceConfigurationContract, DataSourceConfigurationContract>().ForMember(x => (object)x.DatabaseTypeException, opt => opt.Ignore())
.ForMember(x => (object)x.DatabaseType, opt => opt.Ignore());
var mappedValue = Mapper.Map<DataSourceConfigurationContract, DataSourceConfigurationContract>(dataSourceConfiguration);
for this class
public sealed class DataSourceConfigurationContract {
public string Name { get; set; }
public string ConnectionString { get; set; }
public string ConnectionType { get; set; }
public DataSourcePropertyContractCollection Properties { get; set; }
public DataSourceAreaConfigurationContractCollection Areas { get; set; }
public UserContractCollection AllowedUsers{ get; set; }
public DataSourceType? DatabaseType { get; set; }
public ExceptionContract DatabaseTypeException { get; set; }
public DataSourceType DataSourceType { get; set; } }
some Properties are ignored (e.g. Areas) that should be mapped. The string properties seem to be always correctly mapped. What have I done wrong?
AutoMapper only support the following collections out of the box: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home . I guess that your properties that are not copied are of type XXXCollection.
You can solve this by creating a custom type converter for your collection types: http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home
For your collections you need to do something similar to the following (taken from some code I've recently worked on):
Mapper.CreateMap<List<QuizItemTypeModel>, List<Quiz.DataContracts.QuizItemType>>()
.Include<QuizDataCompositeModel, Quiz.DataContracts.QuizDataComposite>();
Where QuizDataCompositeModel and Quiz.DataContracts.QuizDataComposite both extend List<"RespectiveType">
It's quite simple:
Mapper.CreateMap<DataSourceAreaConfigurationContract, DataSourceAreaConfiguration>();
Mapper.CreateMap<DataSourceConfigurationContract, DataSourceConfigurationContract>()
.ForMember(dest => dest.Areas, opt => opt.UseDestinationValue());
Tipp: Download the source code and learn from the given unittests and samples!
You can get it there: http://automapper.codeplex.com/SourceControl/list/changesets