Wrap Business Object to DTO with additional fields [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
My REST service has 3 layers:
- Persistence Layer - database entities live here
- Business Logic Layer - business objects live here
- Access Layer - data transfer objects live here
The flow for reading a single entity (/entity/id
) is:
Database Entity Object --> Business Object --> DTO
The trick is that the DTO
should contain additional field compared to the Business Object
or the Database Entity Object
.
To be more exact, I have an entity User
. The data model across layers is:
UserTable --> UserBO --> UserDTO
Every user has a profile photo, and the DTO should contain the URL of the profile photo, likewise:
"firstName" : "David",
"lastName" : "Beckham",
"profilePhotoURL" : "amazon.s3.com/123"
Now the thing is that I don't save the URLs to the database, because they have an expiry date. Instead I just fetch them from Amazon S3
on the fly. This means that the UserTable
and UserBO
will not have the field profilePhotoURL
, but UserDTO
will have it.
The Question
What is the good way to convert the UserBO
to UserDTO
, but to still give it the additional field (profilePhotoURL
)?
The URL is fetched from the service like String url = urlService.getUrlForUser(userId);
I was thinking to design my code to something similar to this:
public UserDTO getUser(long userId)
return dbService
.fetchById(userId) // UserBO at this point
.toDto() // UserDTO at this point, but missing URL
.withUrl(urlService.getUrlForUser(userId)); // concat the URL to the DTO
The downside of this approach is that the client code can "forget" to call the withUrl(...)
which will result with the DTO lacking the photo URL. Another downside would be if additional fields are required in time, then I would have more and more withX()
, withY()
methods, making the client code harder to maintain.
Any idea on how to make this design better?
java object-oriented design-patterns spring
closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela Jun 22 at 16:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela
add a comment |Â
up vote
2
down vote
favorite
My REST service has 3 layers:
- Persistence Layer - database entities live here
- Business Logic Layer - business objects live here
- Access Layer - data transfer objects live here
The flow for reading a single entity (/entity/id
) is:
Database Entity Object --> Business Object --> DTO
The trick is that the DTO
should contain additional field compared to the Business Object
or the Database Entity Object
.
To be more exact, I have an entity User
. The data model across layers is:
UserTable --> UserBO --> UserDTO
Every user has a profile photo, and the DTO should contain the URL of the profile photo, likewise:
"firstName" : "David",
"lastName" : "Beckham",
"profilePhotoURL" : "amazon.s3.com/123"
Now the thing is that I don't save the URLs to the database, because they have an expiry date. Instead I just fetch them from Amazon S3
on the fly. This means that the UserTable
and UserBO
will not have the field profilePhotoURL
, but UserDTO
will have it.
The Question
What is the good way to convert the UserBO
to UserDTO
, but to still give it the additional field (profilePhotoURL
)?
The URL is fetched from the service like String url = urlService.getUrlForUser(userId);
I was thinking to design my code to something similar to this:
public UserDTO getUser(long userId)
return dbService
.fetchById(userId) // UserBO at this point
.toDto() // UserDTO at this point, but missing URL
.withUrl(urlService.getUrlForUser(userId)); // concat the URL to the DTO
The downside of this approach is that the client code can "forget" to call the withUrl(...)
which will result with the DTO lacking the photo URL. Another downside would be if additional fields are required in time, then I would have more and more withX()
, withY()
methods, making the client code harder to maintain.
Any idea on how to make this design better?
java object-oriented design-patterns spring
closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela Jun 22 at 16:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My REST service has 3 layers:
- Persistence Layer - database entities live here
- Business Logic Layer - business objects live here
- Access Layer - data transfer objects live here
The flow for reading a single entity (/entity/id
) is:
Database Entity Object --> Business Object --> DTO
The trick is that the DTO
should contain additional field compared to the Business Object
or the Database Entity Object
.
To be more exact, I have an entity User
. The data model across layers is:
UserTable --> UserBO --> UserDTO
Every user has a profile photo, and the DTO should contain the URL of the profile photo, likewise:
"firstName" : "David",
"lastName" : "Beckham",
"profilePhotoURL" : "amazon.s3.com/123"
Now the thing is that I don't save the URLs to the database, because they have an expiry date. Instead I just fetch them from Amazon S3
on the fly. This means that the UserTable
and UserBO
will not have the field profilePhotoURL
, but UserDTO
will have it.
The Question
What is the good way to convert the UserBO
to UserDTO
, but to still give it the additional field (profilePhotoURL
)?
The URL is fetched from the service like String url = urlService.getUrlForUser(userId);
I was thinking to design my code to something similar to this:
public UserDTO getUser(long userId)
return dbService
.fetchById(userId) // UserBO at this point
.toDto() // UserDTO at this point, but missing URL
.withUrl(urlService.getUrlForUser(userId)); // concat the URL to the DTO
The downside of this approach is that the client code can "forget" to call the withUrl(...)
which will result with the DTO lacking the photo URL. Another downside would be if additional fields are required in time, then I would have more and more withX()
, withY()
methods, making the client code harder to maintain.
Any idea on how to make this design better?
java object-oriented design-patterns spring
My REST service has 3 layers:
- Persistence Layer - database entities live here
- Business Logic Layer - business objects live here
- Access Layer - data transfer objects live here
The flow for reading a single entity (/entity/id
) is:
Database Entity Object --> Business Object --> DTO
The trick is that the DTO
should contain additional field compared to the Business Object
or the Database Entity Object
.
To be more exact, I have an entity User
. The data model across layers is:
UserTable --> UserBO --> UserDTO
Every user has a profile photo, and the DTO should contain the URL of the profile photo, likewise:
"firstName" : "David",
"lastName" : "Beckham",
"profilePhotoURL" : "amazon.s3.com/123"
Now the thing is that I don't save the URLs to the database, because they have an expiry date. Instead I just fetch them from Amazon S3
on the fly. This means that the UserTable
and UserBO
will not have the field profilePhotoURL
, but UserDTO
will have it.
The Question
What is the good way to convert the UserBO
to UserDTO
, but to still give it the additional field (profilePhotoURL
)?
The URL is fetched from the service like String url = urlService.getUrlForUser(userId);
I was thinking to design my code to something similar to this:
public UserDTO getUser(long userId)
return dbService
.fetchById(userId) // UserBO at this point
.toDto() // UserDTO at this point, but missing URL
.withUrl(urlService.getUrlForUser(userId)); // concat the URL to the DTO
The downside of this approach is that the client code can "forget" to call the withUrl(...)
which will result with the DTO lacking the photo URL. Another downside would be if additional fields are required in time, then I would have more and more withX()
, withY()
methods, making the client code harder to maintain.
Any idea on how to make this design better?
java object-oriented design-patterns spring
asked Jun 22 at 10:06
wesleyy
1111
1111
closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela Jun 22 at 16:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela
closed as off-topic by Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela Jun 22 at 16:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â Toby Speight, Billal BEGUERADJ, Stephen Rauch, Hosch250, Sam Onela
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes