Thursday, 18 June 2020

What is the role of DAL and Services in Software Development?

I was looking for the best way to structure a node project and stumbled upon this source from the Node.js Best Practices repo on GitHub. It says:The ultimate solution is to develop small software: divide the whole stack into self-contained components that don't share files with others, each constitutes very few files (e.g. API, service, data access, test, etc.) so that it's very easy to reason about it.Now, in my typical express app, the folder structure would look like this:``` models ---> User.jscontrollers ---> usersController.jsroutes ---> users.js ```User.js will have the user schema or the user document that will have fields like username, email, etc. Database model, in general.```js const userSchema = new Schema({ username: { type: String, required: true }, email: { type: String, reuired: true }, password: { type: String, required: true }, }, { timestamps: true })const User = mongoose.model("User", userSchema)module.exports = User ```usersController.js will have all the logic. For example, to get a user from the database, there will be a query to the db, and after that, a response is sent to the client or to the views. I've just shown a single function; there are other functions as well, such as for login, registration, list all users, etc.js module.exports = { getUser: async (req, res, next) => { try { const user = await User.findById(req.params.id) if (!user) { return res.status(404).json({ message: "User not found" }) } return res.status(200).json({ user }) } catch (error) { return next(error) } }, }I have all my endpoints in the routes directory. For example, in routes/users.js:```js router.post("/register", usersController.registerUser) router.post("/login", usersController.loginUser) router.get("/:id", usersController.getUser)module.exports = router```In the best practices repo, I've seen the folder structure with files like usersDAL.js and usersService.js. I know that DAL stands for Data Access Layer but I need to know what is its real use and how do I incorporate it into my current folder structure.

Submitted June 18, 2020 at 12:01PM by psychedelicreddog

No comments:

Post a Comment