It's more clear now after your comment. Let's try again:
First, some clarifications:
Your RestaurantOwnerRepository
implements the repository pattern. Your @Entity
annotated objects are hibernate entities and also DAO proxies. Your RestaurantOwnerService
is a GWT-Service which can only return a DTO shared with the client and server.
So in a very simple server-side setup, you have a DB-Backend, access to the data via hibernate as a persistence layer, and a service layer as rest-service. In such a setup your hibernate entities are shared among the whole server side code. Your service layer is converting the entities to json format, for example. Deal?
Your "advanced" setup
- Persistence layer
- with Hibernate (delivering @Entity-Annotated objects)
- maybe other stuff, too
- Repository Layer (unclear for you what to return)
- Service Layer (GWT Servlets, delivering DTOs which are shared with the client side)
Definition of Repository-Layer: In my opinion, it's an abstraction for different data/persistence layers. It doesn't provide business logic, which is more the purpose of a further business layer. The business layer compiles the outputs of the upper layer together, makes computations and returns the results. But looking according to your comment, this may also be the case in your repository layer. But it's ok for our clarification.
Your question: Is it okay to return DTO objects from the Repository Layer?
Answer: No, it is not really okay to return a DTO from the "repository" layer.
Why: 1. Your DTO is a domain entity transferred into a format which can be sent to the client side. It has limitations so that some server side libraries cannot be used in them. 2. Consider the case that you also want to provide other service layers. A REST-Interface maybe, another GUI-Framework maybe. They all have their own limitations for transferring the domain entities. Do you really want to duplicate the repository layer for each service layer? 3. Consider the case where you want to extend your repository/business layer so that it will use the output of your RestaurantOwnerRepository
. Do you really want to work on DTOs there?
These are why the creation of a DTO is the purpose of a service layer. So the DTO is shared among the client side, and your service layer. In the same sense, you need objects, shared among the service layer and your repository layer. I call these domain entities. These are returned from the repository layer and used by the service layer. Again the same between the repository layer and persistence layer. The persistence layer for example returns the Hibernate entities which are used on the repository layer.
In most cases, it is ok to propagate your objects from multiple layers downwards. So you can return your hibernate entites from the repository layer to the service layer. Newer versions of GWT even allow to use JPA-entities on the client side with a special setup. So your service layer can further return your persistence entities.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…