简体   繁体   English

框架类型的扩展方法的用法

[英]Usage of extension methods for framework types

In a new line of work I have been told to avoid using Extension Methods for Types that you (or your organization) have no control over, meaning external libraries, framework types such as string, list, and others. 在新工作中,我被告知要避免对您(或您的组织)无法控制的类型使用扩展方法,这意味着外部库,框架类型(如字符串,列表等)。

The argument I was given is that this will be bad if the framework developer now decides to implement a method that has the same names and/or parameters as your extension method. 我得到的论据是,如果框架开发人员现在决定实现与扩展方法具有相同名称和/或参数的方法,那将是不好的。

While this problem may arise, this argument effectively reduces the usability of extension methods to nearly zero. 尽管可能会出现此问题,但此参数有效地将扩展方法的可用性降低到几乎为零。 Would this argument be considered valid? 这个论点会被认为是有效的吗? I am not suggesting to use extension methods everywhere, but I would like to know of similar arguments for and against it. 我不建议在任何地方都使用扩展方法,但是我想知道支持和反对它的类似论点。

It's an argument which has some merit, but in lots of cases the problems can be avoided: 这是一个有一定优点的论点,但是在很多情况下,可以避免这些问题:

  • If you control your code and can easily update it if necessary, then you can easily write unit tests to detect the problem and correct it if it occurs. 如果您控制代码并可以根据需要轻松地对其进行更新,则可以轻松编写单元测试以检测问题并在出现问题时进行纠正。 (I assume you'd be validating any update to the external library before deploying it anyway.) (我认为您无论如何都要先对外部库进行任何更新,然后再进行部署。)
  • If you trust the external library developer to follow normal good practices for backward compatibility, then they shouldn't be adding members to interfaces anyway, as that would break existing implementations... so you could at least write extension methods for interfaces. 如果您信任外部库开发人员遵循向后兼容的常规良好做法,那么他们无论如何都不应在接口中添加成员,因为那样会破坏现有的实现方式,因此您至少可以为接口编写扩展方法。
  • If your extension methods have names which would be very unlikely to be added to the external libraries, then practically speaking it's not an issue. 如果您的扩展方法有这将是不太可能的名字被添加到外部库,然后实际来看这不是一个问题。 For example, if your company writes Frobulators, and that's a term which is specific to you, then writing 例如,如果您的公司写的是Frobulators,而这是您特有的术语,那么请写

     public static Frobulator ToFrobulator(this string Frobulator) 

    really isn't going to be a problem in reality. 实际上,这实际上不会成为问题。

Of course, the risk is there, but defining something on closed or sealed types you have no control over is exactly what extension methods is about. 当然,存在风险,但是在您无法控制的封闭或sealed类型上定义某些东西正是扩展方法的本质。 If you'd only create extension methods on types from your own, the effectiveness of extension methods (compared to regular methods) would be minimized. 如果只对自己的类型创建扩展方法,则扩展方法的有效性(与常规方法相比)将降至最低。

There is a very easy 'solution' for this in naming conventions. 在命名约定中有一个非常简单的“解决方案”。 If you'd prefix or postfix your extensions with a specific identifier, you will be sure Microsoft doesn't create a similar method. 如果使用特定标识符在扩展名前添加前缀或后缀,则可以确保Microsoft不会创建类似的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM