java - 存储库模式的优点和 Spring 实现

标签 java spring design-patterns repository dao

文章中Don’t use DAO, use Repository对于 DAO 和存储库模式之间的差异有一个很好的解释。

我的简短复述 - DAO 让我们用多种方法使接口(interface)变得臃肿,这阻碍了更改和测试。反过来,存储库使用 query 方法封装所有自定义/更改,该方法接受 Specification 作为参数。当您在存储库中需要新行为时 - 您不应该更改它,而是创建 Specification 的新继承人。

我的结论 - 存储库模式比 DAO 更好,因为它的接口(interface)无法修改。

到目前为止我是对的吗?我没有错过存储库模式的一些好处吗?

但是,如果你看看Spring's accessing data JPA guide ,您将找到下一个代码:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
    List<Customer> findByLastName(String lastName); //each time you need custom behavior you need to add a method
    //...
}

这和上一篇文章不冲突吗?为什么 Spring 的存储库强制我们向​​接口(interface)添加新方法?

最佳答案

My conslusion - repository pattern is better than DAO due its interfaces are closed to modification

这取决于...
因为存储库模式更复杂,因为它需要编写更多代码,并且对于存储库客户端及其实现而言,比 DAO 模式具有更高的抽象级别。
当您需要在查询中具有灵 active 和/或查询在结果中混合多个实体时,存储库模式可以满足这些需求。 如果您需要在表上进行主要简单的增删改查操作(基本的创建、读取、更新和删除操作),我认为使用真正的存储库(因此具有规范)可能是一种开销。

BUT, if you'd look at Spring's accessing data JPA guide, you'll find the next code:

 public interface CustomerRepository extends CrudRepository<Customer,Long> {
    List<Customer> findByLastName(String lastName); //each time you need custom behavior you need to add a method
     //... }

Doesn't it conflict with the previous article? Why Spring's repository force us to add new methods to interface?

是的。我认为问题来自于 Spring,它使用一个时尚术语(存储库)来代表一个类,该类不是根据模式文献的存储库。 (http://martinfowler.com/eaaCatalog/repository.html)
我认为您应该将 Spring Repository 视为 DAO,因为 Spring 存储库的基本功能接口(interface)是 CrudRepository。从本质上讲,CRUD 操作是我们在 DAO 中找到的操作...

之后,没有什么可以阻止您丰富存储库以提供具有 Spring 规范的存储库方法,如 Spring data 官方文档的 2.4 点所示。 .
Spring 建议您延长 org.springframework.data.repository.CrudRepository界面如您的示例所示。这是更简单的方法,我想这是最常见的方法......

public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
 …
}

JpaSpecificationExecutor提供使用规范的方法,从而促进减少查询源代码中的重复处理:

/*
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.jpa.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 * Interface to allow execution of {@link Specification}s based on the JPA criteria API.
 * 
 * @author Oliver Gierke
 */
public interface JpaSpecificationExecutor<T> {

    /**
     * Returns a single entity matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    T findOne(Specification<T> spec);

    /**
     * Returns all entities matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    List<T> findAll(Specification<T> spec);

    /**
     * Returns a {@link Page} of entities matching the given {@link Specification}.
     * 
     * @param spec
     * @param pageable
     * @return
     */
    Page<T> findAll(Specification<T> spec, Pageable pageable);

    /**
     * Returns all entities matching the given {@link Specification} and {@link Sort}.
     * 
     * @param spec
     * @param sort
     * @return
     */
    List<T> findAll(Specification<T> spec, Sort sort);

    /**
     * Returns the number of instances that the given {@link Specification} will return.
     * 
     * @param spec the {@link Specification} to count instances for
     * @return the number of instances
     */
    long count(Specification<T> spec);
}

但我认为它将创造出弗兰肯斯坦生物:一半是 DAO,一半是存储库。

另一个解决方案是直接实现基础和标记接口(interface):org.springframework.data.repository.RepositoryJpaSpecificationExecutor像这样的界面:

public interface CustomerRepository extends Repository<Customer, Long>, JpaSpecificationExecutor {
 …
}

通过这种方式,您可以拥有一个真正的存储库,仅包含规范方法。
我不知道它是否有效。我从来没有尝试过。

编辑:回复评论

存储库模式是 Hibernate 不提供开箱即用的解决方案的概念。
但 Hibernate 以及更普遍的 JPA 2 规范确实提供了 Criteria 作为将规范创建为类的基本成分。
然后,您可以创建一个自定义类,通过提出所需的方法并将规范作为输入来实现存储库模式。 我认为您可以使用 ORM 和存储库,因为您可以通过使用标准 API 将 ORM 与规范一起使用。 有些人反对 ORM 和 Repository,我不同意。 ORM 不是基于 DAO 模式。 DAO 是操作数据库中数据的方式。 ORM 是构建数据对象以表示数据库结构的方法。 例如,通过使用两者,您可以从规范的灵 active 以及关系对象映射和实体之间编织的强大功能中受益。
如果您不使用 ORM 或像 IBatis 那样的 ORM,您应该自己编写 ORM 为您提供的内容:对象关系映射。

关于java - 存储库模式的优点和 Spring 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068965/

相关文章:

linq-to-sql - 工作单元设计模式

java - 如何修复 “cannot find symbol error”?

java - 如何在GWT中的一组下添加复选框?

java - 高级 Spring @Qualifier

java - 使用具有一对多关系的 CriteriaBuilder 的 Spring Data JPA 规范

ruby - 在 Ruby 中使 NullObject 评估为 falsy

c# - 使用许多 if 和重复逻辑重构代码以从不同控件类中提取值的最佳方法是什么

java - 如何显示来自广播接收器的通知?

java - Sysout 结果序列

java - Spring:如何从另一个项目注入(inject)bean