DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Protecting Your Domain-Driven Design from Anemia
  • An Introduction to Object Mutation in JavaScript
  • Domain-Driven Design: Manage Data With Jakarta Data and JNoSQL
  • 10 Things to Avoid in Domain-Driven Design (DDD)

Trending

  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • Understanding and Mitigating IP Spoofing Attacks
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  1. DZone
  2. Data Engineering
  3. Data
  4. Understanding the Differences Between Repository and Data Access Object (DAO)

Understanding the Differences Between Repository and Data Access Object (DAO)

DAO focuses on database operations (insert, update, delete), while Repository aligns with business logic in DDD. Learn the differences with a Java example.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Oct. 24, 24 · Analysis
Likes (7)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

Repository and Data Access Object (DAO) play crucial roles in software development and data handling. However, their purposes and contexts differ, especially when we consider how they relate to the business logic of an application. Let’s explore the key differences between these concepts, where they originate, and when you should choose one.

About Repository and Data Access Object (DAO) Patterns

The Repository pattern originates from Domain-Driven Design (DDD), as described by Eric Evans in his book, "Domain-Driven Design: Tackling Complexity in the Heart of Software." Repositories are not just about managing data; they encapsulate business logic, ensuring that operations adhere to the Ubiquitous Language of the domain.

On the other hand, the Data Access Object (DAO) pattern comes from early enterprise application design, and its formalization can be found in the book "Core J2EE Patterns: Best Practices and Design Strategies" by Deepak Alur, John Crupi, and Dan Malks, published in 2001. DAO abstracts and encapsulates all access to a data source, separating persistence logic from business logic. This allows changes to the underlying data source without affecting the rest of the application, which is especially useful in enterprise contexts where systems might switch databases or data storage mechanisms over time.

The primary distinction between Repository and DAO lies in the semantics of the API they expose:

  • DAO: DAO is data-centric, providing database operations such as insert, update, delete, and find. These methods directly map to database actions, focusing on how data is stored and retrieved.
  • Repository: This is domain-driven and aligns with the* business logic*. It represents a collection of aggregate roots or entities. Instead of database operations, repositories offer operations that reflect the domain language. For instance, in a hotel system, a Repository might provide checkIn, checkout, or checkReservation, abstracting away the actual data operations.
Aspect DAO Repository
Focus Database-centric Domain-centric
Operation Naming Based on CRUD actions (insert, update) Reflects business logic (checkIn, reserve)
Abstraction Level Low-level, close to the database High-level, abstracted from database details
Origin Core J2EE Patterns book (2001) Domain-Driven Design (Eric Evans, 2003)
When to Use Simple applications or direct database access Complex domains with rich business logic
Implementation Context Can vary between databases Often maps to aggregate roots in DDD

Illustration

Let’s illustrate this difference with a simple example in Java. We’ll model a Room entity and treat the Guest as a Value Object:

Java
 
public class Room {

    private int number;
    private boolean available;
    private boolean cleaned;
    private Guest guest;

    // Getters and setters omitted for brevity
}

public record Guest(String name, String document) {
}


Now, let’s look at how the DAO and Repository would handle operations for this entity:

In the case of a DAO, the method terminology reflects database operations, like insert, update, or delete:

Java
 
public interface RoomDAO {

    void insert(Room room);

    void update(Room room);

    void delete(Room room);

    Optional<Room> findById(int number);
}


The Repository, on the other hand, defines operations that align with the business domain. The methods relate to actions that make sense in the context of managing a hotel:

Java
 
public interface Hotel {

    void checkIn(Room room);

    void checkOut(Room room);

    Optional<Room> checkReservation(int number);
}


In this sample code, consider the Hotel as a collection of Rooms. Check-in represents the process of assigning a guest to a room and updating its status accordingly, while checkout involves freeing the room and marking it as available once the guest departs.

It’s important to understand that this example focuses solely on the basic aspects of room management related to check-in and checkout. This simplified approach is intended to clarify the concept and provide a solid foundation for grasping more complex scenarios in the future. In a real-world situation, these processes would likely involve additional services, such as payment processing, which would expand the concept beyond a simple repository and into a service layer. However, to keep this example focused and easy to understand, the complexities of those integrations are beyond the scope of this article. This aims to reassure the reader.

  • Use DAO when your application primarily performs database operations without complex business logic. For example, if you’re working on a simple CRUD app or your data source is the focus, DAO fits naturally.
  • Use Repository when your application has complex domain logic that needs to be encapsulated. This is especially relevant when following Domain-Driven Design principles, where the Repository is an abstraction that ensures business operations are consistent with domain rules.

They understand the distinction between DAO and Repository when building scalable and maintainable applications. While both patterns handle data, DAOs are more focused on the persistence layer, and Repositories encapsulate business logic, abstracting away how the data is stored. Choosing the correct pattern for your scenario ensures that your application remains flexible, robust, and easy to maintain.

The sample Java code provided reveals that Repositories operate at the domain level, interacting with entities and business operations, while DAOs focus on the underlying data storage and retrieval. This conceptual difference is important as it shapes the structure and maintainability of your code in the long run.

Video


Data access object Domain-driven design Data (computing) Object (computer science) Repository (version control)

Opinions expressed by DZone contributors are their own.

Related

  • Protecting Your Domain-Driven Design from Anemia
  • An Introduction to Object Mutation in JavaScript
  • Domain-Driven Design: Manage Data With Jakarta Data and JNoSQL
  • 10 Things to Avoid in Domain-Driven Design (DDD)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!