在养成坏习惯之前需要建议
我有一个名为 AuctionsController 的控制器。其中我有名为 Index() 和 AuctionCategoryListing() 的操作:
//Used for displaying all auctions.
public ActionResult Index()
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions();
return View(auctions);
}
//Used for displaying auctions for a single category.
public ActionResult AuctionCategoryListing(string categoryName)
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions()
.Where(c => c.Subcategory.Category.Name == categoryName);
return View("Index", auctions);
}
如您所知,它们都调用相同的视图(此操作称为“调用视图”。它的正确名称是什么?)。
@model IEnumerable<Cumavi.Models.Auction>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
IDSubcategory
</th>
<th>
IDCity
</th>
<th>
IDPerson
</th>
<th>
Title
</th>
<th>
TextBody
</th>
<th>
ContactNumber
</th>
<th>
AskingPrice
</th>
<th>
AddressDirection
</th>
<th>
LatestUpdateDate
</th>
<th>
VisitCount
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
<td>
@item.IDSubcategory
</td>
<td>
@item.IDCity
</td>
<td>
@item.IDPerson
</td>
<td>
@item.Title
</td>
<td>
@item.TextBody
</td>
<td>
@item.ContactNumber
</td>
<td>
@String.Format("{0:F}", item.AskingPrice)
</td>
<td>
@item.AddressDirection
</td>
<td>
@String.Format("{0:g}", item.LatestUpdateDate)
</td>
<td>
@item.VisitCount
</td>
</tr>
}
</table>
它们都继承自同一个模型。
我的问题是,我是否以正确、适当的方式做事?或者这只是我设法拼凑起来的一个黑客。在我养成坏习惯之前帮助我。
I have a controller called AuctionsController. In it I have Actions called Index() and AuctionCategoryListing():
//Used for displaying all auctions.
public ActionResult Index()
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions();
return View(auctions);
}
//Used for displaying auctions for a single category.
public ActionResult AuctionCategoryListing(string categoryName)
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions()
.Where(c => c.Subcategory.Category.Name == categoryName);
return View("Index", auctions);
}
As you can tell, they both invoke the same View (is this action called 'to invoke a view'. What is it's proper name?).
@model IEnumerable<Cumavi.Models.Auction>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
IDSubcategory
</th>
<th>
IDCity
</th>
<th>
IDPerson
</th>
<th>
Title
</th>
<th>
TextBody
</th>
<th>
ContactNumber
</th>
<th>
AskingPrice
</th>
<th>
AddressDirection
</th>
<th>
LatestUpdateDate
</th>
<th>
VisitCount
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
<td>
@item.IDSubcategory
</td>
<td>
@item.IDCity
</td>
<td>
@item.IDPerson
</td>
<td>
@item.Title
</td>
<td>
@item.TextBody
</td>
<td>
@item.ContactNumber
</td>
<td>
@String.Format("{0:F}", item.AskingPrice)
</td>
<td>
@item.AddressDirection
</td>
<td>
@String.Format("{0:g}", item.LatestUpdateDate)
</td>
<td>
@item.VisitCount
</td>
</tr>
}
</table>
They both inherit from the same Model.
My question is, am I doing things the right appropriate way? Or is this just a hack I managed to scrape together. Help me before I learn a bad habit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你必须在某个地方进行分支,所以这可能更像是一个偏好问题。
我处理它的方法是使用一个方法,并让它以类别名称作为参数。由于字符串可为空,因此如果未指定字符串将为空。我的一个操作方法可能看起来像这样:
You have to do branching somewhere, so it's probably more of a preference question.
The way I would handle it is to have a single method, and have it take in the category name as the parameter. Since strings are nullable, if one is not specified it will be null. My one action method would probably look something like:
出于性能考虑,我更希望存储库执行过滤和分页等操作,并且 DRY 概念
DAL 应该负责此任务。
I would prefer the repo to do things like filtering and pagination for the sake of performance and DRY concept
DAL should be responsible for this tasks.
MVC 的一个特点是视图和控制器是独立的。您同样可以对同一事物使用共享视图或部分视图,如果有的话,我会说这是一件好事,因为您正在编写可重用的代码并利用它。
It's a feature of MVC that the view and controller are independent. You could equally use shared or partial views for the same thing, if anything I'd say it's a good thing because you're writing re-usable code and making use of it.
我会将其修改为:
您的路线可能如下所示:
由于操作非常相似,因此我认为没有理由将它们分开。
I'd modify this to:
Your route might then look like:
Since the actions are so similar, I don't see a reason to separate them.
与任何框架一样,ASP.NET MVC 给您很多搬起石头砸自己脚的机会。如果没有深思熟虑,控制器操作、视图模型和视图的重用很快就会成为维护的噩梦。更不用说如果没有类似的考虑,您的路线将变得很难连接在一起。
遵循约定优于配置的原则,您可以通过使用单独的操作但重用视图部分来解决问题。对我来说,
AuctionsController
的索引操作应该负责列出系统中的所有拍卖。我不会将我的类别操作称为AuctionCategoryListing
,而是将其简单地称为Category
。按照惯例,这可以将路由布局为:site.com/auctions/
,用于索引site.com/auctions/category/CATEGORYNAME
,用于类别。该路线很容易被用户理解,并且您也很容易明确了解每个路线的作用。 (至此,奥马尔在他的回答中提供了一个很好的建议 让您的存储库处理分页、过滤等。)
至于每个操作应返回的内容,您有多种选择。我的偏好是返回单独的视图,每个视图都包含对公共部分的引用。这使您可以灵活地围绕部分创建不同的视图,但可以重复使用常见的部分。
进一步阅读可能会有所帮助:
Like any framework ASP.NET MVC gives you plenty of opportunities to shoot yourself in the foot. Without forethought the reuse of controller actions, view models, and views can quickly become a maintenance nightmare. Not to mention that without similar consideration your routes will become hard to tie together.
Following the tenets of convention over configuration you could solve your problem by using separate actions but reusing a view partial. To me the index action of the
AuctionsController
should be responsible for listing all Auctions in the system. I wouldn't call my category actionAuctionCategoryListing
, but would instead call it simplyCategory
. Through convention this has the nice effect of laying out the routes as:site.com/auctions/
for the indexsite.com/auctions/category/CATEGORYNAME
for the category.The route is easily understandable by the user and easy for you to understand explicitly what each does. (To this point Omar provides a good suggestion in his answer to let your repository handle pagination, filtering etc.)
As far as what each action should return you have several options. My preference would be to return separate views each containing a reference to a common partial. This gives you flexibility to create different views surrounding the partial but provides reuse for the piece that is common.
Further reading that might be of help: