DEV Community

[Comment from a deleted post]
Collapse
 
jofisaes profile image
João Esperancinha • Edited

Hi there,

Thank you for your question.

All the business logic lives in the service. In this architecture type, that is where things like, calculations, parsing, transformations is made. Things like accessing databases or external parties, aren't made directly. This is where the concept of ports comes in. This is why the repository interface (a.k.a. port) is injected into the the service. This is to make sure that we can use repositories without any knowledge of its implementation. In other words, to be able to use them as a black box.

The adapters are the implementation of ports or in the Java world. We can say that ports are interfaces and adapters are implementations in java. They are not instantiated by the code of the REST application itself. Instead they are seamlessly instantiated by the library code. This is done by stereotyping our services, repositories and controllers by the use of the annotations @Service, @Repository and @RestController, respectively. If you look at the service constructor, you'll see just a normal constructor and this may lead to wonder where does the Repository comes from. That is where the stereotyping "magic" of Spring comes into action. Since we've stereotyped both the service and the repository, Spring then knows where and how to get them and we don't need to worry about it. Its important to note that by default and for this implementation, these are all singleton instances.

All of the concepts I've described above are part of IoC (Inversion of control) where very simply said it only means that we are giving Spring the control over the injection or creating the new instances. We don't specify that ourselves in the code. And that bit about the constructor is one example of DI (Dependency Injection). This is where we see that we do not specify the dependency implementation. This is passed through the constructor as a black box.

Please let me know if you have any more questions,

Cheers!

João

Collapse
 
flex301 profile image
Flex

Thank you!

It's a little bit clearer now.

I'll continue learning this architecture, since I just started.