Answer the question
In order to leave comments, you need to log in
What is/what is the difference between Repository and Dao interfaces?
Good day.
I looked through various repositories on github, and found several projects where both DAO interfaces for entities and Repositories for them were created, moreover, in most cases they contained the same methods (this is about interfaces). In the implementations, there was manipulation of dao implementations, and transactions were imposed.
Actually, why can't transactions be made in dao objects?
And when, what and why should you use it?
Links to repositories are welcome,
You can also recommend a couple of books :)
Answer the question
In order to leave comments, you need to log in
Here is a tutorial from oracle, everything is explained there. And here's another one , for example.
In general, there have always been disagreements and confusions with class names in Java. And often DAO is called something that should not be.
In essence, DAO is a rather low-level thing, it works directly with the repository. For each Transfer Object (entity) there must be a DAO implementation for a particular store.
For example, there are 2 entities: User and Post. There are different storages: 2 SQL databases (MySql and Postgres), file system, storage based on xml files.
The User object has a UserDao interface with CRUD methods. And there should be 4 implementations of this method: MySqlUserDao, PostgresUserDao, FileSystemStorageUserDao, XmlFileStorageUserDao. Similarly for the second entity. And usually a DAO factory is created, which will produce the desired implementation (there are schemes at the first link). Well, due to the similarity of implementations, DAO is usually made abstract and typed.
Thus, a unified interface for data manipulation is obtained. You can transparently change the repository simply by choosing a different DAO implementation (for example, through dependency injection or configs), without changing the business logic.
So usually DAO is created where there is no ready-made implementation of communication with some kind of storage (or this implementation does not suit). And the same transactions are a higher level, they can not be included in the DAO, so as not to complicate the architecture and solidity.
The repository is a more general and abstract thing.
In general, the name "repository" is usually found in the world of spring, in JavaEE there are other terms. Yes, and the suffix DAO in spring is used more often, although in meaning, it is not always the same DAO, in the interpretation of Sun / Oracle.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question