简体   繁体   English

我应该更改为扩展方法(在ListItemCollection上)

[英]Should I change to Extension Methods (on ListItemCollection)

(I hope this isn't regarded as "too much like a discussion", but we'll see.) (我希望这不会被视为“太像讨论”,但我们会看到的。)

I have a few hours to refactor long-existing code. 我有几个小时来重构长期存在的代码。 I notice I have quite a number of static methods on ListItemCollection: 我注意到ListItemCollection上有很多静态方法:

public static ListItem ListItem_InsertIfNotPresent(ListItemCollection lic, ...)
public static ListItemCollection ListItems_Selected(ListItemCollection lic)
public static string[] ListItemValues(ListItemCollection lic)
public static void ListItems_SortByText(ListItemCollection lic)
....

So they look suitable to refactor. 因此,它们看起来很适合重构。 ListItemCollections are returned by many CLR functions, so I cannot derive/sub-class. ListItemCollections由许多CLR函数返回,因此我无法派生/子类。 I am toying with: 我在玩:

  1. Implement as extension methods. 实施为扩展方法。 Code currently does not have any extension methods. 代码当前没有任何扩展方法。 Not mad about having to add using ListItemCollectionExtensions to all my source files, nor risking future .NET CLRs possibly adding clashes. 不必为必须using ListItemCollectionExtensions添加到我的所有源文件而生气,也不必冒将来可能会添加冲突的.NET CLR的风险。 But the calling syntax is nice. 但是调用语法很好。
  2. Add a ListItemCollectionHelper class to put these in. Code will have to go var licx = new ListItemCollectionHelper(dropDownList.Items); 添加一个ListItemCollectionHelper类以将其放入。代码将必须为var licx = new ListItemCollectionHelper(dropDownList.Items); before any licx.SortByText(); 在任何licx.SortByText();之前licx.SortByText(); -type calls, which is a bit messy when I only need to call one method. 类型调用,当我只需要调用一个方法时,这有点混乱。
  3. Leave as-is, with named statics as now! 保持原样,并使用命名静态变量!

I have looked at articles like What are the best practices for using Extension Methods in .Net? 我看过类似.Net中使用扩展方法的最佳实践什么的文章 and others, but they don't really advise this case. 和其他,但他们并没有真正建议这种情况。 Of particular interest is your comments on #1 versus #2. 特别有趣的是您对第一和第二的评论。

I don't actually have to worry about "other users" of my code, but good practice never hurts. 实际上,我不必担心我的代码的“其他用户”,但是良好的实践永远不会有什么坏处。 For right or for wrong, I do not want to use LINQ or lambda solutions. 对还是错,我不想使用LINQ或lambda解决方案。

In your example I would leave it as is unless you need the code somewhere else because it is working and refactoring just to refactor might lead to bugs. 在您的示例中,除非您在其他地方需要代码,否则我将保留原样,因为它正在运行,并且仅重构的重构可能会导致错误。 However, if I was starting from scratch I would make these extension methods. 但是,如果我从头开始,我将使用这些扩展方法。 It makes the code easier to re-use and hopefully prevents someone from copying this code the next time this same pattern is needed. 它使代码更易于重用,并希望防止有人在下次需要相同模式时复制此代码。

I prefer Extension methods over the Helper class because it is generally easier to find the methods through Intellisense. 我更喜欢扩展方法而不是Helper类,因为通过Intellisense通常更容易找到方法。 In my experience any class that ends with the word Helper turns into a mess at some point. 以我的经验,任何以“ Helper”一词结尾的课程在某个时候都会变成一团糟。 That becomes the place where code that does not fit anywhere goes. 这就变成了不适合任何地方的代码的地方。 The Helper classes never start out bad but in a larger team you will soon find methods in there that have no business being there. Helper类从不会从坏开始,但是在一个更大的团队中,您很快就会在其中找到没有业务的方法。

I would go ahead and make them extension methods. 我会继续进行扩展。

  1. It doesn't break anything (you can still use like regular methods in the Helper pattern, so your existing code will still function) 它不会破坏任何内容(您仍可以像Helper模式中的常规方法那样使用,因此您现有的代码仍将起作用)
  2. When you do refactor existing code or write new code, you can use the cleaner extension method syntax. 当您重构现有代码或编写新代码时,可以使用更清洁的扩展方法语法。

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

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