I try to figure out what the different exactly with Model against Repository and Service. i found sometime Repository code will inside Model. Sometime will need middleman Service between Model and Repository.
View is to present data
// index.ejs
<% users.forEach(user => { %>
<tr>
<td>user['id']</td>
<td>user['username']</td>
<td>user['password']</td>
</tr>
<% }) %>
// present data
1 ADMIN ADMINPASS
2 TEST TESTPASS
Repository layer is a layer that logic action to interact with database.
class UserRepository {
public selectAll(){
return DBConnector.execute(query)
}
public insert({username, password})
public update({username, password)
public delete(id)
}
Controller is a layer that transform data from database, then throw that to view layer.
class UserController {
public index(req, res){
// Get all user, then transform to uppercase
const usersList = new UserRepository.selectAll();
const usersTransform = usersList[0].map(user => user.toUpperCase());
res.render('index', {users: usersTransform})
}
}
Route
app.get('/', new UserController.index)
When u need to use this layer? I see alot discusion when talking about MVC. it explains action logic that interact with database inside Model. sometime Model, Service, Repository interchangeable. so confuse right.
class UserService {
}
class UserModel {
}
Aucun commentaire:
Enregistrer un commentaire