Coder Social home page Coder Social logo

jjaram / chameleon Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 100 KB

Is a java framework that helps the copy objects that have the same attributes types, without the necessity to create heavy factories and deal with the creation of the subrelations objects.

Java 100.00%
jpa spring-aop spring-beans aspectjweaver java java-proxy javassist

chameleon's Introduction

logo

What is Chameleon?

Is a java framework that helps the copy objects that have the same attributes types, without the necessity to create heavy factories and deal with the creation of the subrelations objects.

Problem to attack

For example Chameleon will prevent this.

public class CarFactory {

     public static CarFactory instance = null;

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

     public CarDTO create(Car car) {
          Motor motor = MotorFactory.getInstance().create(car.getMotor()); // First factory created because we have a relation
          MotorDTO motorDTO = new MotorDTO(); // Object from relation
          motorDTO.setId(motor.getId());
          CarDTO carDTO = new CarDTO(); 
          carDTO.setModel(car.getModel()); 
          carDTO.setYear(car.getYear());
          carDTO.setMotor(motorDTO);
          return carDTO;
     }
}

Representation of MotorFactory class.

public class MotorFactory {

     public static MotorFactory instance = null;

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

     public MotorDTO create(Motor motor) {
          MotorDTO motorDTO = new MotorDTO();
          motorDTO.setId(motor.getId());
          return motorDTO;
     }
}

In the previous example we are creating a new CarDTO, maybe we want to return this value from an API and hide some fields, this scenario is the most common, we create a factory for each class, but this work can be boring and very difficult to give support, why?, imagine the next scenario; we want to display the all cars but we need to display only the model and we need to hide the year. Many people creates a new method in CarFactory and pass the object but ignore the line.

public class CarFactory {

     public static CarFactory instance = null;

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

     public CarDTO create(Car car) {
          Motor motor = MotorFactory.getInstance().create(car.getMotor()); // First factory created because we have a relation
          MotorDTO motorDTO = new MotorDTO(); // Object from relation
          motorDTO.setId(motor.getId());
          CarDTO carDTO = new CarDTO(); 
          carDTO.setModel(car.getModel()); 
          carDTO.setYear(car.getYear());
          carDTO.setMotor(motorDTO);
          return carDTO;
     }
     
       public CarDTO createWithoutYear(Car car) {
          car.setYear(null);
          return create(car);
     }
}

As you can see we are adding a little of complexity to our factory, because can be many of possiblities, and each posiblity means a new method.

How chameleon will solved this problem?

As you saw in the last section we had the problem to create to many methods in a factory, the propose to chameleon is manage this creation using a similar sintax as SQL, we can use this query and select the fields that we want and ignore the all complexity of object creation.

public interface PlaceDTORepository {
     @Query("SELECT C.model, C.year, C.id, M.id FROM Car c JOIN Motor M")
     Set<PlaceDTO> fetchCollection(Set<Car> cars);
}

As you saw the only thing that we need is to create a simple interface, add the expected result and the columns that we want to display.

public interface CarDTORepository {
     @Query("SELECT C.model, C.year, C.id, M.id FROM Car c JOIN Motor M")
     Set<PlaceDTO> fetchCollection(Set<Car> cars);
    
     @Query("SELECT C.model, C.id, M.id FROM Car c JOIN Motor M")
     Set<PlaceDTO> fetchCollectionWithoutCarYear(Set<Car> cars);
}

And to call out repository we need to inject our class in the desire place.

public class PlaceController {

     @Resource private CarDTORepository carDTORepository;
     
     public Set<PlaceDTO> getList() {
          Set<Car> cars = someMethodRetrieveAJPACollection(...);
          return carDTORepository.fetchCollection(cars);
     }
}

As you can see we are not working any more with factories, and the Chameleon framework deal with the object creations.

Dependencies

  • spring-aop 4.2.5.RELEASE
  • spring-beans 4.2.4.RELEASE
  • spring-context 4.2.4.RELEASE
  • spring-aop 4.2.5.RELEASE
  • aspectjweaver 1.8.8
  • aspectjrt 1.8.8
  • hibernate-core 5.1.0.Final

chameleon's People

Contributors

jjaram avatar

Watchers

 avatar  avatar

chameleon's Issues

Create proxy for repository

Actually when we want to create queries using @query we need to create a new class and add the annotation but in the body we need to return a null value as the example:

@Component
public class PlaceDTORepository {

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

So in the new solution we want to create queries in a interface, like SpringData.

Create reader of properties

We need to create a reader properties, because currently with the creation of the ticket #2 we set the ProxyVendorAdapter by default using hibernate.

public class ChameleonVendorAdapterStrategy {

    public static VendorProxyAdapter getInstance(Field field, String fieldName, Object data) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        VendorProxyAdapter proxy = new DefaultProxyChameleon(field, fieldName, data);
        if (proxy.getValue() != null) {
            try {
                Class<?> clazz = Class.forName("com.jjm.chameleon.support.proxy.jpa.HibernateVendorProxyAdapter");
                Constructor<?> constructor = clazz.getConstructor(Object.class, Field.class);
                proxy = (VendorProxyAdapter) constructor.newInstance(new Object[] { proxy.getValue(), field });
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return proxy;
    }
}

Create jpa adapter

Actually we have the hibernate logic linked in the principal project, we want to separate the logic to prevent create a big jar

public static ChameleonProxyAdapter getInstance(Field field, String fieldName, Object data) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
     ChameleonProxyAdapter proxy = new DefaultProxyChameleon(field, fieldName, data);
     if (proxy.getValue() != null) {
          if (proxy.getValue() instanceof HibernateProxy ) {
               proxy = new HibernateProxyChameleon(proxy.getValue(), field);
          } else if (proxy.getValue() instanceof PersistentSet) {
               proxy = new PersistentSetProxy(proxy.getValue(), field);
          }
     }
     return proxy;
}

Maybe we want think in create and herency of classes using Adapter Pattern

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.