lundi 6 décembre 2021

Design Pattern question - Implementing DAO pattern with Modern Java

We've inherited some code where there is a DAO pattern implemented as such:


interface DomainMapper<S, T> {
    T mapObjectModel(S source);
}
public abstract class AbstractDao<Source, Target> implements DomainMapper<Source, Target> {
    protected List<T> execute(SqlParameter params...) {
        // Executes some query
        public S mapRow(ResultSet rs, int rowNum) {
            // does the mapping
            S source = super.mapRow(rs, rowNum);
            T value = mapObjectModel(s);
        }
    }
}
public class SpecificDAO extends AbstractDAO<Source, Target> {
    // Other functions
    public Target mapObjectModel(Source s) {
         // Mapping implementation here
    }
}

We have about 20 DAO's implemented in the similar fashion.

Here's the question

  1. There is a specific DAO that needs some kind of state to persist between calls. I mean, between every call to mapObjectModel() with the Source, I need to store some value that I will use during the next call. for e.g. imagine Parent - Child relation, the mapObjectModel will be called for every child with the Parent and I want to create an Object graph something like below.

    public class Parent { private List<Child> children; }

so, I want to create a "context" that I can refer to in the next call and get the Parent and add children to it.

What is the best and easy way to update the mapObjectModel() to carry state information between calls and with minimal change to the existing DAO's?

Is there a pattern that can solve this problem for me?

Thanks

Aucun commentaire:

Enregistrer un commentaire