سؤال

so we are developing classic multi-tier application and one of the usual things we often need to do is to map between domain entities, DTOs, other DTOs from different layers etc.

Right now, we are doing this manually and I am actually quite fine with that. Some colleagues are bothered by this and want to have some more automatic way of object mapping - something like Dozer, Apache BeanUtils etc. What I don't like is that using this approach means losing static checking and refactoring - if you change property name in your domain entity, application will be broken, but compiler won't warn you - you will crash hard during run-time. Colleagues argue that once you will have everything covered by unit-tests, these errors will be catched. I'm not so sure that this will always be the case.

However, I'd like to find solution which has the best of both worlds - safety of manual method and easiness and conciseness of semi-automatic mapping.

I imagine that safety would be checked during application startup - this checking code would find all entities with defined mapping and would check if all mapped attributes have corresponding attributes in the other class.

Does any mapping library do this? I couldn't find any info on this. I know that this could be quite easy to implement, but standard existing solution would be much better argument to use it. Thanks!

هل كانت مفيدة؟

المحلول

I would say Selma is definitely the tool you are looking for.

This mapping frameworks is built as an Annotation Processor, so it generates the mapping code at compile time. You define an interface to specify the mapping contract. Selma will raise compilation failure if things are going badly with explainfull logs.

It is built to be refactor proof because, Selma generates the code on compilation and moreover you'll be able to browse the mapping sources.

Give it a try and tell me.

نصائح أخرى

MapStruct is very similar to Selma - it also generates mapping code using annotation processor, should be type safe and refactoring-proof.

http://mapstruct.org/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top