Search code examples
clean-architecturesingle-responsibility-principleservice-layer

Questions about service class single responsibility in Clean Architecture


I'm developing an application to train Clean Architecture concepts. I have a user creation use case, where some information is passed in a multipart/formdata form, which includes the user's avatar. In my application, file upload is not an isolated use case. For the User entity, I just need to return a string with the direct access url to the image (in this case, I'm using Amazon's S3).

In the user creation service, I'm uploading the file to S3 and then, with the returned url, I persist the user's data in the database. In this case, by calling userstore (creation) and S3 in my usercreation service, am I infringing the sole responsibility of the usercreation service?

I have another use case that also uploads a file, very similar to the one used in creating a user. Is there a way to abstract this process and avoid code repetition?


Solution

  • I can't really figure out how your architecture is set up, but I will explain how I would do it.

    In a clean architecture a controller would be responsible for getting the avatar image and all the other data to create a user.

    The controller would then create a request model that contains all the data that the use case needs. E.g. the username, email, image as byte array, and so on.

    The controller then passes the request model to the use case. The use case creates the user entity and persists it with the help of a repository. Now the user entity has an id and I would use this id as the document name in s3. Thus I would create an avatar entity (id and bytes) and persists it using a repository.

    Finally the use case creates a response model that contains the created user information, like the id.

    The controller can then use a presenter that can create a s3 url using the id of the user.