ORM and encapsulation demystifying

Bruno Ciccarino λ - Oct 3 - - Dev Community

Hello everyone, Bruno Ciccarino

In today's text I will talk a little about encapsulation and orm, I will try to explain to you how orm can compromise encapsulation and I will mitigate these problems.

To begin we first need to know what an orm is, what is ORM?

Image description

ORM or Object-Relational Mappers is an object-relational mapping technique that allows you to create a relationship between objects and the data they represent. It has been used a lot lately and has been growing a lot in recent years.

This growth is due to the fact that developers don't feel very comfortable writing SQL code by hand like they used to, whether this feeling is valid or not I don't know. The point is, productivity increases a lot using tools that perform this technique, things like a simple CRUD become ridiculously easier to do using a tool like this.

Encapsulation in Practice:

Image description

• Visibility: Encapsulation controls the visibility of a class's members (attributes and methods). Through access modifiers (public, private, protected), we define who can access and modify these members.
• Invariants: Encapsulation ensures that objects maintain their consistent internal state, that is, that the class invariants are always respected.
• Abstraction: Encapsulation allows classes to expose a well-defined interface, hiding the complexity of the internal implementation.

How ORMs Can Affect Encapsulation:

• Direct Mapping:

  • Attribute exposure: By directly mapping the attributes of an entity to columns of a table, the ORM exposes these attributes to the outside world, allowing any part of the code to access and modify them.
  • Violation of invariants: Lack of control over how attributes are modified can lead to violation of the class's business rules.

• Coupling:

  • ORM dependency: The domain class is directly coupled to the ORM, making it difficult to reuse in other contexts.
  • Difficulty in carrying out tests: The need for a database to carry out unit tests makes it difficult to create isolated tests.

Mitigating the Risks of ORM on Encapsulation
To address these challenges, it’s crucial to implement strategies that reinforce encapsulation while leveraging the benefits of ORM. Here are some approaches:

  • Data Transfer Objects (DTOs): Use DTOs to decouple the domain model from the database schema. This allows you to control how data is exposed and modified, reducing direct access to entity attributes.

  • Service Layer: Introduce a service layer that mediates between the ORM and your business logic. This layer can enforce invariants and business rules, ensuring that any modifications to entities happen through controlled methods.

  • Custom Repositories: Instead of exposing the ORM directly, create repository classes that abstract the data access logic. This way, you maintain a clean separation between your domain model and data access concerns.

  • Encapsulation Best Practices: Continue to use access modifiers wisely, ensuring that sensitive attributes remain private and that any necessary access is provided through well-defined methods.

By thoughtfully integrating these practices, developers can harness the efficiency of ORMs without compromising the core principles of encapsulation. This balance not only enhances code maintainability but also ensures that your applications remain robust and resilient to changes.

Thank you for reading, and I hope this discussion helps you navigate the intricacies of using ORMs effectively!

. . . . . . . .
Terabox Video Player