Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent ORM Active Record - Is this the right approach?

I think that I have finally hit code puberty since I am starting to rebel and question everything I have been taught. I hope someone in here can point me in the right direction. Currently what should be simple and short tasks, end up taking hours because I sit and stare at a few lines of code and question if that is indeed the right way to do it...

Here is an example. We have a system that makes it possible for a User to create a Post and Mention someone in that post. We have a controller that gets the request, which is then sent to a PostService which has a method like: createPostByUser(int $id, string $title, string $content) and eventually sent to $postingUser->post($title, $string) where the post is saved.

In the createPostByUser method we would check if there are any @mentions which we will then pass to a MentionService. So in this case we would access: $mentionService->createMentionByUser(int $id, string $username, Mentionable $mentionable) And then check if the mentioning user ($id) and the mentioned user ($username) exists or possibily even restore the mention if it has exists but is softdeleted. If it doesn't already exist and cannot be restored and both users are valid, then we will proceed to call $mentioningUser->mention($mentionedUser) where the mention will be saved.

While all this works, I start to see some problems with it. What would prevent a new developer from just skipping the service layers and going directly onto the user objects and use these methods? I mean, in the MentionService we check if everything is good before we just save the mention, like checking if both users needed are valid, if the mention should be restored. etc. What would prevent a new unknowing developer from going straight onto the user object and just create a new mention without thinking twice?

Hope someone can point me in the right direction. Are we on the right track here?

Am I just overthinking things?

like image 488
Severin Avatar asked Nov 01 '25 13:11

Severin


1 Answers

I think this issues cannot be resolved in a technical way. Any design pattern and any architectural style can somehow be misunderstood and deliberately circumvented. This problem can probably be solved at the organizational layer (in your team). One possibility would be, for example code reviews, trainings and documentation. Most important would be code reviews with branches and pull requests. With the time you can achieve a learning effect for the new developers and prevent new failures.

like image 116
odan Avatar answered Nov 04 '25 05:11

odan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!