制作一个不使用扩展对象的扩展方法是一个好习惯吗?

发布于 2024-09-25 09:57:52 字数 1076 浏览 5 评论 0原文

在我们的 ASP.NET MVC 项目中,我们有一个 HtmlHelper 扩展方法来生成静态谷歌地图。

public static MvcHtmlString StaticMap(this HtmlHelper helper, string address, string alt, int width, int height, int zoom)
{
    var src = new Uri("http://maps.google.com/maps/api/staticmap?markers=size:mid|color:red|{0}&zoom={1}&size={2}x{3}&maptype=roadmap&sensor=false".FormatInvariant(Uri.EscapeUriString(address), zoom, width, height));
    var href = new Uri("http://maps.google.com/maps?f=q&source=s_q&hl=en&q={0}".FormatInvariant(Uri.EscapeUriString(address)));

    var img = new TagBuilder("img");
    img.MergeAttribute("src", src.ToString());
    img.MergeAttribute("alt", alt);

    var link = new TagBuilder("a") { InnerHtml = img.ToString() };
    link.MergeAttribute("href", href.ToString());

    return MvcHtmlString.Create(link.ToString());
}

对于这个新项目,我们还试图保持所有代码分析规则的开启。现在,显然,Visual Studio Code 分析表明我们应该删除 helper 参数,因为它没有被使用。

这让我想知道扩展方法是否应该始终使用扩展对象,如果不使用,那么它可能不应该是扩展方法。

有人有可以帮助我做出决定的指南或说明的链接吗?

In our ASP.NET MVC project, we have an HtmlHelper extension method to generate a static google map.

public static MvcHtmlString StaticMap(this HtmlHelper helper, string address, string alt, int width, int height, int zoom)
{
    var src = new Uri("http://maps.google.com/maps/api/staticmap?markers=size:mid|color:red|{0}&zoom={1}&size={2}x{3}&maptype=roadmap&sensor=false".FormatInvariant(Uri.EscapeUriString(address), zoom, width, height));
    var href = new Uri("http://maps.google.com/maps?f=q&source=s_q&hl=en&q={0}".FormatInvariant(Uri.EscapeUriString(address)));

    var img = new TagBuilder("img");
    img.MergeAttribute("src", src.ToString());
    img.MergeAttribute("alt", alt);

    var link = new TagBuilder("a") { InnerHtml = img.ToString() };
    link.MergeAttribute("href", href.ToString());

    return MvcHtmlString.Create(link.ToString());
}

For this new project, we are also trying to keep all code analysis rule on. Now, obviously, Visual Studio Code analysis states that we should remove the helper parameter because it is not being used.

That made me wondering if an extension method should always make use of the extended object and if it does not, then maybe it shouldn't be an extension method.

Does someone has a link to a guideline or an explication that would help me decide?

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

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

发布评论

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

评论(3

盛夏尉蓝 2024-10-02 09:57:52

如果您不使用 helper 对象,则您不会扩展任何内容,并且确实没有理由不将其设为您自己的命名空间中的常规静态方法。

If you're not using the helper object, you'r not extending anything and there's really no reason not to make this a regular static method in your own namespace.

韵柒 2024-10-02 09:57:52

扩展方法和静态方法之间的使用代码的区别纯粹是概念性的;一个以与实例方法相同的方式涉及特定类型的对象,而另一个则不然。

既然如此,我会问一个问题:“考虑这个操作在 HtmlHelper 对象上有意义吗?”。这又变成“考虑 HtmlHelper 对象提供此操作有意义吗?”。如果我对这个问题的回答是“是”,那么无论我是否使用 HtmlHelper 对象,我都会认为扩展方法是一种合理的方法。相反,如果我对这个问题回答“否”,我会认为扩展方法是一种不明智的方法,即使确实使用了 HtmlHelper 对象。

代码分析在分析代码使用方面比分析概念做得更好,因此这里给出了警告。它是不完美的,并且确实存在其他情况,忽略参数是合理的(保持与先前版本或接口的兼容性是最好的情况,“保留以供将来使用”是更有争议的情况)。

值得注意的是,在某些语言(例如 C++)中,您可以精确地省略参数名称,以表示“此参数在签名中,但不会被使用”。我非常喜欢这一点,因为通常不使用参数确实是一个坏兆头,所以最好有一种方法来表明您是故意这样做的。

编辑:

另一个理由。想象一下,您在一个类上有一个使用相关对象的扩展方法。现在想象一下,您意识到,通过以不再使用该对象的方式重写,可以使其更可靠、更高效,或者在某些“更好”值上“更好”。既然你已经改进了扩展方法,难道不应该允许你保留它吗?你应该被迫继续使用劣质版本吗?

The distinction to the using code between an extension method and a static method is purely conceptual; one relates to an object of a particular type in the same manner as an instance method, and the other does not.

This being so, I would ask the question, "does considering this operation to be upon an HtmlHelper object make sense?". This in turn becomes "does considering HtmlHelper objects to provide this operation, make sense?". If I answered "yes" to that question, I would consider an extension method to be a reasonable approach, whether I used an HtmlHelper object or not. Conversely, if I answered "no" to that question, I would consider an extension method to be an ill-advised approach, even if the HtmlHelper object did get used.

The code-analysis does a better job at analysing code-use than concepts, so it gives a warning here. It's imperfect, and indeed there are other cases where ignoring a parameter is reasonable (maintaining compatibility either with a previous version or an interface being the best case, "reserved for future use" being a more arguable case).

It's notable that in some languages (well, C++ for one) you can leave a parameter name out precisely to mean "this parameter is in the signature, but won't be used". I quite like this, as it is true that generally not using a parameter is a bad sign, so it's nice to have a means to indicate you were deliberate in this.

Edit:

Another justification. Imagine you had an extension method on a class that did use the relevant object. Now imagine you realise you can make it more reliable, efficient, or otherwise "better" for some value of "better" by re-writing in such a way that you are no longer using the object. Should you not be allowed to keep the extension method now that you've improved it? Should you be forced to stay with the inferior version?

醉梦枕江山 2024-10-02 09:57:52

为什么一开始就将此作为扩展方法?我想说这不是你应该做的事情。我想不出一个好的理由。

Why did you make this an extension method to begin with? I'd say that this is not something you should do. I can't think of a good reason for it.

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