Search code examples
javaspringentity-frameworkenumsdto

Entity to DTO conversion in a J2EE application using an enum?


This is one of those topics I don't even know how to search in google (tried already, most of the results were for C#), so here I go: I'm messing around with our huge application, trying to get to work a brand new DAO/Entity/Service/DTO.. euh...thing. I've been left more or less on my own, and, again, more or less, I'm getting to understand some of the hows and maybe one or two of the whys.

The thing is that I got all, the way "up", from the DB to the Service:

  • I got a DAO class which executes a query stored on an Entity class. After executing it, it returns the Entity with the values.
  • The service receives the Entity and, somehow, transforms the Entity to a DTO and returns it to whenever is needed.

My problem is with the "somehow" thing the code goes like this:

DTOClass dto = ClassTransformerFromEntityToDTO.INSTANCE.apply(entityQueryResult);

I went into ClassTransformerFromEntityToDTO and found this:

public enum ClassTransfomerFromEntityToDTO implements  Function<EntityClass,DTO Class> ) {
INSTANCE;

    @Override
    public DTOClass apply(EntityClass entityInstance) {
        /*Code to transform the Entity to DTO and the return*/
    }
}

The class that this... thing, implements, is this:

package com. google .common . base;

import com. google .common . annotations. GwtCompatible ;
import javax. annotation .Nullable ;

@GwtCompatible
public abstract interface Function <F , T >
{
  @Nullable
  public abstract T apply (@Nullable F paramF) ;

  public abstract boolean equals (@Nullable Object paramObject) ;
}

I'm in the classic "everyone who where at the beginning of the project fled", and no one knows why is this or what is this (The wisest one told me that maybe it had something to do with Spring), so, I have two main questions (which can be more or less answered in the same side):

1) What's this? What's the point of using an enum with a function to make a conversion?

2) What's the point of this? Why can I just make a class with a single function and forget about this wizardry?


Solution

  • not sure there's much to answer here... And I'm adding an answer to illustrate my thoughts with some code I've seen, but that you have is horrible. I've actually seem similar stuff. My guess is that that codes actually precedes Spring. It's used as some sort of Singleton.

    I have seen code like this, which is worse:

    public interface DTO {
        find(Object args)
    }
    public class ConcreteDTO1 implements DTO {
       ...
    }
    public class ConcreteDTO2 implements DTO {
       ...
    }
    public enum DTOType {
    
      CONCRETE_DTO1(new ConcreteDTO1(someArgs)),
      CONCRETE_DTO2(new ConcreteDTO2(someOtherArgs))
    
      private DTO dto;
      public DTOType(DTO dto) {
        this.dto = dto;
      }
    
      public DTO dto() {
        return dto;
      }
    }
    

    and then the DTOs are basically accessed through the Enum Type:

    DTOType.CONCRETE_DTO1.dto().find(args);
    

    So everyone trying to get hold of a DTO accesses it through the enum. With Spring, you don't need any of that. The IoC container is meant to avoid this kind of nonsense, that's why my guess is that it precedes Spring, from some ancient version of the app when Spring was not there. But it could be that someone was wired to do such things regardless of whether Spring was already in the app or not. For that kind of stuff you're trying to do, you're better of with the Visitor pattern. Here's an example from a different answer: passing different type of objects dynamically on same method