Dozer mapping of Property of a Object in the list to another list with hints - dozer

I have the following scenario of Mapping
Class Contact {
List<SimpleCode> marketSectorList;
}
Class SimpleCode {
protected String code;
protected String label;
}
Class ContactTarget {
List<String> marketSectors;
}
The following map is not working
<mapping>
<class-a>Contact</class-a>
<class-b>ContactTarget</class-b>
<field>
<a>marketSectorList</a>
<b>marketSectors</b>
<b-hint>java.lang.String</b-hint>
</field>
The mapping is not working. Please note that I can not change the classes and I would like to solve by using hints not by custom mappers

You could try to create a mapping from SimpleCode to java.lang.String so dozer knows how to map those objects. Something similar to this:
<mapping>
<class-a>SimpleCode</class-a>
<class-b>java.lang.String</class-b>
<field>
<a>code</a>
<b>this</b>
</field>
</mapping>
I hope it helps.

Related

Xstream attribute and implict collection ambiguity

Hope you are fine.
Can you please help me to resolve my below problem. I have an xml element with attribute named as “value” which can have one value E.g. (1) below , but there can be another case which i can have it as a child element named as “value” with multiple values E.g. (2) below.
E.g. 1
<variable id="123" value="Adam"/>
E.g. 2
<variable id="123">
<value>Adam</value>
<value>Philip</value>
... ...
</variable>
Can you please tell me how would i map this in Xstream serializer in same java class?
if i configure as below it gives me duplicate error
#XStreamAlias(“variable”)
public class Variable {
#XStreamAsAttribute
private String id;
#XStreamAsAttribute
private String value;
#XStreamImplicit
private List<String> value = new ArrayList<String>();

Dozer mapping collection

I am new to using dozer. I need to map a collection to a class that is an attribute for the source class in the collect.
I have a class technology as follows
Class Technology {
String name
List<TechnologyOwner> techOwners
}
that I would like to map to
Class TechSummary {
String name
List<Employee> techOwners
}
where
class TechOwner {
Employee techOwner;
Date sinceDt;
}
How will my dozer mapping look like?
Below is the resolve scheme:
1. add mapping configuration in dozerBeanMapping.xml:
<mapping>
<class-a>demo.Technology</class-a>
<class-b>demo.TechSummary</class-b>
<field>
<a>techOwners</a>
<b>techOwners</b>
<b-hint>java.lang.String</b-hint>
</field>
</mapping>
2.use dozer mapper to create destination class:
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
TechSummary techSummary = mapper.map(t,TechSummary.class);
It will be worked.

Indexed getter only in Dozer

I'm struggling with a mapping in Dozer. The basic structure of my classes is this:
class Foo {
private String someString;
public String getSomeString() {
return someString;
}
public void setSomeString(String someString) {
this.someString = someString;
}
}
and the interesting part:
class Bar {
// note that no field is declared
public String[0] getSomeBarString() {
// This returns an array where the acctually desired string is a index 0
}
public void setSomeBarString(String someString) {
// stores the string otherwise
}
}
Compensating the absence of a field and the differently named getter/setter methods was quite easy:
<mapping>
<class-a>Foo</class-a>
<class-b>Bar</class-b>
<field>
<a>someString</a>
<b get-method="getSomeBarString" set-method="setSomeBarString">someBarString</b>
</field>
</mapping>
From my understanding I could even omitt get-method and set-method as there is no field access by default.
My problem is that the getter is indexed and the setter isn't. I've already read about indexed property mapping but it does it both ways. Is there a way to make only one direction indexed? E.g. would get-method="getSomeBarString[0]" work?
After a night of sleep I got an idea myself. I just define two one way mappings and make one of them indexed. It also turns out indexing is defined the same way (after the property name) even if you declare a different get-method or set-method.
<mapping type="one-way">
<class-a>Foo</class-a>
<class-b>Bar</class-b>
<field>
<a>someString</a>
<b set-method="setSomeBarString">someBarString</b>
</field>
</mapping>
<mapping type="one-way">
<class-a>Bar</class-a>
<class-b>Foo</class-b>
<field>
<a get-method="getSomeBarString">someBarString[0]</a>
<b>someString</b>
</field>
</mapping>

I need to check for validity of data before deep mapping with dozer, can I?

I'm using dozer to map between my Model Entities and my DTOs.
Now I'm facing with the problem that I need to map some properties of classA.classC to different properties of classB, but first I need to check for inconsistency, because if I don't classC will throws exception and the mapping will not work.
So assume that I have:
class ClassA {
private String name;
private ClassC c;
public ClassC getC() throws ValidityException;
}
class ClassB {
private String code;
private Integer value;
}
class ClassC {
private String name;
private Integer value;
// Getters & Setters below
}
So now I want to map like this:
<mapping>
<class-a>ClassA</class-a>
<class-b>ClassB</class-b>
<field>
<a>c.name</a>
<b>code</b>
</field>
<field>
<a>c.value</a>
<b>value</b>
</field>
</mapping>
if access to ClassC instance from ClassA instance throws exception, I will need to map null for both b properties.
From what I was reading I assume that I should use a CustomConverter in order to access ClassC instance catch the exception and map null in that cases, but not sure how can I implement this kind of converter.
Anyone could give me some ideas about how this can be implemented using Dozer?
Are you sure you wrote the correct mapping? Because ,
<field>
<a>c.name</a>
<b>name</b>
In above snippet, you wrote name for classB. Actually it should be code.

Cascading Resolution in Unity

Suppose I have two types, TypeA and TypeB, that I want to register with Unity. TypeB depends on TypeA so I want to inject TypeA into type B through constructor injection. So I would like to write something like the following and have Unity be smart enough to cascade the resolution for me:
_container.RegisterType<ITypeA, TypeA>();
_container.RegisterType<ITypeB, TypeB>();
How can I tell Unity to resolve TypeA and inject into TypeB?
It looks like this is possible if using a config file, but I don't know how you would do it programmaticaly:
<type name="typeB" type="ITypeB" mapTo="TypeB">
<lifetime type="Singleton"/>
<typeConfig extensionType="...">
<constructor>
<param name="typeA" parameterType="ITypeA">
<dependency/>
</param>
</constructor>
</typeConfig>
</type>
Thanks in advance for any suggestions!
EDIT: So, Unity does handle this for me. However, I think my issue is that I have a class with two constructors:
public TypeB(TypeA typeA)
{
_x = typeA;
}
public TypeB() : this(Something.Value)
{
}
It seems that Unity is having trouble knowing which constructor to use. The first constructor is for unit testing and the second should be used at during runtime. Unity is having trouble with this.
You do it like this:
class TypeA
{
}
class TypeB
{
[InjectionConstructor]
public TypeB([Dependency] TypeA typeOfA)
{
}
}