使用JPA / Spring的(通用)DDD存储库的方法:它看起来不对吗? [英] Approach for a (generic) DDD Repository with JPA/Spring: does it look wrong?

查看:199
本文介绍了使用JPA / Spring的(通用)DDD存储库的方法:它看起来不对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是DDD和JPA的新手。

I'm pretty new to DDD and JPA.

我正在使用JPA和Spring开发通用存储库。我非常喜欢文章中公开的方法 DDD:通用存储库 JPA实施模式:数据访问对象。我的目标是使用JPA和Spring在域驱动设计中构建完美的存储库。

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

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

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?

谢谢。

编辑:


  • @Autowired on internalRepository - 感谢axtavt指出它。

  • read()可以改进

推荐答案

所有,它都行不通,因为Spring无法将 EntityManager 注入到用 new 创建的内部对象中。所以,你必须写这样的东西:

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;
    }
    ...
}

也是你的 read 方法看起来有点过于通用。它错过了一些重要的用例,例如 getSigleResult setFirstResult / setMaxResults

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

我个人更喜欢第二篇文章apporach,因为使用合成你最终会得到 EntityManager OrganizationRepository 中,以实现 IInternalGenericRepository 中遗漏的功能。

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.

这篇关于使用JPA / Spring的(通用)DDD存储库的方法:它看起来不对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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