How to map 2 different objects together
In my current project I have 2 modules ModuleA and ModuleB, and inside ModuleA and ModuleB I have a class called 'Student' (same class name, same attributes, but for some purpose ModuleA must call ModuleB to do the actual task). They communicate to each other through Web Services开发者_C百科. Now I want ModuleA web service to call ModuleB proxy to do the actual task.
In my ModuleA web service I have a method to create a record:
public void createStudent(ModuleA.Student student){
// Here will call ModuleB proxy to do the actual task which is create.
*moduleBFacade().createStudent( );*
}
In my ModuleB Proxy:
public void createStudent(ModuleB.Student student){}
So now the problem is, I cannot pass the moduleA object into the createStudent method as it only takes moduleB objects as arguments.
Any idea how to deal this problem? Please give me some suggestions.
As you are invoking with WS can you convert the moduleA.Student to xml and then change the namespace of the xml and then instantiate a moduleB.Student object from xml.
Something like:
String xmlA = moduleA.Student.toXml();
//Change namespace. Also, Compare the genrated xml of ModuleA and ModuleB.
ModuleB.BStudent studentB= StudentDocument.Factory.parse(xmlA, ..);//second argument can be diff namespace
*moduleBFacade().createStudent(studentB);
You cannot change the class of an object in Java. Also, you cannot "merge" two classes into one class. What you could do is to introduce a common interface, but for that you must own the sourcecode of both classes.
Given the constraint that you can change neither of the two classes, then manually converting ModuleA.Student
to ModuleB.Student
and back is the best option you get.
PS: as an alternative you can use reflection. Given that both classes have the same attribut names, then mapping from one class to the other should not be a problem.
public static <A,B> B convert(A instance, Class<B> targetClass) throws Exception {
B target = (B) targetClass.newInstance();
for (Field targetField: targetClass.getFields()) {
Field field = instance.getClass().getField(targetField.getName());
targetField.set(target, field.get(instance));
}
return target;
}
Usage:
StudentB studentB = convert(studentA, StudentB.class);
The example above assumed that all fields are private. If they are not, then the same can be can done with methods (module mapping setter names to getter names).
Circular dependency == bad design.
Redesign the modules to remove the circular dependency.
Might not sound right, but here:
Java code
supposing student object passed is of type ModuleAStudent
//create a new bStudent with main criteria
ModuleBStudent bStudent = new ModuleBStudent();
bStudent.setStudentId(student.getStudentId());
bStudent.setStudentNo(student.getStudentNo());
//finally
moduleBFacade().createStudent(bStudent);
UPDATE
Since your object is the same in the two packages and you are making a web service, i would suggest this Simple framework, yea its called Simple actually. Simple helps you serialize your object to XML and deserialize it back, pretty Simple.
You can use BeanUtils.copyProperties to copy to similar beans (note, this is a shallow copy)
精彩评论