Layered architecture and persistence annotations on the model beans?

给你一囗甜甜゛ 提交于 2019-12-06 06:51:34

问题


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!


回答1:


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");


来源:https://stackoverflow.com/questions/20074837/layered-architecture-and-persistence-annotations-on-the-model-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!