Skip to content
JJaraM edited this page Mar 3, 2016 · 1 revision

logo

  1. [What is Chameleon?](# What is Chameleon)
  2. [What Is Hystrix For?](#What Is Hystrix For?)

What is Chameleon?

Is a java framework that helps the conversion of POJO and JPA object to DTO Objects, without the necessity to create heavy factories and deal with the creation of the subrelations objects.

For example Chameleon will prevent this.

public class Factory {

     public static Factory instance = null;

     public static Factory getInstance() {
          if (instance == null) {
               instance = new Factory();
          }
          return instance;
     }

     public CarDTO create(Car car) {
          CarDTO carDTO = new CarDTO();          
          carDTO.setModel(car.getModel()); 
          Motor motor = car.getMotor(); //Problem when deal with relations 
          MotorDTO motorDTO = new MotorDTO();
          motorDTO.setId(motor.getId());          
          carDTO.setMotor(motorDTO);
     }

}

As you can see we are creating a factory and passing the attributes to a new object, sometimes this task can be big because what happen if we want convert an object to DTO with 10 relations, we need to create 10 factories classes and set the desire attributes, and when we can to reuse the factory this can be difficult because not always we want to display the same attributes.

With Chameleon the only thing that we need to do is to create a simple Repository class and define the attributes that we want to use as the next example:

Repository:

@Repository
public interface PlaceDTORepository {
    @Query("SELECT V.referenceId, V.name, P.referenceId, P.prefix, P.suffix, P.width, P.height FROM Place V JOIN Photos P")
    Set<PlaceDTO> fetchNearPlacesByLocationName(Set<Place> source);
}

And in the place that we want to use we need to inject the instance:

public class PlaceController {
     public Set<PlaceDTO> getList() {
          Set<Place> places = someMethodRetrieveAJPACollection(...);
          placeDTORepository.fetchNearPlacesByLocationName(places);
     }
}

As you can see we are not working any more with factories, and the Chameleon framework deal with the heavy relations and Lazy JPA collections.

Clone this wiki locally