I'm struggling a bit with the class. Let me set the scene. I have a Java server code that uses a service level and orchestras. The request enters the service level in bean format (a java class matching the front end representation), and then I have a group of domainBeanMapper classes that take a bean object and translate it into a domain format object. For example, UserBean has dateOfBirth represented by a string, while User has dateOfBirth represented by Date, so UserMapper.java will make the date string into a date object. Therefore, for each object in the system, I have * .java, * Bean.java and * Mapper.java (User.java, UserBean.java, userMapper.java).
in applicationContext, I save the relationship from each object to their cartographer, for example:
<util:map id="domainBeanMappers"> <entry key="UserBean" value-ref="userMapper" /> <entry key="User" value-ref="userMapper" /> .....
and then I define mapper:
<bean id="userMapper" class="com.me.mapping.UserMapper" parent="baseDomainBeanMapper"/>
I call bean domain mapers like this from my service level:
UserBean userBean = (UserBean) getDomainBeanMapper().mapDomainToBean(user);
When I run this code, I find the mapper object that I need as follows:
DomainBeanMapper mapper = findApplicableMapper(myObject.getClass().getName()); if (mapper == null) { mapper = findApplicableMapper(myObject.getClass().getSimpleName()); }
where findApplicableMapper works as follows:
private DomainBeanMapper findApplicableMapper(String string) { return domainBeanMappers.get(string); }
Over the past few years, it has worked like a charm. For any object that I want on the system, I can easily select the appropriate resolver, and then transfer from my bean format to the domain format and vice versa, based on the call to .getClass () of any instance.
Now I have a new requirement that is causing me problems. I want to be able to translate from my User object into multiple sub-objects based on a parameter. So for some calls, I want to return only id, firstName and lastName. For other calls, I want more fields, but still not the whole object, and then for some calls I want the whole object to be returned as before. I do not want to follow the path of subjects and end with UserLight, UserLightWithName, UserLightWithNameButNoAddress, ... argh nightmare.
So instead, I was hoping to create a bunch of interfaces representing "views." This way, the request comes with ViewType Basic, and that means that I want the user personal data and address. So I wrote the UserBasic interface, got the User to implement it, and added the mapping from UserBasic to UserBasicMapper and from UserBasicBean to UserBasicMapper in the hope that I can make the transfer call as follows:
UserBasicBean restrictedUserReturn = (UserBasicBean) getDomainBeanMapper().mapDomainToBean((UserBasic)user);
but this does not work, because getClass () always returns the class of instances, and not the interface to which it was added. I think I can always add another paramater to the mapDomainToBean call, which is the class that I want to use, but the code base is pretty massive, and I will need to touch every call if I make this change.
So basically, I'm looking for a way to cast an instance to an interface type, and then find that interface type? Is this possible ??
crosses fingers ...