Organizing service methods that require multiple dependencies #5
-
I'm curious how one would organize a service that requires multiple dependencies to carry out an operation. For example if I had an s3AttachmentSvc := s3.NewAttachmentService()
attachmentSvc := postgres.NewAttachmentService(s3AttachmentService)
attachmentSvc.create(&Attachment{...}) // atomically stores on s3 and pg Thanks for creating this project and its accompanying articles. I enjoyed your earlier articles, and I've used this pattern for many of my and my company's Go projects! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi Sean, I would probably wrap it how you have done there with I'm not sure you can necessarily make the operation atomic. Regardless of the order you wrap them, if it fails halfway through, then you'll have the attachment stored in only one place. You could have a cleanup operation that removes objects from S3 that have not been persisted in Postgres to fix that though. |
Beta Was this translation helpful? Give feedback.
Hi Sean,
I would probably wrap it how you have done there with
postgres
wrappings3
. That seems intuitive to me although you could do it the other way around.I'm not sure you can necessarily make the operation atomic. Regardless of the order you wrap them, if it fails halfway through, then you'll have the attachment stored in only one place. You could have a cleanup operation that removes objects from S3 that have not been persisted in Postgres to fix that though.