|
DTO
DTO conversion automaticcally
IntroductionWe need Data Transfer Object to pass object between presentation and business layers, or between remoting invocation. We use Dozer and @Delegator to do this for us. SetupFirst, we need config the bean mapping file location: <bean class="cn.muthos.polyforms.conversion.BeanMappingFile"> <constructor-arg value="bean-mappings.xml" /> </bean> Then, we add the bean-mappings.xml file: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mappings PUBLIC "-//DOZER//DTD MAPPINGS//EN"
"http://dozer.sourceforge.net/dtd/dozerbeanmapping.dtd">
<mappings>
<configuration>
<stop-on-errors>true</stop-on-errors>
<date-format>MM/dd/yyyy HH:mm:ss</date-format>
<wildcard>true</wildcard>
<trim-strings>false</trim-strings>
</configuration>
</mappings>Details
When disable user, we need pass User object as the parameter, but from presentation layer, we just pass the user name is enough. So we add disable method to UserService and delegate to UserManager:
We also need add converter for conversion between entity identity and entity in bean-mappings.xml. <custom-converters> <converter type="cn.muthos.polyforms.conversion.dozer.EntityConverter"> <class-a>java.lang.String</class-a> <class-b>cn.muthos.polyforms.sample.domain.User</class-b> </converter> </custom-converters>
We want to pass minimal info when creating user, so we using UserDto to pass info between presentation and business layers:@Delegator(bean = "userRepository", value = "save") UserDto addUser(UserDto user); |
Sign in to add a comment