控制器数量
我正在制作一个 Asp.Net MVC 3 项目,该项目应该有大约 10-15 个视图。
我该如何决定是将它们放入一个控制器还是多个控制器中?
或者对数据库的一个表使用一个控制器是一种更好的方法吗?
I'm making an Asp.Net MVC 3 project which should have about 10-15 views.
How should I decide if I should put them in one controller or many controllers?
Or is it a better approach to use one controller for one table of the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从逻辑上分离您的控制器。如果您有处理产品的东西,请创建一个产品控制器。如果您正在处理帐户,请使用 AccountsController 等。
Separate your controllers logically. If you have something that is dealing with Products, make a Products controller. If you are dealing with an Account, use an AccountsController, etc.
真的取决于你的设计。示例:具有地址的客户端可能保存在多个表中(客户端信息、地址等),在这种情况下,您可能最好只使用客户端控制器。无论如何,这只是一种情况。
Depends on your design really. Example: Client with address, might be saved in multiple tables (clientinfo, address, etc.), you probably are better off with just a client controller in that case. Anyways, it's just one scenario.
通常,只要遇到您还没有控制器的新业务实体,您就应该创建一个新控制器。因此,您可以创建一个
CustomerController
和一个ProductsController
,但创建CustomersToProductsController
没有意义。控制器既不是与数据库表一对一的关系,也不是与视图一对一的关系。我希望这有帮助。
Typically you should create a new Controller anytime you encounter a new Business Entity that for which you do not have a Controller already. Therefore, you may create a
CustomerController
and aProductsController
, but it doesn't make sense to create aCustomersToProductsController
.Controllers are neither 1-to-1 with database tables and they are certainly not 1-to-1 with Views. I hope this helps.
使用控制器到表方法绝对不是更好,除非您的表的布局使得每个逻辑对象及其所有部分都在一个表中。正如解雇所说。按模块或用户组或权限或组合进行逻辑分组。即注册、登录、产品、用户管理等。
Definitely not better to use a controller to table approach unless your tables are laid out such that each logical object and all of its pieces are in one table. As Dismissle said. Logical groupings either by module or user group or permissions or a combination. i.e. Registration, login, products, usersadmin and so on.
您可以根据您的域需求创建控制器。例如,如果您有一个具有注册表单 /login 的系统,您可以为其创建一个名为“User”的控制器,并且可以为它们使用不同的 Actions 方法。
类似地,您可以为另一个域模型(例如产品)设置另一个控制器,
我猜,您的表将主要代表您的域模型。所以你可以为每个表创建一个控制器,如用户、客户、产品、订单等。
You can create controllers based on your domain needs. For example if you have a system which has a signup form /login , you can create a controller for that with name "User" and can have different Actions methods for these.
similarly you can have another controller for your another domain model like Product
I guess, your tables will be mostly represnting your domain model. so you can create a controller for each table like User, Customer, Products,Order etc..