在哪里进行请求参数数据类型转换:Controller or Model?
where to perform request parameters data type conversion: Controller or Model?
在MVC应用中,请求参数的数据类型转换应该在哪里进行:Controller还是Model?
例如,我有 3 个参数随请求一起提供:user_name、密码、role_id。所以我在 Controller 中收到了所有 3 个字符串。这是例如 Controller 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
}
}
第三个参数必须被视为整数。所以我有两个选择:
- 首先在Controller中将其转换为整数,然后传递给Model。
在这种情况下,这是 控制器 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
int roleIdN = Integer.parseInt(roleId);
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleIdN);
...
}
}
这是 型号 代码:
public class MyService {
public boolean login(String userName, String password, int roleId) {
...
}
}
- 将其作为字符串传递给 模型; Model 会在使用前将其转换为整数。
在这种情况下,这是 控制器 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleId);
...
}
}
这是 型号 代码:
public class MyService {
public boolean login(String userName, String password, String roleId) {
int roleIdN = Integer.parseInt(roleId);
...
}
}
哪里最有意义?我试图找出 Controller 和 Model 的边界。如果第三个参数不包含数字怎么办?
更新
据我所知,这些是支票:
(a)请求中存在参数
(b) 参数不为 null 或为空。
(c) 参数可以转换为各自正确的数据类型。
(d) 参数在正确的范围内,例如 role_id 应该是 1 到 4 之间的数字。
哪些检查应该在 Controller 中,哪些在 Model 中?
仅针对 (a) 我确定它应该在 Controller 中。
您应该考虑在您的领域中最有意义的表示形式是什么,并将其用于您的模型。让控制器调整它接收到的字符串表示以适应模型数据类型。
因此在您的示例中,控制器应负责转换为 int,并在未收到 int 的情况下返回格式错误的请求。
更新:
根据经验,您希望将域对象构造为有效,因此我会在域边界之前进行所有这些检查。从某种意义上说,他们是世界的一部分 "outside" 您的域。它们链接到模型的外部表示,在本例中为 http 参数
在MVC应用中,请求参数的数据类型转换应该在哪里进行:Controller还是Model?
例如,我有 3 个参数随请求一起提供:user_name、密码、role_id。所以我在 Controller 中收到了所有 3 个字符串。这是例如 Controller 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
}
}
第三个参数必须被视为整数。所以我有两个选择:
- 首先在Controller中将其转换为整数,然后传递给Model。
在这种情况下,这是 控制器 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
int roleIdN = Integer.parseInt(roleId);
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleIdN);
...
}
}
这是 型号 代码:
public class MyService {
public boolean login(String userName, String password, int roleId) {
...
}
}
- 将其作为字符串传递给 模型; Model 会在使用前将其转换为整数。
在这种情况下,这是 控制器 代码:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("user_name");
String password = request.getParameter("password");
String roleId = request.getParameter("role_id");
MyService myService = new MyService();
boolean result = myService.login(userName, password, roleId);
...
}
}
这是 型号 代码:
public class MyService {
public boolean login(String userName, String password, String roleId) {
int roleIdN = Integer.parseInt(roleId);
...
}
}
哪里最有意义?我试图找出 Controller 和 Model 的边界。如果第三个参数不包含数字怎么办?
更新
据我所知,这些是支票:
(a)请求中存在参数
(b) 参数不为 null 或为空。
(c) 参数可以转换为各自正确的数据类型。
(d) 参数在正确的范围内,例如 role_id 应该是 1 到 4 之间的数字。
哪些检查应该在 Controller 中,哪些在 Model 中? 仅针对 (a) 我确定它应该在 Controller 中。
您应该考虑在您的领域中最有意义的表示形式是什么,并将其用于您的模型。让控制器调整它接收到的字符串表示以适应模型数据类型。
因此在您的示例中,控制器应负责转换为 int,并在未收到 int 的情况下返回格式错误的请求。
更新: 根据经验,您希望将域对象构造为有效,因此我会在域边界之前进行所有这些检查。从某种意义上说,他们是世界的一部分 "outside" 您的域。它们链接到模型的外部表示,在本例中为 http 参数