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

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?

1

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.

1

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật