模型bean上的分层架构和持久性注释? [英] Layered architecture and persistence annotations on the model beans?

查看:110
本文介绍了模型bean上的分层架构和持久性注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新的Java EE网络应用中遵循关注点分离设计原则。如果我理解正确,这意味着我必须保留我的 DAL 的技术选择(数据访问)从我的模型/业务层看不到层。

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.

当我使用Spring Data Neo4j时,我必须用例如注释我的模型bean @NodeEntity,一个特定于Spring Data Neo4J的注释。这似乎将模型层与数据访问层混合在一起。

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.


  • 这是一个很好的分析,我在这里做什么?

  • 如果是这样,我如何使用Spring Data Neo4j注释得到一个独立于我的DAL的模型?

感谢您的帮助!

推荐答案

一个常见的解决方案是通过接口原理应用编程,为每个实体和关系创建接口并使用SDN注释类实现它们。
这样,模型层只能通过接口访问实体,而不了解实现。
要实现数据库访问操作,您可以创建DAO接口并使用SDN存储库和/或Cypher查询实现它们。
一个例子:

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

这篇关于模型bean上的分层架构和持久性注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆