添加扩展方法我认为应该在框架中:坏主意?
我希望 .NET 框架中有一项特定功能,但事实并非如此。我希望有一个 DBDataReader.GetString(或 GetDateTime、GetInt32 等)重载接受字段名称而不是索引。在我看来,使用字段名称可以更轻松地进行维护,但 GetXxx 方法仅接受字段位置。
我可以通过使用扩展方法将这些添加到我的项目中(至少以有限的能力满足我的即时需求),但感觉有些不对劲。除了我必须为每个新项目重复此过程这一明显问题之外,还有什么理由不做我正在考虑的事情呢?
例如,我是否会陷入试图重写框架以适应我的突发奇想的滑坡?我会让那个必须修改我的代码的可怜人(可能是我)摸不着头脑试图弄清楚发生了什么吗?我是否违反了 OOP 的某些核心原则?
只是寻找指导和观点。谢谢。
There's one specific feature that I wish was in the .NET framework, but isn't. I wish there were a DBDataReader.GetString (or GetDateTime, GetInt32, etc.) overload that accepted the field name instead of the index. Using the field name makes for easier maintenance IMO, but the GetXxx methods only accept the field position.
I can add these to my project (at least in a limited capacity to suit my immediate needs) by using extension methods, but something about that feels wrong. Beyond the obvious problem that I'd have to repeat this process for every new project, is there any reason not to do what I'm thinking about?
For example, am I headed down a slippery slope of trying to rewrite the framework to suit my whims? Will I leave the poor guy who has to modify my code (probably me) scratching his head trying to figure out what's going on? Am I violating some core principle of OOP?
Just looking for guidance and viewpoints. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这正是扩展方法能够解决的场景之一。您需要的功能在您无法控制的类型上尚不可用。
至于在每个项目中复制代码,您需要做的是创建一个类库项目来存储像这样的公共/共享代码,然后在每个解决方案中包含该项目并使用项目引用或编译项目,将生成的程序集放在共享位置并将其作为文件项目引用包含在内。
请记住,扩展方法仅在“纳入范围”时才可用。这意味着,如果您将扩展方法定义为:
除非代码文件包含带有 using 指令的 CustomExtensions 命名空间,否则它将在任何字符串值上都不可见。
因此,建议您不要将自己的扩展放入 .NET Framework 定义的命名空间中,而应使用自己的命名空间。这样做可以最大程度地减少混乱的可能性,因为除非明确将扩展纳入范围,否则您的扩展将不可用。
另外,请记住,如果您提供的扩展方法与“内置”方法完全相同的签名,则您的扩展方法将永远可见或可调用 -内置方法始终优先。如果 Microsoft 决定在框架的更高版本中包含该功能并使用您选择的完全相同的签名,那么您的扩展方法将不再被调用。
当您定义扩展方法时,您实际上并没有重新定义要扩展的基础类型,因此您没有自己的“单独版本”。扩展方法提供的只是一种在静态类中调用静态方法的方便且更自然的方法。事实上,使用我上面的示例扩展方法,这些是等效的调用:
虽然 Microsoft 确实花了很多心思来考虑框架中包含或不包含的内容,但他们无法了解可能驱动的特定用例您对某些功能的特殊需求。是的,大多数时候他们做得很好并提供“开箱即用”的功能,但有时却不然。 (一个很好的例子就是缺少 InvariantToString() 方法。)
This is exactly one of the scenarios extension methods are able to solve. You need functionality that isn't already available on a type over which you have no control.
As far as duplicating the code in each of your projects, what you need to do is create a class library project to store your common/shared code like this and then either include that project in each Solution and use project references or compile the project, put the resulting assembly in a shared location and include it as a file project reference.
Remeber that extension methods are only available when they are "brought in to scope". This means that if you have an extension method defined as:
It will not be visible on any string values unless the code file includes the CustomExtensions namespace with a using directive.
For this reason, it is recommended that you do not put your own extensions in a .NET Framework defined namespace but rather use your own namespace. Doing that minimizes the potential for confusion because your extension won't be available unless it is explicitly brought in to scope.
Also, keep in mind that if you provide an extension method with the exact same signature as a "built-in" method, your extension method will never be visible or callable - the built-in method always takes precedence. If Microsoft decides to include that functionality in a later version of the Framework and uses the exact same signature you chose then your extension method will no longer be called.
When you define an extension method you aren't actually redefining the underlying type being extended so you don't have your own "individual versions". All an extension method provides is a convenient and more natural way to call a static method in a static class. In fact, using my example extension method above, these are equivalent calls:
While Microsoft does put a lot of thought in to what to include or not include in the Framework, they have no way of knowing about specific use-cases that might be driving your particular need for certain functionality. Yes, most of the time they do a great job and provide functionality "out of the box" but other times they don't. (A good example is missing an InvariantToString() method.)
实际上,我认为 .NET 做得相当好:要使扩展方法起作用,您必须使用它们定义的名称空间。Visual Studio 将向您显示该方法来自何处等。
无论如何,如果您找到抽象这使您的代码更易于阅读/维护,然后就去做吧!
Actually, I think .NET did it rather nicely: For extension methods to work, you have to be using the namespace they were defined in. Visual Studio will show you where the method came from etc.
By all means, if you find an abstraction that makes your code easier to read/maintain, then go for it!
考虑以下问题的答案,然后继续创建您的扩展方法。让您的工作更轻松是扩展方法成为该语言一部分的原因。
Think about the answers to the following issues, then go ahead and create your extension method. Making your work easier is the reason extension methods are part of the language.
扩展是语法糖。本质上,您所做的就是创建一些静态方法,就像任何其他静态方法一样......除了一个例外,它们可以为您节省更多的打字时间。
也就是说,如果您(和您的团队,如果您有的话!)确定某些扩展方法可以节省您(和您的团队)的时间和精力,那么就添加它们!
这个想法的注意事项:
Extensions are syntactic sugar. Essentially all you're doing is creating some static methods just like any other static method out there... with one exception, they save you a little more typing.
That said, if you (and your team if you have one!) determine that some Extension methods would save you (and your team) time and effort, then add them!
Caveats to this idea:
那么,在 Java 中,选项是下载并编译您自己的 JRE。或者通过继承来扩展类。现在重新考虑一下,扩展方法不是您可以在您的情况下使用的最佳方法吗?
我还建议创建您自己的“MyNiftyLibrary”来存储此代码。这是我存储所有“StringHelper”、“...Helper”代码的地方。将其视为您的个人 .NET 插件。
Well, in Java the option would be to download and compile your own JRE. Or extend the class through inheritance. Now reconsider, aren't Extentension Methods the best you could use in your situation?
I also would recommend creating your own "MyNiftyLibrary" to store this code into. It's the place where I store all my "StringHelper", "...Helper" code. Think of it as your personal .NET AddOn.