views:

121

answers:

5

My current project got 2 modules ModuleA and ModuleB, inside ModuleA and ModuleB got a class call '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. Now I want ModuleA WS will call ModuleB proxy to do the actual task.

In my ModuleA WS I got 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 can not pass in the moduleA object into the createStudent method as it take in moduleB object.

Any idea how to deal this problem? Please give me some suggestion.

A: 

Circular dependency == bad design.

Redesign the modules to remove the circular dependency.

duffymo
Sorry, I've got nothing. Why can't you redesign? If it doesn't work, what does your current code do for you?
duffymo
+1  A: 

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).

Adrian
How many of these similar classes do you have? ^^
Adrian
hi Adrian thanks, I tried out your method reflect the object no error but it return me no value, studentA value wont be reflect to studentB?
I dont understand, can you rephrase your question? The `convert` method is supposed to copy all attributes from A to B, once, so to get the values back from B to A, you must call `convert` to other way round again.
Adrian
+1  A: 

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);
BSingh
Whats the framework you are using for accesing the WebService?. Most WS frameworks, like Axis2, will use some binding framework to marshall the object to xml based on the wsdl. The binding framework provides the implemenation of the toXml() and also helper classes like StudentDocument during wsdl compile.
BSingh
I am using JAXB to bind to java to xml
A: 

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.

medopal
A: 

You can use BeanUtils.copyProperties to copy to similar beans (note, this is a shallow copy)

Ehrann Mehdan