使用 JPA/Spring 的(通用)DDD 存储库的方法:它看起来有问题吗?

发布于 2024-08-21 14:34:20 字数 2267 浏览 5 评论 0原文

我对 DDD 和 JPA 还很陌生。

我正在使用 JPA 和 Spring 开发一个通用存储库。我真的很喜欢文章 DDD:通用存储库JPA 实现模式:数据访问对象。我的目标是使用 JPA 和 Spring 在领域驱动设计中构建完美存储库。

遵循第一篇文章的概念,我使用内部通用存储库来尊重域与数据存储的合同

public interface IInternalGenericRepository<K, E> {
    List<E> read(String query, Object[] params);
    void persist(E entity);
    void remove(E entity);
}

public class InternalGenericRepository<K, E> implements IInternalGenericRepository<K, E> {

    // Injected through DI in Spring
    @PersistenceContext
    private EntityManager em;

    private final Class<E> entityClass;

    public List<E> read(String query, Object[] params) {
        Query q = em.createQuery(query);
        for (int i = 0; i < params.length; i++) {
            q.setParameter(i + 1, params[i]);
        }
        return q.getResultList();
    }

    public void persist(E entity) {
        em.persist(entity);
    }

    // ...
}

然后,特定实体(例如:组织)的存储库如下所示。

public interface IOrganizationRepository {
    List<Organization> organizationByCityName(String city);

    void create(Organization o);
}


@Repository
public class OrganizationRepository implements IOrganizationRepository {

    @Autowired
    IInternalGenericRepository<Long, Organization> internalRepository;

    public List<Organization> organizationByCityName(String city) {
        Object[] params = new Object[1];
        params[0] = city;
        return internalRepository.read("select o from Organization o where o.city.name like ?1",
                params);
    }

    @Override
    public void create(Organization o) {
        internalRepository.persist(o);
    }
}

它看起来是使用 JPA 和 Spring 实现 DDD 存储库的好方法。然后将 OrganizationRepository 注入到我的服务层中。

我希望获得外部观点,以避免缺陷或误解。您认为如何?如何改进?

谢谢。


编辑:

  • internalRepository 上的 @Autowired - 感谢 axtavt 指出这一点。
  • read() 可以改进

I'm pretty new to DDD and JPA.

I'm working on a generic Repository with JPA and Spring. I really like the approaches exposed in the articles DDD: The Generic Repository and JPA implementation patterns: Data Access Objects. My aim is to build the perfect Repository in Domain-Driven Design with JPA and Spring.

I use an internal generic Repository to respect the domain’s contract with the data store, following the first article concepts.

public interface IInternalGenericRepository<K, E> {
    List<E> read(String query, Object[] params);
    void persist(E entity);
    void remove(E entity);
}

public class InternalGenericRepository<K, E> implements IInternalGenericRepository<K, E> {

    // Injected through DI in Spring
    @PersistenceContext
    private EntityManager em;

    private final Class<E> entityClass;

    public List<E> read(String query, Object[] params) {
        Query q = em.createQuery(query);
        for (int i = 0; i < params.length; i++) {
            q.setParameter(i + 1, params[i]);
        }
        return q.getResultList();
    }

    public void persist(E entity) {
        em.persist(entity);
    }

    // ...
}

Then, a Repository for a particular entity (for example: Organization) looks like this.

public interface IOrganizationRepository {
    List<Organization> organizationByCityName(String city);

    void create(Organization o);
}


@Repository
public class OrganizationRepository implements IOrganizationRepository {

    @Autowired
    IInternalGenericRepository<Long, Organization> internalRepository;

    public List<Organization> organizationByCityName(String city) {
        Object[] params = new Object[1];
        params[0] = city;
        return internalRepository.read("select o from Organization o where o.city.name like ?1",
                params);
    }

    @Override
    public void create(Organization o) {
        internalRepository.persist(o);
    }
}

It looks like a good way to implement DDD Repository with JPA and Spring. The OrganizationRepository is then injected in my services layer.

I'd like to have external views to avoid flaws or misconception. What do you think and how could it be improved?

Thanks.


Edit:

  • @Autowired on internalRepository - thanks to axtavt for pointing it out.
  • read() can be improved

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-08-28 14:34:20

首先,它不起作用,因为 Spring 无法将 EntityManager 注入到使用 new 创建的内部对象中。所以,你必须写这样的东西:

public class OrganizationRepository implements IOrganizationRepository { 

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        internalRepository.em = em;
    }
    ...
}

另外你的 read 方法看起来有点太通用了。它错过了一些重要的用例,例如 getSigleResultsetFirstResult/setMaxResults

就我个人而言,我更喜欢第二篇文章的方法,因为使用组合,您最终会在 OrganizationRepository 中拥有 EntityManager,以便实现 IInternalGenericRepository 中缺少的功能。

First of all, it wouldn't work, because Spring can't inject EntityManager into internal object created with new. So, you have to write something like this:

public class OrganizationRepository implements IOrganizationRepository { 

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        internalRepository.em = em;
    }
    ...
}

Also your read method looks a bit too generic. It misses some important use cases, such as getSigleResult and setFirstResult/setMaxResults.

Personally I prefer the second article apporach, because using composition you'll end up with having EntityManager in OrganizationRepository in order to implement features missed in IInternalGenericRepository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文