使用 JPA/Spring 的(通用)DDD 存储库的方法:它看起来有问题吗?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,它不起作用,因为 Spring 无法将
EntityManager
注入到使用new
创建的内部对象中。所以,你必须写这样的东西:另外你的 read 方法看起来有点太通用了。它错过了一些重要的用例,例如
getSigleResult
和setFirstResult
/setMaxResults
。就我个人而言,我更喜欢第二篇文章的方法,因为使用组合,您最终会在
OrganizationRepository
中拥有EntityManager
,以便实现IInternalGenericRepository
中缺少的功能。First of all, it wouldn't work, because Spring can't inject
EntityManager
into internal object created withnew
. So, you have to write something like this:Also your
read
method looks a bit too generic. It misses some important use cases, such asgetSigleResult
andsetFirstResult
/setMaxResults
.Personally I prefer the second article apporach, because using composition you'll end up with having
EntityManager
inOrganizationRepository
in order to implement features missed inIInternalGenericRepository
.