ASP.NET MVC 控制器中的方法似乎没有被 XML 记录是否有原因?

发布于 2024-10-18 21:38:45 字数 790 浏览 4 评论 0原文

我非常喜欢 .NET 中的 XML 文档。

但是,我可以诚实地说,我从未见过教程或项目,例如,我们有这样的标记:

/// <summary>
/// dummy text
/// </summary>
/// <returns>blah</returns>
public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

而不是:

// **************************************
// URL: /Account/LogOff
// **************************************

public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

这有什么特殊原因吗?我是唯一一个想要记录我的控制器方法的人吗?

编辑1:

虽然大多数控制器方法似乎很简单,但在说这个问题时详细说明情况如何: MVC:如何与具有多个子实体的实体一起工作? ?

I'm a huge fan of XML documentation in .NET.

However, I can honestly say I've never seen a tutorial or project where, for example, we had markup like this:

/// <summary>
/// dummy text
/// </summary>
/// <returns>blah</returns>
public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

Instead of:

// **************************************
// URL: /Account/LogOff
// **************************************

public ActionResult LogOff() {
    FormsService.SignOut();

    return RedirectToAction("Index", "Home");
}

Is there any particular reason for this? Am I the only one who wants to document my Controller's methods?

EDIT 1:

And while most controller methods seem to be simple, how about cases detailed in say this question: MVC: How to work with entities with many child entities? ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫相离 2024-10-25 21:38:45

当需要为使用公共 API 的外部方记录公共 API 时,XML 文档非常有用。在我看来,控制器不属于这一类。

同样与苗条控制器一致,它们应该对其所做的事情是不言自明的,特别是对于诸如 HttpPostHttpGet 之类的属性元数据。

您是否设想第三方使用您的控制器作为 API?

XML documentation is great when the public API needs to be documented for external parties using it. Controllers in my opinion, don't fall into that category.

Also in line with slim controllers, they should be self explanatory as to what they do, especially with attribute metadata such as HttpPost and HttpGet.

Do you envisage a third party using your controllers as an API?

糖粟与秋泊 2024-10-25 21:38:45

我通常会用简短的​​描述来记录我的控制器操作,例如该操作的用途,例如:

/// <summary>
/// Controller for viewing and updating the jobs list.
/// </summary>
public class JobsController : Controller
{
    /// <summary>
    /// Displays a list of all jobs for a given site.
    /// </summary>
    /// <param name="siteId">Id of the site to view jobs for.</param>
    public ActionResult Index(string siteId);

    /// <summary>
    /// Displays a detail view of a single job.
    /// </summary>
    /// <param name="id">Id of the job to view.</param> 
    public ActionResult Detail(string id);
}

它与我的其他类的 xml 文档不太一样,因为这些类永远不会直接使用,因此它的更多文档网站/页面行为比其他任何事情都重要。也就是说,我发现描述操作的作用和参数是什么很有用,这里是一个很好的地方。

请注意,我没有包含路径 - 不仅因为如果路径发生更改,则信息已过时,而且还因为通过查看路由映射应该显而易见路径将是什么。

更新/回复评论:

这种文档可能看起来完全没有意义,因为无论如何,这些类几乎都是自我描述的,但是在正确结构化代码中命名良好的方法这通常是 XML 的情况无论如何,文档。然而,我仍然相信此类文档会增加价值:

  • 它用简单的英语阐明了类/方法/参数的作用
  • ,确认没有发生任何特殊情况(而不是有人只是懒得编写文档)。

请注意,我记录返回值,因为我绝对没有什么可以说的。

您还需要考虑到这个示例是非常人为的 - 某些参数可能是 JSON 序列化数据,或者负值意味着完全不同的东西。我对 XML 文档的看法是,要么不记录任何内容,要么应该记录所有内容(无论多么明显)。如果只有一半的方法被记录下来,那么你永远无法判断它是否是因为它完全显而易见,或者开发人员是否只是太懒了 - 还要考虑到方法的目的对其他人来说可能不像对你那么明显。

例如,我懒得为事件处理程序编写文档(我曾经这样做过,但注释总是完全相同)。

I usually document my controller actions with a short description of what that action is meant to do, for example:

/// <summary>
/// Controller for viewing and updating the jobs list.
/// </summary>
public class JobsController : Controller
{
    /// <summary>
    /// Displays a list of all jobs for a given site.
    /// </summary>
    /// <param name="siteId">Id of the site to view jobs for.</param>
    public ActionResult Index(string siteId);

    /// <summary>
    /// Displays a detail view of a single job.
    /// </summary>
    /// <param name="id">Id of the job to view.</param> 
    public ActionResult Detail(string id);
}

It's not quite the same as my xml documentation for other classes because these classes will never be used directly, and so its more documentation of the site / page behaviour than anything else. That said I find it useful to have a description of what the action does and what the parameters are, and here is as good a place as any.

Note that I don't include the path - not only because if the path changes the information is out of date, but also becasue it should be obvious by looking at the route mappings what the path will be anyway.

Update / response to comments:

This sort of documentation may seem entirely pointless as the classes are pretty much self describing anyway, however on well named methods in properly structured code this is usually the case with XML documentation anyway. I still believe that this sort of documentation adds value however:

  • It clarifies what the class / method / parameter does in plain english
  • In confirms that nothing special is happening (as opposed to someone just not bothering to write the documentation).

Note that I don't document the return value as there is absolutely nothing useful that I can ever say about it.

You also need to consider that this example is incredibly contrived - it could be that certain parametrs are JSON serialised data, or that a negative value means something completely different. My view on XML documentation is that you should either document nothing, or you should document everything (no matter how obvious). If only half your methods are documented then you can never tell if its because its entirely obvious, or if the developer was simply too lazy - also consider that the purpose of a method may not be as obvious to others as it is to you.

For example I don't bother to write documentation for event handlers (I used to but the comment was always exactly the same).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文