为什么没有 ObservableKeyedCollection在.NET框架中?
.NET Framework 从 3.0 版开始包含 ObservableCollection
好吧,我可以通过派生 KeyedCollection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有 ObservableKeyedCollection (或任何其他此类类型,只是其他泛型类型的组合)的原因是因为 ObservableCollection 是通用的,这使得“ObservableKeyedCollection”的实现像这样简单:
虽然这肯定不是一个单行程序,但其中大部分都是样板文件。最重要的是,它不会像您一样重新实现 ObservableCollection正在建议;相反,它充分利用了它。
它“不是 .NET Framework 的一个好的补充”的原因是,当已经有一种方法可以做某事时,创建另一种方法来完成某件事是一个坏主意。完成某项特定任务的方法越少,做得不好的方法就越少。 8)
工具已经提供了,现在就看你如何使用它们了。
希望有帮助!
The reason that there is no ObservableKeyedCollection (or any other such type which is merely a combination of other generic types) is because ObservableCollection is generic, and that makes implementation of an "ObservableKeyedCollection" as easy as this:
While that is certainly not a one-liner program, most of it is boilerplate. Most importantly, it doesn't re-implement the ObservableCollection as you were suggesting; instead it fully utilizes it.
The reason that it "whouldn't be a good addition to the .NET Framework" is because when there's already one way to do something, creating another way to do it is a bad idea. The fewer ways there are to get some particular task done, the fewer ways there are to do it poorly. 8 )
The tools are provided, it's now all about how you use them.
Hope that helps!
请查看 ObservableKeyedCollection 类实现。这很容易。
Please take a look at the ObservableKeyedCollection class implementation. It's pretty easy.
我建议您查看 C5。它是一个很棒的通用集合库,提供可观察集合作为其所有集合的标准,包括
Added
、Inserted
、Removed
、已删除
、已清除
和已更改
。此外,C5 系列都拥护“接口编程”理念。所有接口都提供了底层实现的完整功能,这是System.Collections.Generic
命名空间所缺少的。此外,还有完整的文档。我强烈建议您检查一下。I'd recommend you take a look at C5. It is a wonderful generic collection library that offers observable collections as standard for all of its collections, including
Added
,Inserted
,Removed
,RemovedAt
,Cleared
, andChanged
. Additionally, the C5 collections all espouse the "programming to interface" ideal. All of the interfaces provides expose the full functionality of the underlying implementations—which is lacking in theSystem.Collections.Generic
namespace. Additionally, there is thorough documentation. I highly encourage you to check it out.