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
Brendonovich
Brendonovich2y ago
assuming you're using trpc, just put the logic in a function?
jonnyfapson
jonnyfapsonOP2y ago
Like I said, this leads to a lot of code duplication, which I'd like to avoid
Brendonovich
Brendonovich2y ago
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
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
jonnyfapson
jonnyfapsonOP2y ago
Thanks, I think this is the best way to do this 👍
Brendonovich
Brendonovich2y ago
i feel like that's a more in depth explanation of what i meant but whatever works haha
Want results from more Discord servers?
Add your server