Is there a way to convert from plain DTO like:
class DTO {
private Set<String> prop;
}
to entity like:
class Entity {
private Nested nested;
}
class Nested {
private Set<String> prop;
}
When I try default configuration nested field remains empty.
What should be configured?
You can use the functionality of the deep mapping
ModelMapper mapper = new ModelMapper();
mapper.createTypeMap(DTO.class, Entity.class)
.addMappings(mapping -> mapping.<Set<String>>map(DTO::getProp, (dest, v) -> dest.getNested().setProp(v)));
mapper.createTypeMap(Entity.class, DTO.class)
.addMappings(mapping -> mapping.map(src -> src.getNested().getProp(), DTO::setProp));
Related
I am building a automated swagger plugin. Here I run through annotated classes.
When we talk about the datatypes of String, Long, etc. is is enough for me use the simpleName method.
But when get to a Class of List, Set, Collection I need to know the generic type.
So how can I do this?
A example of code which do most of the job:
class Foo {
List<String> myString
}
class SomeUtilClass {
static String dataType(Class<?> c) {
return c.simpleName
}
static List<String> dataTypes(Class<?> c) {
return c.metaClass.properties.findAll {MetaProperty metaProperty ->
metaProperty?.field != null
}.collect {dataType(it.type)}
}
}
SomeUtilClass.dataTypes(Foo) // ["List"] but I want something like ["List<String>"]
I found the solution. I can look on the generic type from Cached fields.
See below example:
class SomeUtilClass {
static String dataType(Class<?> c) {
return c.simpleName
}
static List<String> dataTypes(Class<?> c) {
return c.metaClass.properties.findAll {MetaProperty metaProperty ->
metaProperty?.field != null
}.collect {findGenerics(it.type)}
}
static void findGenerics(CachedField t) {
t.field.genericType?.actualTypeArguments.collect {dataType(it)}
}
}
I have 2 classes, Class1 should be mapped to Class2. I do mapping with AutoMapper. I'd like to test my configuration of the mapper and for this purposes I'm using AutoFixture. Source class Class1 has property of type IList<>, destination class Class2 has a similar property but of type IEnumerable<>. To simplify test preparation I'm using AutoFixture (with AutoMoqCustomization) to initialize both source and destination objects. But after initializing property of type IEnumerable<> with AutoFixture, AutoMapper can't map the property.
Error text:
Error mapping types.
Mapping types: Class1 -> Class2 ConsoleApplication1.Class1 ->
ConsoleApplication1.Class2
Type Map configuration: Class1 -> Class2 ConsoleApplication1.Class1 ->
ConsoleApplication1.Class2
Property: Items
Could anybody help me to configure either AutoMapper or AutoFixture to make the mapping work? As a workaround I can assign null to the destination property, but I do not want to do this in the each test.
Simplified example of code:
public class AutoMapperTests
{
public static void TestCollectionsProperty()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ItemClass1, ItemClass2>();
cfg.CreateMap<Class1, Class2>();
});
var src = new Class1();
src.Items = new List<ItemClass1>()
{
new ItemClass1() { Text = "111" },
new ItemClass1() { Text = "222" }
};
var fixture = new Fixture();
var dst = fixture.Create<Class2>();
Mapper.Map(src, dst); //Error at this line of code
}
}
public class Class1
{
public IList<ItemClass1> Items { get; set; }
}
public class Class2
{
public IEnumerable<ItemClass2> Items { get; set; }
}
public class ItemClass1
{
public string Text { get; set; }
}
public class ItemClass2
{
public string Text { get; set; }
}
It's not really an AutoFixture issue per se. You can reproduce it without AutoFixture by instead creating dst like this:
var dst = new Class2();
dst.Items = Enumerable.Range(0, 1).Select(_ => new ItemClass2());
This will produce a similar error message:
Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Int32,Ploeh.StackOverflow.Q45437098.ItemClass2]' to type 'System.Collections.Generic.IList1[Ploeh.StackOverflow.Q45437098.ItemClass2]'
That ought to be fairly self-explanatory: WhereSelectEnumerableIterator<int, ItemClass2> doesn't implement IList<ItemClass2>. AutoMapper attempts to make that cast, and fails.
The simplest fix is probably to avoid populating dst:
var dst = new Class2();
If you must use AutoFixture for this, you can do it like this:
var dst = fixture.Build<Class2>().OmitAutoProperties().Create();
Unless the Class2 constructor does something complex, however, I don't see the point of using AutoFixture in that scenario.
If, on the other hand, you do need dst to be populated, you just need to ensure that dst.Items is convertible to IList<ItemClass2>. One way to do that would be like this:
var dst = fixture.Create<Class2>();
dst.Items = dst.Items.ToList();
You could create a Customization to make sure that this happens automatically, but if you need help with that, please ask a new question (if you don't find one that already answers that question).
Here is a working example for your problem. As #Mark Seemann already told, Mapper.CreateMap has been deprecated, so this example is using the new structure.
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ItemClass1, ItemClass2>();
cfg.CreateMap<Class1, Class2>();
});
var src = new Class1();
src.Items = new List<ItemClass1>()
{
new ItemClass1() { Text = "111" },
new ItemClass1() { Text = "222" }
};
var dest = Mapper.Map<Class1, Class2>(src);
AM requires IList because you're mapping to an existing list and that works by calling IList.Add.
I have a situation where I have another DTO in inside a DTO to which I have to map to its corresponding entity.
I am using mapstruct and I already have AnotherEntityMapper already existing.
DTO
public class EntityDTO {
private AnotherEntityDTO anotherEntityDTO;
// other fields
}
Entity
#Entity
public class Entity {
private AnotherEntity anotherEntity;
// other fields
}
How to change the EntityMapper interface, so that I can map anotherEntityDTO to anotherEntity?
Thanks.
It really depends which version of MapStruct you are using. If you are using 1.2.0.Beta or higher they you can just define the nested properties on your EntityMapper interface:
#Mapper
public interface EntityMapper {
#Mapping(target = "anotherEntity", source = "anotherEntityDTO")
#Mapping(target = "anotherEntity.propE", source = "anotherEntityDTO.propD")
Entity map(EntityDDTO dto);
}
Another option (and a must if you are using version less than 1.2.0.Beta) is to add a new Method in your EntityMapper like:
#Mapper
public interface EntityMapper {
#Mapping(target = "anotherEntity", source = "anotherEntityDTO")
Entity map(EntityDDTO dto);
#Mapping(target = "propE", source = "propD")
AnotherEntity map(AnotherEntityDTO);
}
or you can define a new Mapper AnotherEntityMapper for the AnotherEntity and use #Mapper(uses = {AnotherEntityMapper.class}):
#Mapper
public interface AnotherEntityMapper {
#Mapping(target = "propE", source = "propD")
AnotherEntity map(AnotherEntityDTO);
}
#Mapper(uses = {AnotherEntityMapper.class}
public interface EntityMapper {
#Mapping(target = "anotherEntity", source = "anotherEntityDTO")
Entity map(EntityDDTO dto);
}
It really depends on your use case. If you need to do mappings between AnotherEntity and AnotherEntityDTO on other places, I would suggest to use a new interface so you can reuse it where you need it
I have a class (many more fields than defined below, but you get the basic idea):
public class Embedded
{
public int Field1{get;set;}
}
public class Source
{
public int Field2{get;set;}
public Embedded Embedded{get;set;}
}
public class Destination
{
public int Field1{get;set;}
public int Field2{get;set;}
}
The normal approach would be:
Mapper.Initialise(cfg=>
{
cfg.CreateMap<Source, Destination>(dest=>dest.Field1, opt=>opt.MapFrom(src=>src.Embedded.Field1));
}
My Embedded object has many fields (and I have multiple embedded objects) and they will map by convention to the fields in the Destination object.
I need something like the functionality provided by IncludeBase<> but to be able to specify which field should be used to use as the src.
Is there a simpler way of doing this?
I found Map<,>(s,d) and AfterMap:
Mapper.Initialize(cfg=>{
cfg.CreateMap<Embedded, Destination>();
cfg.CreateMap<Source, Destination>()
.AfterMap((s,d) {
Mapper.Map(s.Embedded, d);
}
});
var src = new Source{
Embedded = new Embedded();
}
var dest = Mapper.Map<Source, Destination>(src);
How can I map a List<string> to List<Class>?
Usecase: from the Webservice I'm getting a class with a list of string but in my MVC Viewmodel, I want to have Class instead with a single property, which has the value of the string. That way I can add Validation attributes to the property.
I have the way how I convert the List into a List, however I can't get the other way around to work.
Any simple solutions?
The way to do this with AutoMapper is to use .ConstructUsing:
Mapper.CreateMap<string, Class>()
.ConstructUsing(str => new Class { MyProp = str });
Example: https://dotnetfiddle.net/0Vlc8b
You can easily do it with Linq.
var newList = oldList.Select(x => new Item(x)).ToList();
You could do this:
void Main()
{
AutoMapper.Mapper.CreateMap<string,A>()
.ForMember(a => a.Name, m => m.MapFrom(s => s));
new[] {"A", "B"}.Select (AutoMapper.Mapper.Map<A>).Dump();
}
class A
{
public string Name { get; set; }
}
(Linqpad code)
But I think this can go down as a textbook example of over-engineering. Just do it as in Daniel's example.