2

As per this Wikipedia article: Implementing Dependency Inversion Principle can be done in two ways:

  • Having an abstraction of a low level component in a separate package upon which both high level and low level components depend.
  • Having the abstraction of the low level component reside in the same package of the high level component.

The following illustration depicts the dependencies before and after DIP using the two approaches:

Before DIP: The repository resides in a separate maven module and has no interface, and the service has a direct dependency on the Repository implementation. Dependencies before DIP

Approach 1: An interface (abstraction) of the repository is introduced. The implementation of that interface is another module, and both the service and the repository implementation have direct dependency on the interface. DIP Implementation Approach 1

Approach 2: In this approach, the interface resides in the same package of the service. The diagram used by Fowler to describe Separated Interface pattern seems to be also an example of this approach. DIP Implementation Approach 1

I've been following Approach1 and because I used spring's JavaConfig, the services module had to have a ,maven dependency to both the infrastructure interfaces and implementation modules. Apart from my @Configuration file, there's absolutely no reference to any infrastructure concrete implementation.

I'm currently considering switching to Approach2, but obviously it won't work with JavaConfig as I will end up having direct references in the code to the interface implementation module resulting in a cyclic dependency, something build tools like maven can't deal with.

The question is how can configure spring and maven to achieve Approach2? Is there a way I can ask spring to scan for components that are not added as a maven dependency? Will that require changes in the way I'm using maven?

5
  • it won't work with JavaConfig as I will end up having direct references in the code to the interface implementation module resulting in a cyclic dependency: only if the JavaConfig is in the "Services" module, that is supposed to contain service and repository interfaces. But why would it be there, and not in the module containing the concrete implementation classes, or in an external module depending on both?
    – JB Nizet
    Commented Nov 2, 2015 at 6:58
  • @JBNizet where else would the JavaConfig be? Even if I move it to the "Infrastructure Impl" module, I will need to import that configuration in my service module which will cause the same cyclic dependency. Commented Nov 2, 2015 at 7:20
  • You can use Spring Boot specifically to manage this scenario; it uses SPI discovery. Commented Nov 2, 2015 at 9:35
  • I don't understand why you would need to "import configuration" in your service module. Isn't it supposed to contain service and repository interfaces only? That should be completely independant of Spring.
    – JB Nizet
    Commented Nov 2, 2015 at 17:34
  • @JBNizet The reason I reference implementations in a JavaConfig is that although as I mentioned previously, I don't use the infrastructure implementations anywhere in my code neither does spring inject them when running my unit tests (it injects doubles instead), yet, I would like my integration server to run these tests using the infrastructure implementations. Makes sense? Commented Nov 8, 2015 at 14:32

1 Answer 1

0

I had the same query. A very late reply, but this is what I came up with for an XYZ project (flattened maven style):

xyz-service1
    src/main/java/com/example/xyz/service1/Service1.java
xyz-service1-repository1
    src/main/java/com/example/xyz/service1/repository1/Repository1.java (interface)
xyz-file-repository
    src/main/java/com/example/xyz/file-repository/FileRepository.java
    src/main/java/com/example/xyz/file-repository/Repository1Adapter.java
xyz
    src/main/java/com/example/xyz/XyzJavaConfig.java
    src/main/assembly/xyz.bin

Dependency tree:

xyz
    xyz-service1
        xyz-service1-repository1
    xyz-file-repository
        xyz-service1-repository1

Not the answer you're looking for? Browse other questions tagged or ask your own question.