I have UsersService and UsersRepository.
UserModel looks like
{id:1, name: 'Eugene', age: 23}
Each user has an avatar and the avatar filename is just user id (for user with id 1 image will stored as "/assets/usersAvatars/1.jpg")
- I don't store filename info in a database, is it the correct approach?
- In the situation where I have to return a user object but with avatar field too (I suppose this is needed for each request), where should I place logic for attaching this "avatar" field to user object, Repository or Service?
{id:1, name: 'Eugene', age: 23, avatar: "/assets/usersAvatars/1.jpg"}
CodePudding user response:
You are ultimately asking if you should store redundant data in the repository because the path is derivable from the user id. The answer is generally "no". Don't keep extra copies of data you don't need, and don't build flexibility until you know you will need it. If, at a later point, you decide you need an avatar path, that would be the time to build in that flexibility.
CodePudding user response:
As per the ECB pattern it says
an entity represents long-lived information relevant for the stakeholders (i.e. mostly derived from domain objects, usually persistent);
a boundary encapsulates interaction with external actors (users or external systems); a control ensures the processing required for the execution of a use-case and its business logic, and coordinates, sequences controls other objects involved in the use-case.
This means in your case, here the Entity is persistent which is your repository service, in other words, its domain object. You should keep this logic in the repository service.
Now your 1st question I don't store filename info in a database, is it the correct approach?
I do not think this is correct approach because imagine if you need to store multiple avatar and you need to keep a different versions of it, so in this case 1-1 relation will not work. You need 1 to many relations.