Do @Annotations create a dependency relationship?

Do @Annotations create a dependency relationship?

No one doubts the effectiveness of annotations in Java. Since they were included in the language, their use has been widespread in various frameworks and libraries. Their usage is almost inevitable in large applications. Or will you tell me you can live without Lombok and JPA?

But I'm not questioning their importance here. The main point is the dependency relationship they create. If, indeed, they do create one.

When developing applications following best practices and certain architectures, like clean code and hexagonal architecture, we strive to ensure that business classes do not depend on the technology. After all, following the SRP principle, classes should only have one reason to change!

Frameworks are wonderful in a programmer’s life, but they are always trying to put their tentacles into our code, making us dependent on them forever! It is the job of the software designer to do everything to prevent this from happening. Here's the question: If you decide to change the framework, will your business modules remain intact? Or will you need to hit CTRL+F (if you know what I mean)?

But enough talk; let’s get to an example! Check out the code below of a class representing a model.

package com.example.accessingdatajpa;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Customer {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private String firstName;
  private String lastName;

  protected Customer() {}

  public Customer(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return String.format(
        "Customer[id=%d, firstName='%s', lastName='%s']",
        id, firstName, lastName);
  }

  public Long getId() {
    return id;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}        

This model is at the core of the business module. However, we can see several annotations related to JPA for persistence. But weren’t all these database-related things supposed to be hidden below a Repository or DAO implementation?

The purpose of this article is not to reach a conclusion but rather to open an honest discussion.

  • Is it worth the effort to keep your business classes “clean”?
  • And what can be done to eliminate such dependency?
  • Have you ever had refactoring issues due to these dependencies?

Please share your opinion in the comments.

#Java #CleanCode #SRP

Thiago Souza

Senior Software Engineer @ Start Consig | Java and Spring Boot Specialist

6 个月

Insightful discussion on the impact of annotations! It's crucial to balance convenience with clean architecture.

要查看或添加评论,请登录

Diego Fialho的更多文章

  • Git and GitFlow: How to Improve Your Repository Organization

    Git and GitFlow: How to Improve Your Repository Organization

    Nowadays, we can't live without a version control tool for software development, and this need becomes even more…

    1 条评论
  • The Paradox of Code Comments

    The Paradox of Code Comments

    We all know that comments are very important in any programming language. And who hasn’t come across code that was…

    1 条评论
  • What to Do When Faced with a Bug?

    What to Do When Faced with a Bug?

    If you’ve been in the development field long enough, you’ve likely encountered errors or strange software behavior…

    2 条评论
  • BDD: A Discovery example

    BDD: A Discovery example

    In our last article, we discussed tasks and activities in BDD. Now it's time to delve a little deeper into the first…

    4 条评论
  • BDD: Tasks and Activities

    BDD: Tasks and Activities

    It's a beautiful Monday, and your project leader, after spending a weekend at a software quality conference, comes to…

  • BDD: the missing link between requirements and code

    BDD: the missing link between requirements and code

    Well, at this moment you might be wondering if this gap between requirements and code isn't what we generally call…

社区洞察

其他会员也浏览了