How to deal with more complex / repeating backend logic?
Sometimes, when developing an application, more complex situations occur.
For example, I have a category, which is assignable to either a post or a user,
with the post and user obviously being to different entities in the db.
Let's say I want to use the post and user, populated with their assigned categories in the frontend.
And let's also say, fetching a categories desired 'return object' is a bit complex, as additional joins with other
tables need to happen, in order to populate some meta data.
One way to solve this problem, would be to have the same category fetching logic in the post and user router.
This however leads to code duplication.
Another way would be to create a service, which is responsible for fetching a category and returning the 'return object',
which is most likely wrapped in some sort of interface, so it is easier to reuse.
I'm just thinking out loud, does anyone have some recommendations, to handle more complex business logic,
without having to duplicate a lot of code? Do you recommend using a simple service pattern?
6 Replies
assuming you're using trpc, just put the logic in a function?
Like I said, this leads to a lot of code duplication, which I'd like to avoid
i'd think that reusing a function would avoid code duplication but ehh
if u wanna go balls to the wall then use graphql
it's great for highly relational data
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Thanks, I think this is the best way to do this 👍
i feel like that's a more in depth explanation of what i meant but whatever works haha