tempura's avatar

Traits, Service Containers, or just Classes?

I have this upload() method that I re-use on different parts of my app, and it may have different kinds of Implementations like if I decided to switch to another image service.

I am not sure if it's Trait i wanted and just be consistent as it can't have an interface, or a Service Container, or just a Class.

Generally, when should we be using one or the other? Any of the three could do the same thing. Is this subjective or what? Cause I'm a bit lost

0 likes
1 reply
TheFriendlyHacker's avatar

In general, it is pretty subjective as to which tactic you use (whether it be traits or inheritance or other stuff).

But based on the info you've provided, you have a method that may, in the future, implement a different service in order to accomplish the same task (correct me if I am wrong).

That being the case, it sounds like you should check out the Repository pattern. Repositories essentially provide an abstract "api" for your model layer; a bunch of methods that you can use to accomplish your task (e.g, a method called "uploadImage", which uploads a given image). The beauty of repositories is that you can set them up to implement different providers/services which accomplish the same task. That way, you could change the service that manages how images are uploaded, but you can still use the same uploadImage method from your repository to accomplish the task.

Please or to participate in this conversation.