在。net中使用扩展方法的最佳实践是什么?

时间:2021-08-02 16:20:09

I have seen these being used every which way, and have been accused of using them the wrong way (though in that case, I was using them that way to demonstrate a point).

我已经看到这些被使用的方式,并被指责使用错误的方式(尽管在这种情况下,我使用它们来演示一个点)。

So, what do you think are the best practices for employing Extension Methods?

那么,您认为使用扩展方法的最佳实践是什么?

Should development teams create a library of extension methods and deploy them across various projects?

开发团队是否应该创建一个扩展方法库并在不同的项目中部署它们?

Should there be a collection of common extension methods in the form of an open source project?

是否应该以开放源码项目的形式存在一组通用的扩展方法?

Update: have decided to create an organization wide extension methods library

更新:已经决定创建一个组织范围的扩展方法库。

6 个解决方案

#1


7  

The upcoming release of the Framework Design Guidelines, 2nd Edition will have some guidance for implementing extension methods, but in general:

即将发布的框架设计指南,第二版将有一些关于扩展方法的指导,但是一般来说:

You should only define extension methods "where they make semantic sense" and are providing helper functionality relevant to every implementation.

您应该只定义“具有语义意义的”扩展方法,并提供与每个实现相关的帮助功能。

You also should avoid extending System.Object as not all .NET languages will be able to call the extension method as an extension. (VB.NET for instance would need to call it as a regular static method on the static extension class.)

您还应该避免扩展系统。对象并不是所有的。net语言都能够调用扩展方法作为扩展。(VB。例如,NET需要将其称为静态扩展类的常规静态方法。

Don't define an extension method in the same namespace as the extended type unless you're extending an interface.

不要在与扩展类型相同的名称空间中定义扩展方法,除非您正在扩展一个接口。

Don't define an extension method with the same signature as a "real" method since it will never be called.

不要定义具有与“真实”方法相同签名的扩展方法,因为它永远不会被调用。

#2


4  

you might want to take a look at http://www.codeplex.com/nxl and http://www.codeplex.com/umbrella which are both extension method libraries. I personally haven't had a look at the source code but I'm sure the guys there would be able to give you some good pointers.

您可能想看看http://www.codeplex.com/nxl和http://www.codeplex.com/umbrella,它们都是扩展方法库。我个人还没有看过源代码,但是我相信那里的人会给你一些很好的指示。

#3


3  

I've been including my extension methods in with my Core libraries in the Utils class because people who are working with my framework are likely to find the methods useful, but for mass deployment where the end developer might have a choice of extension method libraries, I would advise putting all of your extensions into their own namespace, even their own project file, so that people can choose to add a reference or a using statement or simply where required, like so:

我包括我的扩展方法和核心库的Utils类,因为人们谁正在与我的框架可能会发现有用的方法,但对于大规模部署的开发人员可能会选择结束扩展方法库,我建议把你的所有扩展到自己的名称空间,甚至自己的项目文件,这样人们可以选择添加一个引用或使用声明或简单的要求,像这样:

Core.Extensions.Base64Encode(str);

My Utils class is my bestest friend in the whole world, it was before extension methods came along and they have only helped to strengthen our relationship. The biggest rule I would go by is to give people choice over what extension framework they are using where possible.

我的Utils课程是我在全世界最好的朋友,它是在推广方法出现之前,它们只帮助加强我们的关系。我要遵循的最大规则是,让人们在可能的情况下选择使用什么扩展框架。

#4


3  

The Objective-C language has had "Categories" since the early 1990s; these are essentially the same thing as .NET Extension Methods. When looking for best practices you might want to see what rules of thumb Objective-C (Cocoa & NeXT) developers have come up with around them.

自20世纪90年代初以来,Objective-C语言就有了“范畴”;它们本质上与。net扩展方法相同。在寻找最佳实践时,您可能想看看Objective-C (Cocoa & NeXT)开发人员围绕它们提出了哪些规则。

Brent Simmons (the author of the NetNewsWire RSS reader for Mac OS X and iPhone) just posted today about his new style rules for the use of categories and there's been a bit of discussion in the Cocoa community around that post.

布伦特·西蒙斯(Brent Simmons, Mac OS X和iPhone的NetNewsWire RSS阅读器的作者)今天刚刚发布了他的关于分类使用的新风格规则,关于这篇文章在可可社区有一些讨论。

#5


2  

I think that it depends on what purpose the Extension methods serve.

我认为这取决于扩展方法的目的。

  • Extension methods that relate to specific business needs of a project (whether they are connected to basic data types or custom objects) should not be included in a library that would be distributed across multiple projects.
  • 与项目的特定业务需求相关的扩展方法(无论它们是连接到基本数据类型还是自定义对象)不应该包含在跨多个项目分布的库中。
  • Extension methods that relate to basic data types (int, string, etc) or generics that have a wider application could be packaged and distributed across projects.
  • 与基本数据类型(int、string等)或具有更广泛应用程序的泛型相关的扩展方法可以打包并跨项目分布。

Take care not to globally include Extension methods that have little application, as they just clog up intellisense and can lead to confusion and/or misuse.

注意不要在全局范围内包含应用很少的扩展方法,因为它们只会阻塞智能感知并可能导致混乱和/或误用。

#6


1  

When I first found out about Extensions I really overused and abused them.

当我第一次发现扩展时,我真的过度使用和滥用了它们。

For the most part I have started to get away from using any Extension Methods for a number of reasons.

在大多数情况下,我已经开始不使用任何扩展方法,原因有很多。

Some of the reasons I stopped using them are noted in Scott's blog link above, such as "Think twice before extending types you don't own". If you have no control over the source for the types you are extending, you may encounter issues/collisions in the future if the source type has some additions/changes, such as moving your project to a newer .NET version. If the newer .NET version includes a method on the type of the same name as your extension, someone is going to get clobbered.

上面Scott的博客链接中提到了我停止使用它们的一些原因,比如“在扩展你不拥有的类型之前要三思”。如果您无法控制正在扩展的类型的源,那么如果源类型有一些添加/更改(例如将项目移动到新的. net版本),您将来可能会遇到问题/冲突。如果新的。net版本包含与扩展名类型相同的方法,那么将会有人遭受失败。

The main reason why I stopped using Extension Methods is that you can't quickly tell from reading the code where the source of the method is and who "owns" it.

我停止使用扩展方法的主要原因是,您不能从读取方法源代码的位置和谁“拥有”该方法的代码中快速判断。

When just reading through the code you can't tell whether the method is an extension or just a standard NET API method on the type.

在读取代码时,您无法判断该方法是否为扩展,或者只是类型上的标准NET API方法。

The intellisense menu can get really messy really fast.

智能感知菜单会很快变得非常混乱。

#1


7  

The upcoming release of the Framework Design Guidelines, 2nd Edition will have some guidance for implementing extension methods, but in general:

即将发布的框架设计指南,第二版将有一些关于扩展方法的指导,但是一般来说:

You should only define extension methods "where they make semantic sense" and are providing helper functionality relevant to every implementation.

您应该只定义“具有语义意义的”扩展方法,并提供与每个实现相关的帮助功能。

You also should avoid extending System.Object as not all .NET languages will be able to call the extension method as an extension. (VB.NET for instance would need to call it as a regular static method on the static extension class.)

您还应该避免扩展系统。对象并不是所有的。net语言都能够调用扩展方法作为扩展。(VB。例如,NET需要将其称为静态扩展类的常规静态方法。

Don't define an extension method in the same namespace as the extended type unless you're extending an interface.

不要在与扩展类型相同的名称空间中定义扩展方法,除非您正在扩展一个接口。

Don't define an extension method with the same signature as a "real" method since it will never be called.

不要定义具有与“真实”方法相同签名的扩展方法,因为它永远不会被调用。

#2


4  

you might want to take a look at http://www.codeplex.com/nxl and http://www.codeplex.com/umbrella which are both extension method libraries. I personally haven't had a look at the source code but I'm sure the guys there would be able to give you some good pointers.

您可能想看看http://www.codeplex.com/nxl和http://www.codeplex.com/umbrella,它们都是扩展方法库。我个人还没有看过源代码,但是我相信那里的人会给你一些很好的指示。

#3


3  

I've been including my extension methods in with my Core libraries in the Utils class because people who are working with my framework are likely to find the methods useful, but for mass deployment where the end developer might have a choice of extension method libraries, I would advise putting all of your extensions into their own namespace, even their own project file, so that people can choose to add a reference or a using statement or simply where required, like so:

我包括我的扩展方法和核心库的Utils类,因为人们谁正在与我的框架可能会发现有用的方法,但对于大规模部署的开发人员可能会选择结束扩展方法库,我建议把你的所有扩展到自己的名称空间,甚至自己的项目文件,这样人们可以选择添加一个引用或使用声明或简单的要求,像这样:

Core.Extensions.Base64Encode(str);

My Utils class is my bestest friend in the whole world, it was before extension methods came along and they have only helped to strengthen our relationship. The biggest rule I would go by is to give people choice over what extension framework they are using where possible.

我的Utils课程是我在全世界最好的朋友,它是在推广方法出现之前,它们只帮助加强我们的关系。我要遵循的最大规则是,让人们在可能的情况下选择使用什么扩展框架。

#4


3  

The Objective-C language has had "Categories" since the early 1990s; these are essentially the same thing as .NET Extension Methods. When looking for best practices you might want to see what rules of thumb Objective-C (Cocoa & NeXT) developers have come up with around them.

自20世纪90年代初以来,Objective-C语言就有了“范畴”;它们本质上与。net扩展方法相同。在寻找最佳实践时,您可能想看看Objective-C (Cocoa & NeXT)开发人员围绕它们提出了哪些规则。

Brent Simmons (the author of the NetNewsWire RSS reader for Mac OS X and iPhone) just posted today about his new style rules for the use of categories and there's been a bit of discussion in the Cocoa community around that post.

布伦特·西蒙斯(Brent Simmons, Mac OS X和iPhone的NetNewsWire RSS阅读器的作者)今天刚刚发布了他的关于分类使用的新风格规则,关于这篇文章在可可社区有一些讨论。

#5


2  

I think that it depends on what purpose the Extension methods serve.

我认为这取决于扩展方法的目的。

  • Extension methods that relate to specific business needs of a project (whether they are connected to basic data types or custom objects) should not be included in a library that would be distributed across multiple projects.
  • 与项目的特定业务需求相关的扩展方法(无论它们是连接到基本数据类型还是自定义对象)不应该包含在跨多个项目分布的库中。
  • Extension methods that relate to basic data types (int, string, etc) or generics that have a wider application could be packaged and distributed across projects.
  • 与基本数据类型(int、string等)或具有更广泛应用程序的泛型相关的扩展方法可以打包并跨项目分布。

Take care not to globally include Extension methods that have little application, as they just clog up intellisense and can lead to confusion and/or misuse.

注意不要在全局范围内包含应用很少的扩展方法,因为它们只会阻塞智能感知并可能导致混乱和/或误用。

#6


1  

When I first found out about Extensions I really overused and abused them.

当我第一次发现扩展时,我真的过度使用和滥用了它们。

For the most part I have started to get away from using any Extension Methods for a number of reasons.

在大多数情况下,我已经开始不使用任何扩展方法,原因有很多。

Some of the reasons I stopped using them are noted in Scott's blog link above, such as "Think twice before extending types you don't own". If you have no control over the source for the types you are extending, you may encounter issues/collisions in the future if the source type has some additions/changes, such as moving your project to a newer .NET version. If the newer .NET version includes a method on the type of the same name as your extension, someone is going to get clobbered.

上面Scott的博客链接中提到了我停止使用它们的一些原因,比如“在扩展你不拥有的类型之前要三思”。如果您无法控制正在扩展的类型的源,那么如果源类型有一些添加/更改(例如将项目移动到新的. net版本),您将来可能会遇到问题/冲突。如果新的。net版本包含与扩展名类型相同的方法,那么将会有人遭受失败。

The main reason why I stopped using Extension Methods is that you can't quickly tell from reading the code where the source of the method is and who "owns" it.

我停止使用扩展方法的主要原因是,您不能从读取方法源代码的位置和谁“拥有”该方法的代码中快速判断。

When just reading through the code you can't tell whether the method is an extension or just a standard NET API method on the type.

在读取代码时,您无法判断该方法是否为扩展,或者只是类型上的标准NET API方法。

The intellisense menu can get really messy really fast.

智能感知菜单会很快变得非常混乱。