This discussion is archived
3 Replies Latest reply: Nov 26, 2010 2:17 AM by 817377 RSS

Copy Objects

818146 Newbie
Currently Being Moderated
I am sure every one of you would have come across this type of code. We copy objects from Domain to application layer or from Data layer to business layer.
They are mere copy and if the DO's , BO's are long, we ahve to write so many methods like convertToDomain object etc. I am wondering if there is a pattern that we could apply to handle this generically.
One of the thing I am thinking of is to use reflection to copy object of similar type. For eg. PersonDO might have age, name and DOB, and PersonBO might have just name and DOB. Would it not be a good idea to copy over from PersonDO to PersonBO provided all variables match using reflection. It might sound dumb. But I don't like the various copy that we do, it literally fills up the java file.

Any thoughts would be helpful.

thanks
--
  • 1. Re: Copy Objects
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    815143 wrote:
    I am sure every one of you would have come across this type of code. We copy objects from Domain to application layer or from Data layer to business layer.
    I create DTOs as one set. Most of the time the set is inclusive enough that it can be used as is in all the layers.

    When it isn't then several groups are sufficient.
    They are mere copy and if the DO's , BO's are long, we ahve to write so many methods like convertToDomain object etc. I am wondering if there is a pattern that we could apply to handle this generically.
    One of the thing I am thinking of is to use reflection to copy object of similar type. For eg. PersonDO might have age, name and DOB, and PersonBO might have just name and DOB.
    Why can't they be in the same group?
    And why do you need to copy from one to the other?
    Would it not be a good idea to copy over from PersonDO to PersonBO provided all variables match using reflection.
    Code generation. Create the DTOs. Create the copy code (presuming it is actually needed.)
    It might sound dumb. But I don't like the various copy that we do, it literally fills up the java file.
    The reason certainly sounds odd. But so does the need to copy from one DTO to another.
  • 2. Re: Copy Objects
    801079 Newbie
    Currently Being Moderated
    Maintain a single object and pass it to different layers. Any specific reason to create a replica instance and use that instead?
  • 3. Re: Copy Objects
    817377 Newbie
    Currently Being Moderated
    There is one pattern which I had developed for some same scenario.

    What you can do is that use the custom annotation to copy the fields from one object to another.

    Here is the example:

    Create a Custom Annotation:

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.FIELD})
    public @interface MyField {
    String name();
    }

    ClassA {

    @MyField(name="CopyName")
    private String name;

    @MyField(name="CopyAge")
    private int age;

    ... getters and setters goes here
    }

    ClassB{

    @MyField(name="CopyName")
    private String myName;

    @MyField(name="CopyAge")
    private int currentAge;

    }

    Now you can copy the two different class based on the annotation using the reflection like this

    public void copyAtoB (ClassA objA, ClassB objB) {

    Field fields[] = objB.getClass().getDeclaredFields();

    for (Field f : fields) {

    Column annot = f.getAnnotation(MyField.class);
    if (annot != null) {
    Object tempobj = getValueFromObjectA(objA, f.getType(), annot.name()); // Needs to be implemented in same manner.
    String fieldName = f.getName();
    fieldName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    Method method = objB.getClass().getMethod(fieldName, f.getType());
    if (f.getType() == int.class) {
    method.invoke(objB, ((Integer) tempobj).intValue());
    } else {
    method.invoke(objB, tempobj);
    }
    }
    }

    }


    Note: Annotation name should be same for the fields which you want to copy.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points