Question

I would like to follow the separation of concerns design principle in a new Java EE web app. If I understand correctly, this means that I have to keep the technical choices of my DAL (Data Access Layer) invisible from my model/business layer.

As I use Spring Data Neo4j, I must annotate my model beans with e.g. "@NodeEntity", an annotation which is specific to Spring Data Neo4J. This seems to mix the model layer with the Data Access Layer.

  • Is this a good analysis I'm doing here?
  • If so, how can I have a model which is independant of my DAL using Spring Data Neo4j annotations?

Thanks for your help!

Was it helpful?

Solution

A common solution is to apply the programming by interfaces principle, creating interfaces for each entity and relationship and implement them with SDN annotated classes. This way the model layer would only access entities by interfaces, having no knowledge about implementation. To astract database access operations, you can create DAO interfaces and implement them with SDN repositories and/or Cypher queries. An example:

public interface Item {
    String getName();
    ...
}

public interface ItemDAO {
    Item lookup(String name);
    ...
}

@NodeEntity
public class ItemNode implements Item {
    @GraphId private Long id;
    private String name;
    ...
    public String getName() { return name; }
    ...
}

public class Neo4jItemDAO implements ItemDAO {
    ...
    @Override
    public Item lookup(String name) {
        return neo4jOperations.lookup(ItemNode.class,"name", name).to(ItemNode.class).singleOrNull();
    }
}

In your model class you can access entities this way:

@Autowired ItemDAO itemDAO;
Item item = itemDAO.lookup("name");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top