jeudi 10 décembre 2020

Composite Entity DAO performance Java and SQLite

First, sorry for my English.

I have a trouble/question/doub about DAO's and Entities. I've been developing an app for a long time. When I'm start, I use the following approach:

class Entity {
  int id;
  String name;
}

class Composed {
  int id;
  // others prop's
  Entity e;
}

class OtherComposed {
  int id
  // others prop's
  Composed c;
}

DAOEntity {
  private Connection c;
  DAOEntity {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Entity> findAll() {
    String findAll = "LONG QUERY";
    List<Entity> entities = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    entities.add(e);
    return entities;
  }

  public Entity find(int id) {
    String find = "LONG QUERY";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    return e;
  }
}

DAOComposed {
  private Connection c;
  DAOComposed {
     c = new FactoryDB.getConnection();

  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "LONG QUERY" + "INNER JOIN";
    List<Entity> composited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    composited.add();
    return composited;
  }

  public Composed find(int id) {
    String find = "LONG QUERY" + "INNER JOIN";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    return com;
  }

}

DAOOtherComposed {
  private Connection c;
  DAOOtherComposed {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "MONSTER QUERY" + "LONG QUERY" + "INNER JOIN";
    List<Entity> otherComposited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    OtherComposed oc = new OtherComposed(id, com);
    otherComposited.add(oc);
    return otherComposited;
  }

  public OtherComposed find(int id) {
    String find = "MONSTER QUERY" + "LONG QUERY" + "INNER JOIN";
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    Composed com = new Composed(id, e);
    OtherComposed oc = new OtherComposed(id, c);
    return oc;
  }

}

Well. This approach is good for performance but its so bad for maintance and debugging. I read books about lambdas and other things and I change the DAO's logics for the following approach:

  DAOEntity {
    private Connection c;
    DAOEntity {
     c = new FactoryDB.getConnection();
  }

  // cruds methods
  public List<Entity> findAll() {
    String findAll = "LONG QUERY";
    List<Entity> entities = new ArrayList();
    // dao's implements
    // make objects
    Entity e = new Entity(id, name);
    entities.add(e);
    return entities;
  }

  public Entity find(int id) {
    Entity e = findAll.stream.filter...;
    return e;
  }
}

DAOComposed {
  private Connection c;
  DAOComposed {
     c = new FactoryDB.getConnection();
     daoe = new DAOEntity();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "SHORT QUERY" + "INNER JOIN";
    List<Entity> composited = new ArrayList();
    // dao's implements
    // make objects
    Entity e = daoe.find(id);
    Composed com = new Composed(id, e);
    composited.add();
    return composited;
  }

  public Composed find(int id) {
    Composed com = findAll.stream.filter...;
    return com;
  }

}

DAOOtherComposed {
  private Connection c;
  private DAOComposed daoc;
  DAOOtherComposed {
     c = new FactoryDB.getConnection();
     daoc = new DAOComposed();
  }

  // cruds methods
  public List<Composed> findAll() {
    String findAll = "LONG QUERY" + "SHORT QUERY" + "INNER JOIN";
    List<Entity> othercomposited = new ArrayList();
    // dao's implements
    // make objects
    Composed com = daoc.find(id);
    OtherComposed oc = new OtherComposed(id, com);
    othercomposited.add();
    return composited;
  }

  public OtherComposed find(int id) {
    OtherComposed oc = findAll.stream.filter...;        
    return oc;
  }

}

This approach is more easy and more practical for me. But has a big trouble: For every new dao (DAO dao = new DAO) the response time increases for 1 second. In this case Entity has one second, composed two seconds and otherComposed 3 seconds (even more). This approach is bad, or is bad designed? There a better approach for the maintance and debugging? The first approach is fast but has copy/paste code many times, the second is more abstract but has this problem.

My DB is SQLite. Again, sorry for my English. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire