Your preferred approach to have a data layer

Before dependency injection became an so normalized, I would always add a data layer to my web projects to keep all the work for DB operations separate from the controllers and views of my applications. Nowadays I see most projects injecting the DB client or context and having the database operation happen in a private function in the controller. Works fine and easy enough to replicate, but feels like I am bloating my controllers and constantly violating DRY.

It feels cleaner sometimes to have a static class/function, pass the client/dbcontext by reference to the function and perform it’s operations there. also works fine and easy enough to do, but seems like it is a bit verbose.

So curious which is more common stylistically or which you tent to prefer. Both work just fine, but neither feels just right organizationally.

submitted by /u/Fastbreak99
[link] [comments]

Leave a Reply