steve-perkins / fitnessjiffy-spring

Diet and exercise tracker... a robust Spring Boot sample application
http://steveperkins.com/use-spring-boot-next-project/
115 stars 52 forks source link

userDTOConverter #3

Closed thiagogcm closed 9 years ago

thiagogcm commented 9 years ago

Why don't you use the UserToUserDTO in the UserService ?

steve-perkins commented 9 years ago

Hmm... which method(s) in UserService are you talking about? It looks like I am using the converter in all methods that return a UserDTO type return value.

thiagogcm commented 9 years ago

you have the UserToUserDTO in net.steveperkins.fitnessjiffy.dto.converter that implements a converter but in UserService you use:

@Autowired
private Converter<User, UserDTO> userDTOConverter;

Is that correct?

steve-perkins commented 9 years ago

Oh, so are you asking why the dependency-injected type is Converter<User, UserDTO>, rather than the more specific subclass UserToUserDTO?

Well, the short answer is that when I started this project, I patterned that DTO marshalling/unmarshalling code from an example I read somewhere... and injecting the parent type is how THEY did it! It does work fine like that. If you set a breakpoint and debug the unit tests, you'll see that what actually gets injected is an instance of UserToUserDTO.

However, your question made me curious. Barring some kind of odd "Spring magic", there indeed seems to be no reason why you couldn't inject the specific subclass. So I tried that out, and that works fine as well! Same thing, but much more clear and readable.

I'll switch all of the converters over on the next commit. (Note that I'm doing all my work in the reports branch right now. I'll be merging back to master in a week or two.) Thanks!