Does it make sense to use on every spring boot service an interface? - spring

Which kind of design pattern speaks for and which against having an interface on every Service in Spring / SpringBoot?
Or does it only make sense to use Interfaces for services if at least you use two different implementations for a given interface.
What is the advantage to use an interface if you only have always one implementation in your code base.

I can answer your question with: it depends.
If you think about the SOLID principles, especially the Interface Seggregation, and its importance in object-oriented design:
Within object-oriented design, interfaces provide layers of abstraction that simplify code and create a barrier preventing coupling to dependencies.
The interfaces are using to expose the API contracts without exposing their implementation.
But, as I said before, it depends on the use case.
Let's say you're building a library/framework where it can expose multiple implementations for the same interface or allow the users to add their implementations. Then you surely should have interfaces.
On the other hand, it can also lead to some overengineering where your interface has only one implementation, you are sure it won't have more than one implementation, and it won't be used outside of your application. Then maybe you could skip it.
So, from my point of view, it depends.

Related

Why have coding over configuration at all?

When we talk about spring (which ever module say jdbc), one of the reasons we use it is because it enables dependency injection and controls lifecycle of beans/classes. In programming, one of the most important fundamental is to code for interfaces rather than implementations, so today if I am using sql server driver v1, I can change it to v2 tomorrow if my code is written in such a way that it cares about Driver interface and not the implementations, then in what case would I ever need coding over configuration ?
The wording of your question seems a bit strange to me. Perhaps you are asking if there are any drawbacks to using Spring-like dependency injection. I can think of a few drawbacks, but whether these drawbacks outweigh the potential benefits of Spring is a matter of opinion.
Unfortunately, a Spring XML file is much more verbose than code to achieve similar (but hard-coded) initialisation of objects.
A programmer has to look not just at code but also at a Spring XML file to figure out what is going on. This, arguably, is a form of the Yo-yo problem.
One significant benefit of Spring is that it can be used to instantiate and configure any Java class (assuming the classes provide getters and setters). In particular, Java classes do not need to be polluted with the need to inherit from framework infrastructure classes. If you don't mind polluting classes with the need to inherit from framework infrastructure, then it is possible to have much more concise configuration files for instantiating and configuring objects. A case study illustrating this idea can be found in Chapters 9, 10 and 11 of the Config4* Practical Usage Guide. I am not proposing that the approach used in that case study be used for all applications, but I think it is a good approach to use when there is a complex, standardised API (such as for JMS) that is implemented by multiple products. In the case study, the approach results in a significantly easier-to-use API and eliminates some potential bugs from applications. Spring doesn't offer such benefits.
Section 9.4.2 of the Config4* Practical Usage Guide outlines a 9-step initialisation process for typical JMS applications. The framework library discussed in the case study ensures that those 9 steps are carried out in the correct order. It has been years since I looked at Spring so I might be wrong, but I don't think Spring has the flexibility to (easily or perhaps at all) enforce such a complex 9-step initialisation mechanism.

Springboot project without service layer [duplicate]

This question already has answers here:
Why do we need service layer?
(5 answers)
Closed 2 years ago.
I am working on a project which has slightly different design. In all tutorials basically there are POJOs which are representing Models and all the business logic are implemented in service layers. In my project it is implemented in a way that there is not service layer and all the logic are implemented in Model classes by defining some static methods. Some says that it is because of Domain Driven Design but I am not really sure about it.
Putting all the logic inside of the POJOs looks messy for me. I would like to understand what is the benefit of this design and is if it is good software practice to implement business logic without service layer ?
It is definitely a bad idea to use a bunch of static methods, also adding logic in the DTOs is not recommended.
There is not a strict universally accepted rule about creating a service layer, I think what is very important is that the application design is modular and testable.
This is why the service layer pattern comes in handy:
business logic is confined in a layer where you can abstract the complexity into a set of functionality made available to the other layers
application logic is not scattered across packages and classes, simplify the evolution and make possible to refactoring for accommodating future changes
service classes can be tested independently (again critical for refactoring)

Using Laravel contracts in Application/Domain layers

I'm building application at top of the Laravel. In my Domain layer I have services. One of these needs to send an EMail. Laravel has Illuminate\Mail package for these purposes.
But Mailer contract depends on \Illuminate\Mail\PendingMail class.
https://github.com/laravel/framework/blob/5.7/src/Illuminate/Contracts/Mail/Mailer.php
Does it mean I need to write my own interface (port) for my Domain layer to fully decouple my application from framework?
To be honest, I'm not sure what you are asking. But I'll try to answer anway.
If you use (any) framework functionality directly in your code, you obviously couple your application to the framework. That's the whole point of a framework. It offers you a lot of functionalities that are commonly used throughout different types of applications. It can be seen as foundation for an applciation. These functionalities are often made available through interfaces, so that the implementation can be swapped out if necessary. Swapping out the implementation doesn't make your application less coupled to the framework though. It just allows you to use a different kind of implementation; the interface will still be a framework one.
So, the only way to prevent (heavy) coupling with a framework would be to write your own interfaces that abstract all the framework functionalities. You could then have proxy classes that implement these new interfaces and do nothing else than proxying requests to the proper functions of the framework, allowing you to write other proxy classes in future for a different framework.
If you ask me, this sounds like a bad idea though. Because in the end, you are building your own framework based on your own interfaces. So you are using additional custom code which adds no additional functionality, but comes with the added risk of bugs and errors.

Dependency injection vs Factory or Directory

I have been wondering if anyone uses DI/IoC in any non-toy project, where he can choose the implementation style (rather than this being enforced by libraries or development requirements).
After all, a Factory is just the perfect mechanism if we want a single instance of a class, and a Directory service is the perfect mechanism if we want a specific instance.
So what is the big deal with adding Spring/Guice/etc. into this and creating another dependency for ourselves? (Or maybe I completely fail to understand their DI approach...)
Thanks.
DI (Dependency Inversion) principle says nothing about frameworks and simply claims that:
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
Inversion of control (IoC) principle is also related to decreasing coupling among classes in your software.
There are many patterns that are used to keep your classes low coupled and follow DI or IoC principle. It is you responsibility how to meet this goal. Inversion of Control Containers and the Dependency Injection pattern are some of them.
After all, a Factory is just the perfect mechanism if we want a single instance of a class, and a Directory service is the perfect mechanism if we want a specific instance.
And Factory is considered as one of the implementation techniques of inversion of comtrol principle.
I have been wondering if anyone uses DI/IoC in any non-toy project, where he can choose the implementation style (rather than this being enforced by libraries or development requirements).
I see lots of enterprise applications where DI\IoC containers\frameworks are used. These frameworks provide an infrastructure that cares about object creation (like Factory), its lifetime (like Singleton), injecting dependencies, holding a list of all objects (like Registry)
So what is the big deal with adding Spring/Guice/etc. into this and creating another dependency for ourselves?
Your classes have no dependencies on any IoC container when you use it. IoC container is a part of your infrastucture layer only.
I think, the only one reason to avoid usage of IoC container is when you are restricted to use 3rd party libraries by company's rules and agreements.

What does mean Inversion of Control and Dependency Injection in Spring Framework? and what is difference ? Why in the Spring framework?

What does mean Inversion of Control and Dependency Injection in Spring Framework? and what is difference ? Why in the Spring framework ?
Can any one explain ?
Also suggest the some books to learn Spring framework for beginners ?
I shall write down my simple understanding of this two terms:
For quick understanding just read examples*
Dependency Injection(DI):
Dependency injection generally means passing a dependent object as a parameter to a method, rather than having the method create the dependent object. What it means in practice is that the method does not have a direct dependency on a particular implementation; any implementation that meets the requirements can be passed as a parameter.
With this objects tell thier dependencies.
And spring makes it available. This leads to loosely coupled application development.
Quick Example:EMPLOYEE OBJECT WHEN CREATED,IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT (if address is defines as dependency by Employee object).
Inversion of Control(IoC) Container:
This is common characteristic of frameworks,
IOC manages java objects – from instantiation to destruction through its BeanFactory. -Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, lifecycle events, and any AOP features for which it has been configured and coded.
QUICK EXAMPLE:Inversion of Control is about getting freedom, more flexibility, and less dependency. When you are using a desktop computer, you are slaved (or say, controlled). You have to sit before a screen and look at it. Using keyboard to type and using mouse to navigate. And a bad written software can slave you even more. If you replaced your desktop with a laptop, then you somewhat inverted control. You can easily take it and move around. So now you can control where you are with your computer, instead of computer controlling it.
By implementing Inversion of Control, a software/object consumer get more controls/options over the software/objects, instead of being controlled or having less options.
Inversion of control as a design guideline serves the following purposes:
There is a decoupling of the execution of a certain task from implementation.
Every module can focus on what it is designed for.
Modules make no assumptions about what other systems do but rely on their contracts.
Replacing modules has no side effect on other modules I will keep things abstract here, You can visit following links for detail understanding of the topic.
A good read with example
Detailed explanation

Resources