DEV Community

Cover image for ORM and encapsulation demystifying
Bruno Ciccarino λ
Bruno Ciccarino λ

Posted on

1

ORM and encapsulation demystifying

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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay