lightblue-platform / lightblue-migrator

GNU General Public License v3.0
3 stars 13 forks source link

Init facade as dynamic proxy (using InvocationHandler) #269

Closed paterczm closed 8 years ago

paterczm commented 8 years ago

This is https://github.com/lightblue-platform/lightblue-migrator/issues/171 implementation. The benefit is less code to write and maintain. Example:

Current solution:

public class DAOFacadeExample extends DAOFacadeBase<CountryDAO> implements CountryDAO {     

    public DAOFacadeExample(CountryDAO legacyDAO, CountryDAO lightblueDAO) {        
        super(legacyDAO, lightblueDAO);     
    }       

    @Override       
    public Country createCountry(Country country) throws CountryException {     
        try {       
            return callDAOCreateSingleMethod(entityIdExtractor, Country.class, "createCountry", country);       
        } catch (CountryException ce) {     
            throw ce;       
        } catch (Throwable t) {     
            throw new RuntimeException(t);      
        }       
    }

   ( ... )

Dynamic proxy solution:

public class DAOFacadeExample implements CountryDAO {

    CountryDAO facade;

    public DAOFacadeExample(CountryDAO legacyDAO, CountryDAO lightblueDAO) {        
        facade = FacadeProxyFactory.createFacadeProxy(legacyDAO, lightblueDAO, );
    }       

    @Override       
    public Country createCountry(Country country) throws CountryException {     
        return facade.createCountry(country);
    }

   ( ... )

The facade knows which operation this is based on the ReadOperation, WriteOperation and UpdateOperation annotations which need to be added to CountryDAO apis. No annotation means the call will be sent to legacyDAO, no matter the togglz configuration.

This change is backwards compatible, you can still extend DAOFacadeBase if you prefer.

paterczm commented 8 years ago

@alechenninger, @jblashka, I think you guys proposed this originally. Please take a look.