I have ClassA, ClassB, ClassC where:
ClassB extends ClassA
ClassC extends ClassA
I wanted to map only ClassA in my dozer configuration and wrote this:
<mapping wildcard="false">
<class-a>ClassA</class-a>
<class-b>ClassA</class-b>
<field custom-converter="MyConverter">
<a>value</a>
<b>value</b>
</field>
</mapping>
However, when I do mapper.map(instB, instB) the custom converter from ClassA never gets called.
Thank you!
Related
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.
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 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.
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.
I have one interface with 2 classes implementing it, I need to load each class but unity has:
m_unityContainer.Resolve() // Where is the interface IGeneric
my config looks like:
<type type="IGeneric" mapTo="ClassA">
</type>
<type type="IGeneric" mapTo="ClassB">
</type>
any ideas?
thanks
You could also use a generic interface as follow:
public interface IGeneric{}
public interface IGeneric<T> : IGeneric{}
Then have a type safe resolution of the the interface:
container.RegisterType<IGeneric<ClassA>, ClassA>();
container.RegisterType<IGeneric<ClassB>, ClassB>();
ClassA classA = container.Resolve<IGeneric<ClassA>>();
ClassB classB = container.Resolve<IGeneric<ClassB>>();
Some interesting things start happening when you go down this road...
This will give you all the registered classes that implement IGeneric.
IEnumerable<IGeneric> objects = container.ResolveAll<IGeneric>();
I found out the solution, a name property has to be used in each entry:
and the code will look like
obj= container.ResolveAll("ClassA");
Schalk - looks good. What would be the notation for specifying that in the Unity.config?
Here is a slightly different way. I am using Unity.2.1.505.2 (just in case that makes a difference).
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<container>
<register type="IVehicle" mapTo="Car" name="myCarKey" />
<register type="IVehicle" mapTo="Truck" name="myTruckKey" />
</container>
</unity>
Here is the DotNet code.
UnityContainer container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);
IVehicle v1 = container.Resolve<IVehicle>("myCarKey");
IVehicle v2 = container.Resolve<IVehicle>("myTruckKey");
See:
http://msdn.microsoft.com/en-us/library/ff664762(v=pandp.50).aspx