user24618785
user24618785

Reputation: 57

Repository and DAO together at Java program

I'm studying the Repository pattern and I am a bit cnfuse. I'd like to know if it is possible this pattern coexists with DAO pattern.

I have implemented a simple Java program that persists data in PostgreSQL using pure JDBC.

The program has:

A generic Repository

public interface Repository<T> {
    void save(T type);
    void update(T type);
    void delete(T type);
    Optional<T> findById(int id);
    List<T> findAll();
}

An interface that extends the generic Repository for Department domain model:

public interface DepartmentRepository extends Repository<Department> {
    Optional<T> findByName(String name);
}

An interface that extends the generic Repository for Employee domain model:

public interface EmployeeRepository extends Repository<Employee> {
    Optional<T> findByEmail(String email);
}

Therefore, I have the concrete classes that implements these repositories: DepartmentRepositoryImpl and EmployeeRepositoryImpl. The concrete classes call methods from DAOs to manage the data. My DAOs are:

public interface DAO <T>{
     int save(T type);
     void update(T type);
     void delete(T type);
     Optional<T> findById(int id);
     List<T> findAll();
}

public interface DepartmentDAO extends DAO<Department>{
     Optional<T> findByName(String name);
}

public interface EmployeeDAO extends DAO<Employee>{
     Optional<T> findByEmail(String email);
}

And the concrete classes DepartmentDAOImpl and EmployeeDAOImpl that uses pure JDBC to access the Postgres.

My question is: this structure makes sense? Can I have Repository and DAO together as presented here?

Please, I appreciate some guidance.

Thanks

Upvotes: 2

Views: 111

Answers (3)

user24618785
user24618785

Reputation: 57

I understand that it is ok using repository and dao together. Consider the scenario: the app deals with data come from network and from local database. It makes sense to use repository and dao together. The app does not need to know wherenthe data come from. The repository tries to access the online data. On success, it stores these data locally as a cache. If the network is unavailable, the repository can recover the data from lical database. Theregore, it can use DAO to access the DB.

Upvotes: 0

Naren D
Naren D

Reputation: 126

Yes, it is perfectly valid to use both the Repository and DAO patterns together

DAO (Data Access Object): Primarily focuses on the data persistence logic. It provides an interface for data operations, such as CRUD (Create, Read, Update, Delete), directly interacting with the database.

Repository: A Repository is a higher-level part of your code that manages groups of related data. It focuses on the business rules and makes it easier to work with those data groups without worrying about how they are stored.

Clarity: You keep the data access logic (DAO) separate from business logic (Repository), which makes your code cleaner and easier to maintain.

Flexibility: If you decide to change how data is accessed (later switching from JDBC to an ORM like Hibernate), you just need to modify DAO layer without affecting the Repository layer.

Testability: This structure enhances testability. You can easily mock DAOs when testing your Repositories, allowing for unit tests that don't rely on a database.

Upvotes: 1

Francesco Poli
Francesco Poli

Reputation: 1274

Repository and DAO Patterns may cohesist, and as you already did, Repository istances will use DAO implementations for CRUD DB operations.

However, "it depends", as usual; I suggest you to read some articles on the web to make your own opionion on this usage, relying on the complexity of your domain and application.

A first starting point can be this https://www.baeldung.com/java-dao-vs-repository and this What is the difference between DAO and Repository patterns?

Upvotes: 1

Related Questions