Reputation: 6140
It seems to be very simple design question but I am curious of your opinions.
I have a DAO layer and one method returns both max and min value for something. So it returns two integer values but using as a return type int[]
with two elements int[0]=min_val
and int[1]=max_val
is not very clear for the method caller, because he has to exactly know which element is first and which is the second.
Should I use here some kind of DTO like this...
class RangeValuesDTO {
private int min_val;
private int max_val;
}
?
What is the right pattern in such simple situation?
Upvotes: 0
Views: 1562
Reputation: 2962
You should ask yourself why you would need a certain pattern. There is no such a thing as best practice when it comes to enterprise patterns. Wrapping data structures from the db in an object to keep your interfaces tight and stable, as @TechExchange suggests, is a valid concern. But as @Tom Anderson points out, this should be rather called Value Object, not DTO.
And keep in mind that the need for the DTO pattern has some historical reasons (see here) mainly in the pre-EJB-3-world. Those may not hold true anymore.
DTOs can help separating higher layers from accessing the data layer directly. In a simple CRUD application this can however add too much indirection and thus unnecessary duplication and complexity, thus more maintainance. You often end up having DTOs that look very similar to the entities that may not leave the data access layer.
DTOs can be especially helpful in distributed systems where they lead to more coarse grained interfaces and reduce network traffic.
Upvotes: 2
Reputation: 15768
Exactly, you should always wrap your data in meaningful and re-usable DataStructures.
Using DTO will help you in achieving that.
for example, in future if your DAO returns another int
or string
what will you do , If you use DTO the solution is simple to add that int
or string
to the DTO and not affecting other layers of the Application.
So +1 DTO
Upvotes: 1