Java EE的建筑 - 是DAO的使用JPA一样的2时ORM仍建议? [英] Java EE Architecture - Are DAO's still recommended when using an ORM like JPA 2?

查看:140
本文介绍了Java EE的建筑 - 是DAO的使用JPA一样的2时ORM仍建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用像JPA2的ORM - 在那里我有我的实体映射到我的数据库,我应该还是使用DAO?这似乎是一个很多的开销。

例如,我需要保持三个额外的软件包:


  1. 之一,指定我的域对象(pretty多少映射我的实体对象):

     公共类Employee {
        私有String的firstName;
        私人字符串的lastName;
        ...
        // getter和setter方法
    }


  2. 一个包含指定吾道方法接口

     公共接口EmployeeDAO {
        公共无效addEmployee(员工员工);
        公务员getEmployeeById(长ID);
        ...
    }


  3. 一个包含实现我的DAO的。

    会话bean

     公共EmployeeJpaDAO实现EmployeeDAO {
        这里的接口方法实现
        ....
        那个改变我Employee实体进入我的员工的域对象的私有方法
    }


现在这是一个很大微胖加我每次需要执行一个新的CRUD操作时间。

不过,我从有一个DAO看到它的好处是:


  1. 您可以在内存中执行的DAO有一个单元测试你的服务层。这意味着你不需要访问数据库来测试业务逻辑,你可以放心,你的对象总是包含的属性相同的值


  2. 它的业务逻辑从数据库访问逻辑分离


这不涉及实施DAO是只用在服务层实体对象和EntityManager的选项:

  @Stateless
公共类EmployeeEjb {
    @PersistenceContext(=的unitName雇员)
    私人EntityManager的经理;    公务员getEmployeeById(长ID){
        返回manager.createNamedQuery(Employee.GetEmployeeById).getResultList();
    }
    ...
}

有没有中间地带吗?有没有人碰到一个架构或实施符合一定的DAO层,我上面提到的,但不涉及所有参与来实现DAO层的开销(业务逻辑最重要的是可测性单位)的优点的架构?

感谢您的任何建议和/或建议!我真的很好奇,想看看有些人想出问候了这一点。


解决方案

  

如果我使用像JPA2的ORM - 在那里我有我的实体映射到我的数据库,我应该还是使用DAO?这似乎是一个很多的开销。


有。并明确,Java EE的不鼓励使用 DAO模式的(JPA已经提供了标准化的实施店模式并没有在屏蔽它背后DAO)多大的价值。我觉得DAO是防干烧在这样的情况。

因此​​,对于简单的情况下(实际上,大多数情况下),我高兴地跳过DAO和我有没有问题。欲了解更多的复杂情况下(例如使用存储过程,平面文件的时候),我会使用它。换句话说,这取决于,总结 JPA已经杀了DAO?。另请参见下面的相关的问题:

相关问题


  包含执行我的DAO的

会话Bean

(...)一

NOOOO,你肯定不希望实现一个DAO会话bean:


  • 您不想创造尽可能多的(池)会话Bean作为表(资源的巨大浪费)

  • 您不想链会话Bean无处不在,不从过去的错误重现,这是一个已知的不良做法不能很好地扩展。

所以,如果你真的想要去的DAO方式希望EM被注入,要么实施(Java EE 5中),或者CDI你的DAO作为春豆管理的bean(Java EE 6中)。


  

您可以有一个在内存中执行的DAO进行单元测试你的服务层。


如果你真的想这样做的单位测试,模拟的DAO / EntityManager的,没有任何区别。如果你想要做集成测试,您可以配置JPA内存数据库来使用。所以最终,我就是不买这个说法。


  

从数据库访问逻辑业务逻辑分离


老实说,我没有看到一个依托DAO VS实体管理器之间的区别,我看不出一个DAO两码事更好。同样,我不买这个说法。

和我的经验,改变底层的持久性解决方案是一个非常特殊的事件,我不打算引进的DAO的东西,这是非常有可能不会发生(的 YAGNI KISS )。


  

有没有中间地带吗?有没有人碰到一个架构或实施符合一定的DAO层,我上面提到的,但不涉及所有参与来实现DAO层的开销(业务逻辑最重要的是可测性单位)的优点的架构?


我看不到太多的中间立场,正如强烈暗示,我不使用的DAO如果我不觉得有必要。正如我所说,嘲笑的EntityManager 如果你想真正地进行单元测试的业务逻辑。这对我的作品,我很高兴能少写code。

更多资源

If I'm using an ORM like JPA2 - where I have my entities that are mapped to my database, should I still be using a DAO? It seems like a lot more overhead.

For example, I would need to maintain three extra packages:

  1. One that specifies my domain objects (which pretty much map my Entity objects):

    public class Employee {
        private String firstName;
        private String lastName;
        ...
        // Getters and setters
    }
    

  2. One that contains interfaces that specify my DAO methods

    public interface EmployeeDAO {
        public void addEmployee(Employee employee);
        public Employee getEmployeeById(long id);
        ...
    }
    

  3. One that contains session beans that implement my DAO's

    public EmployeeJpaDAO implements EmployeeDAO {
        interface method implementations here
        ....
        private method that transform my Employee entity into my Employee domain object
    }
    

Now that's a lot of extra baggage to add every time I need to perform a new CRUD operation.

However the benefits I see from having a DAO is:

  1. You can have an in memory implementation of the DAO for unit testing your service layer. This means you don't need to access the database to test business logic, and you can be assured that your objects will always contain the same values for properties

  2. It separates business logic from database access logic

The option that doesn't involve implementing a DAO is to just use entity objects and EntityManager in the service layer:

@Stateless
public class EmployeeEjb {
    @PersistenceContext(unitName = "employee")
    private EntityManager manager;

    public Employee getEmployeeById(long id) {
        return manager.createNamedQuery(Employee.GetEmployeeById).getResultList();
    }
    ...
}

Is there no middle ground here? Has anyone come across an architecture or implemented an architecture that meets some of the benefits of a DAO layer (most importantly the unit testability of business logic) that I mentioned above, but doesn't involve all the overhead involved to implement a DAO layer?

Thanks for any recommendations and/or suggestions! I'm really curious to see what some people have come up with in regards to this.

解决方案

If I'm using an ORM like JPA2 - where I have my entities that are mapped to my database, should I still be using a DAO? It seems like a lot more overhead.

It is. And clearly, Java EE doesn't encourage using the DAO pattern when using JPA (JPA already provides a standardized implementation of the Domain Store pattern and there isn't much value at shielding it behind a DAO). I find the DAO to be anti-DRY in such situation.

So for simple cases (actually, most cases), I happily skip the DAO and I have no problem with that. For more complex cases (for example when using stored procedures, flat files), I'd use it. In other words, it depends, as summarized in Has JPA Killed the DAO?. See also the related questions below:

Related questions

(...) One that contains session beans that implement my DAO's

Noooo, you certainly don't want to implement a DAO as a Session Bean:

  • You don't want to create as much (pooled) Session Bean as tables (big waste of resources)
  • You don't want to chain Session Beans everywhere, don't reproduce errors from the past, this is a known bad practice that doesn't scale well.

So if you really want to go the DAO way and want the EM to be injected, either implement your DAOs as Spring beans (in Java EE 5) or CDI managed bean (in Java EE 6).

You can have an in memory implementation of the DAO for unit testing your service layer.

If you really want to do unit testing, mock the DAO/EntityManager, there is no difference. And if you want to do integration testing, you can configure JPA to use an in memory database. So at the end, I just don't buy this argument.

It separates business logic from database access logic

Honestly, I don't see a big difference between relying on a DAO vs an entity manager, I don't see how a DAO separate things "better". Again, I don't buy this argument.

And to my experience, changing the underlying persistence solution is a very exceptional event and I'm not going to introduce DAOs for something that is very likely not going to happen (YAGNI, KISS).

Is there no middle ground here? Has anyone come across an architecture or implemented an architecture that meets some of the benefits of a DAO layer (most importantly the unit testability of business logic) that I mentioned above, but doesn't involve all the overhead involved to implement a DAO layer?

I don't see much middle ground and, as strongly hinted, I don't use DAOs if I don't feel the need. And as I said, mock the EntityManager if you want to truly unit test the business logic. It works for me and I'm happy to write less code.

More resources

这篇关于Java EE的建筑 - 是DAO的使用JPA一样的2时ORM仍建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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