atos1990 / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

Flexible field-name mapping is needed. #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have to define a global strategy which tell Orika how to find property from 
Entity for property from Dto and back. Orika suggests solution for a particular 
class only.
For example:

 public class UserExtendedInfo extends BaseDto {
    private String groupName;

     @myannotation.Field("group.name")   
     public String getGroupName() {
       return groupName;
     }
     ...
 }

public class User extends BaseEntity {
  private UserGroup group;
   ...
  }

Orika should generate code looks like:
userExtendedInfo.setGroupName(
   user.getGroup().getName();
)

One of case is to register similar DefaultFieldMapper:

defaultMapperFactoryBuilder.registerFieldStrategy(
 new FieldMapperStrategy() {
  /**
  *@param  destinationClass ( in this case UserExtendedInfo.class)
  *@param  sourceClass ( in this case User.class)
  *@param  destinationPropertyName ( in this case"groupName")
  *return propertyName (in this case "group.name:)
  **/
 public String suggestSourcePropertyName(String destinationPropertyName, Class destinationClass, Class sourceClass ) { 
      //Custom logic. It is desirable, that caching the result.  

   }
}
);

Original issue reported on code.google.com by dkhomya...@gmail.com on 16 Jul 2012 at 7:47

GoogleCodeExporter commented 9 years ago
Would your need be accomplished if you were able to customize the behavior of 
ClassMapBuilder#byDefault()?

Original comment by matt.deb...@gmail.com on 19 Jul 2012 at 12:34

GoogleCodeExporter commented 9 years ago
I do not quite understand. ClassMapBuilder provides  mapping for a particular 
class.

factory.registerClassMap(ClassMapBuilder.map(Entity1.class,Dto1.class)
...
 .toClassMap()); 

factory.registerClassMap(ClassMapBuilder.map(Entity2.class,Dto2.class)
...
 .toClassMap()); 
 ...
and so on

But, I have to develop api which doesn't demand registrate every DTO.

If I could change  behaviour "public ClassMapBuilder<A, B> 
byDefault(DefaultFieldMapper... defaults)" globally for all instances of 
ClassMapBuilder.class, than maybe yes, my need would be accomplished.

Original comment by dkhomya...@gmail.com on 19 Jul 2012 at 12:01

GoogleCodeExporter commented 9 years ago
Ok, let me clarify: ClassMapBuilder#byDefault() can be called manually, but it 
is also the method used internally by Orika when you try to map a pair of types 
which have not yet been registered.
Essentially, Orika does this on this fly:
   ClassMapBuilder.map(UnmappedA.class,UnmappedB.class)
    .byDefault()
    .toClassMap()); 
So, if you were in control of the byDefault() method, you'd be controlling 
exactly how the classes are mapped on the fly.
Does this make sense now?

Original comment by matt.deb...@gmail.com on 19 Jul 2012 at 4:11

GoogleCodeExporter commented 9 years ago
I see. In customization of byDefault() I'll be to parse target classes's 
anatations and registrate field- mapping. It would be nice. 

Original comment by dkhomya...@gmail.com on 19 Jul 2012 at 4:59

GoogleCodeExporter commented 9 years ago
Please check now in the 1.2.0 branch; with the most recent changes, 
ClassMapBuilder can be extended.
To do this, you should also extend ClassMapBuilderFactory--override the 
'newClassMapBuilder' method to return your own extension of ClassMapBuilder.
Then, you can set this factory to be used by the DefaultMapperFactory; use the 
'classMapBuilderFactory()' method on DefaultMapperFactory.Builder to have your 
ClassMapBuilderFactory be used by the 'classMap()' methods on 
DefaultMapperFactory.
If you have some time, please let me know how if this resolves your issue...

Original comment by matt.deb...@gmail.com on 18 Aug 2012 at 12:47

GoogleCodeExporter commented 9 years ago

Original comment by matt.deb...@gmail.com on 18 Aug 2012 at 12:51

GoogleCodeExporter commented 9 years ago
I've checked 1.2.0 branch. The solution works fine and resolves my issue. 
Probably, you should add getters of private fields 
ClassMapBuilderFactory(propertyResolver, defaults) and ClassMapBuilder 
(defaults). Because I must have access to them in my extensions. While I 
override setters for saving reference to them.

Original comment by dkhomya...@gmail.com on 20 Aug 2012 at 2:19

GoogleCodeExporter commented 9 years ago
Thanks s lot!

Original comment by dkhomya...@gmail.com on 20 Aug 2012 at 2:23

GoogleCodeExporter commented 9 years ago
Glad to help.

Also, getters have been added for the two fields you asked about 
(propertyResolver and defaults).

Original comment by matt.deb...@gmail.com on 3 Sep 2012 at 9:35

GoogleCodeExporter commented 9 years ago
Fixed in 1.2.0 release.

Original comment by matt.deb...@gmail.com on 7 Sep 2012 at 9:54