简体   繁体   English

如何将存储库和存储库实现划分为不同的模块?

[英]How to split Repository and Repository implementation to different modules?

folks! 乡亲们!

I have two maven modules in my application - domain and persistence. 我的应用程序中有两个Maven模块-域和持久性。

Domain has a domain object, services and "data provider" interfaces to external endpoints, for example, persistence. 域具有域对象,服务和到外部端点的“数据提供程序”接口,例如持久性。 Domain contains business logic and has no external dependencies - he know nothing about persistence. 域包含业务逻辑,没有外部依赖关系-他对持久性一无所知。

Persistence depends on domain. 持久性取决于域。 It implements "data provider" interfaces from domain module. 它从域模块实现“数据提供者”接口。 It may be relational db implementation, nosql implementation, file implementation etc. 它可能是关系数据库的实现,nosql的实现,文件的实现等。

For example, I have interface PersonRepository in domain, like this: 例如,我在域中具有接口PersonRepository ,如下所示:

public interface PersonRepository {
    List<Person> findAll();
    List<Customer> findByLastName(String lastName);
}

I want to implement data provider interface with Spring Data JPA. 我想用Spring Data JPA实现数据提供者接口。 I need to write something like that: 我需要写这样的东西:

public interface PersonRepository extends CrudRepository<Person, Long> {
    List<Person> findAll();
    List<Person> findByLastName(String lastName);
}

But I don't want to inject spring dependencies to "core domain". 但我不想将spring依赖项注入“核心域”。 I want to stay my domain very lightweight and independent. 我想保持我的域非常轻巧和独立。

Is there way to implement PersonRepository with Spring Data in Persistence module? 在持久性模块中是否可以使用Spring Data实现PersonRepository?

The Spring Data JPA interface can extend multiple interfaces. Spring Data JPA接口可以扩展多个接口。 Something like the following should work. 像下面这样的东西应该起作用。

Defined in your domain module: 在您的域模块中定义:

public interface PersonRepository {
    List<Person> findAll();
    List<Customer> findByLastName(String lastName);
}

In your persistence module: 在您的持久性模块中:

@Reposistory
public interface SpringDataJPAPersonRepository extends CrudRepository<Person, Long>, PersonRepository {
    // Any Spring Data JPA specific overrides e.g. @Query
}

Instead of extending the ready-made interfaces by Spring Data you can just copy the methods in your own interface. 无需通过Spring Data扩展现成的接口,您只需在自己的接口中复制方法即可。 Normally you would just put a @Repository annotation on it and Spring Data would do its job. 通常,您只需在其上放置@Repository批注,Spring Data即可完成其工作。 But that would reintroduce the dependency. 但这将重新引入依赖性。

So what you could do instead is in your Spring Configuration invoke the JpaRepositoryFactory.getRepository yourself. 因此,您可以做的是在Spring Configuration中自己调用JpaRepositoryFactory.getRepository Something like this should work: 这样的事情应该起作用:

@Bean
public PersonRepository(RepositoryFactorySupport factory) {
    return factory.getRepository(PersonRepository.class);
}

This will be either in your persistence module or in a third module which does the wiring of all your modules, so the dependencies shouldn't be an issue. 这将在您的持久性模块中,或者在第三个模块中进行所有模块的接线,因此依赖关系不应该成为问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM