处理单数和复数控制器/路由 [英] Handling Singular and Plural Controllers/Routes
问题描述
我对我应该如何处理我的web应用单数和复数的路线和控制器有点糊涂了。
I'm a little confused on how I should handle singular and plural routes and controllers in my web application.
该网站是一个简单的报价网站 - 爱因斯坦认为,莎士比亚等不保险。在该项目我有一个叫做'QuoteController控制器。控制器名称是单数,那么这是否意味着控制器应只处理单引号的显示? I.E.
The website is a simple quotes site - think Einstein, Shakespeare etc. not Insurance. Within the project I have a controller called `QuoteController'. The controller name is singular, so does this mean that the controller should only handle the display of single quotes? I.E.
/quote/love-is-a-battlefield-1
我再需要另一个控制器多个报价(复数)的显示?例如:
Do I then need another controller for the display of multiple quotes (plural)? For example:
/quotes/ (would default to most recent)
/quotes/new
/quotes/shakespeare
/quotes/popular
它是惯例,还是不错的实践,有单复数的路线单独的控制器?我希望是有道理的。
Is it convention, or good practice, to have separate controllers for singular and plural routes? I hope that makes sense.
推荐答案
仅仅因为ASP-MVC的默认控制器具有奇异的名字,这并不意味着你应该实现单数形式为所有的控制器。
Just because the default controllers of asp-mvc have singular names, it doesn't mean you should implement singular form for all your controllers.
正确的答案是:这取决于实体控制器重新presents的数量。
The correct answer is: it depends on the quantity of the entity that your controller represents.
奇异,例如的AccountController
是奇异的,因为它重新presents动作(action方法)只属于一个帐户。
Singular, example the AccountController
is singular because it represents actions (action method) pertaining to a single account only.
复数如果你的控制器包含在一个单一的交易处理多个实体至少有一个操作方法。
Plural If your controller contains at least one action method that handles multiple entities at a single transaction.
示例复数形式
users/update/3
上面的路线会让你觉得你正在编辑的所有用户,如果你喜欢一个句子读不有道理。不过,如果你读你的路线查询功能,它将使更多的意义。
The route above makes you think you are editing all users, which does not makes sense if you read it like a sentence. However, if you read your route like query, it will make much more sense.
如果我们仔细想想,一个路由是一个查询: {实体} / {行动} / {参数}
看起来像一个查询我
If we think about it, a route IS a query: {entities}/{action}/{parameter}
looks like a query to me.
用户/
用户速记/全部
读的选择所有用户表的
用户/ 123
读的从用户表中选择单一的实体的
用户/更新/ 123
读的从用户表更新单独的实体的
主要网站使用复数格式为:,见下文
stackoverflow.com/questions <- list of questions (multiple)
stackoverflow.com/questions/18570158 <- individual question (single)
stackoverflow.com/questions/ask <- new question (single)
stackoverflow.com/users <- display list of users (multple)
stackoverflow.com/users/114403 <- individual user (single)
asp.net/mvc/tutorials <- display list of tutorials (multiple)
asp.net/mvc/tutorials/mvc-5 <- individual tutorial (single)
facebook.com/messages/ <- Display list of messages (multiple)
facebook.com/messages/new <- Create a single message (single)
facebook.com/messages/john <- view individual messages (multiple)
我认为,英语语法应该在每一个编程方面的严格纳入。它读取更自然,并导致良好的code卫生。
I believe that English grammar should be strictly incorporated in every programming aspect. It reads more natural, and leads to good code hygiene.
这篇关于处理单数和复数控制器/路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!