当我的方法是 Helper、Utility 还是应该变成 Extension 时?
例如,这个加密函数可以称为实用程序?
public static string HashText(string text)
{
byte[] encodedBytes;
using (var md5 = new MD5CryptoServiceProvider())
{
var originalBytes = Encoding.UTF8.GetBytes(text);
encodedBytes = md5.ComputeHash(originalBytes);
}
return Encode(encodedBytes);
}
而这个其他功能将是一个 Helper ?
public static string Encode(byte[] byteArray)
{
if (byteArray == null || byteArray.Length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteArray.Length; i++)
{
sb.Append(byteArray[i].ToString("x2"));
}
return sb.ToString();
}
扩展怎么样,你能提供一些简单的例子来说明我什么时候应该将某些东西变成方法的扩展吗?
For example this encryption function could be called a Utility ?
public static string HashText(string text)
{
byte[] encodedBytes;
using (var md5 = new MD5CryptoServiceProvider())
{
var originalBytes = Encoding.UTF8.GetBytes(text);
encodedBytes = md5.ComputeHash(originalBytes);
}
return Encode(encodedBytes);
}
While this other function would be a Helper ?
public static string Encode(byte[] byteArray)
{
if (byteArray == null || byteArray.Length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteArray.Length; i++)
{
sb.Append(byteArray[i].ToString("x2"));
}
return sb.ToString();
}
How about a Extension could you provide any simple examples of when I should turn something into a extension of a method ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
何时将方法设为静态辅助方法或扩展方法是一个主观决定。如果您需要帮助来决定是否使某个方法成为扩展方法,那么问问自己将该方法作为普通实例方法包含在另一个类中是否有意义。
例如,您可能有这样的方法:
作为
String
类的实例方法,这是否有意义?从设计角度来看,这会用完全不相关的类污染
String
类。它使String
类成为一个“厨房水槽”,可以收集与字符串远程相关的所有内容。另一方面,假设您有这样的方法:
这作为
String
类的实例方法有意义吗?绝对地!这是现有
Split
功能的自然扩展。 它可能包含在String
的原始实现中。之间的所有内容都可以根据具体情况进行判断。
When to make a method a static helper method or an extension method is a subjective decision. If you need help deciding whether or not to make a method an extension method, then ask yourself whether it makes sense to include that method in the other class as a normal instance method.
For example, you might have a method like this:
Does this make sense as an instance method of the
String
class?Design-wise this polutes the
String
class with a completely unrelated class. It makes theString
class a "kitchen sink" that collects anything and everything remotely related to strings.On the other hand, suppose you have method like this:
Does this make sense as an instance method of the
String
class?Absolutely! This an natural extension to the existing
Split
functionality. It could have been included in the original implementation ofString
.Everything in between can be judged on a case-by-case basis.
我不确定这是否完全正确,但我经常听到术语“帮助程序”和“实用程序”可以互换使用。在我看来,它们都是相当非正式的术语,因为它们指的是组织方法,而不是有关语言如何处理它们的任何具体内容。尽管如此,还是有一些含义可能会消除您的一些困惑。
实用程序方法通常是经常使用的方法,但并不特定于您的应用程序。余弦和正弦方法就是很好的例子。
辅助类或辅助方法通常经常使用,但特定于您的应用程序。加密就是一个很好的例子,因为所采用的加密方法因应用程序而异。
关于何时应该编写实用方法,答案是这并不是真正的“应该”或“不应该”的情况。就性能而言,差异可以忽略不计(如果有的话),并且是否使用扩展方法或将参数传递给帮助器/实用程序方法纯粹是偏好问题。虽然扩展方法可能给人一种更清晰的感觉,但有许多语言不支持扩展方法,因此这两种方法都没有错误,并且对源代码的可读性的影响可以忽略不计。
I'm not sure if it's entirely correct but I've often heard the terms helper and utility used interchangeably. They both strike me as fairly informal terms since they refer to an organizational approach rather than anything specific about how the language handles them. Still, there are some connotations that may clear up some confusion for you.
A utility method will typically be one that is used often but is not specific to your application. Cosine and Sine methods are good examples of this.
A helper class or helper method is typically used often but is specific to your application. Encrypt is a good example because encryption methods employed vary from application to application.
With your regard to when you should write a utility method the answer is it's not really a "should" or "should not" kind of situation. There is negligible (if any) difference when it comes to performance and whether you use an extension method or pass in a parameter to a helper/utility method is purely a matter of preference. While an extension method may have a cleaner feel to it there are many languages that do not support extension methods so neither approach is wrong and will have a negligible impact on readability of your source code.
关于扩展方法,不应过度使用它们。
当扩展方法似乎可以扩展对象的现有功能时,它们是最好的。例如,您可以使用方法
AppendHex
扩展StringBuilder
。但是,如果功能看起来与对象或其现有功能无关,那么使用“实用程序”类是更好的做法。这些方法将更容易阅读、编写和理解。
Regarding the extension methods, they shouldn't be overused.
Extension methods are best when they seem to extend the existing functionality of an object. For example, you might extend
StringBuilder
with a methodAppendHex
.However, if the functionality seems unrelated to the object or to its existing functionality, using a "utility" class is a better practice. It will be easier to read, write, and understand these methods.