c# - Is it good practice to return an IEnumerable<T> for a collection that implements INotifyCollectionChanged -
Writing another question for SO, I came across a pattern that I often use and in fact I have never been reflected in it ever. But now, I am not sure that this is the right way:
If I have a collection that is tied to my WPF-controls, then I almost always IEnumerable & lt; SomeType & gt;
. Although internally, in most cases a ReadOnlyObservableCollection & lt; SomeType & gt;
. I had no problem with this and all consumer controls are always updated correctly, which is not surprising because they check for INotifyCollectionChanged
-interface
but my question Now, if this signature is only in the IEnumerable & lt; SomeType & gt;
is bad behavior to declare, but to return to something more powerful (even more dependent), INotifyCollectionChanged
).
Update:
I try to clarify:
My main purpose is a IEnumerable & lt; SomeType & gt; Return
. But most of the time, returned IEnumerable & lt; SomeType & gt;
implies INotifyCollectionChanged
such as ReadOnlyObservableCollection & lt; SomeType & gt;
Bind controls consuming accordingly (what is my second intention).
Maybe I should have asked: Is there an interface in which actually IEnumerable and INotifyPropertyChanged
Remember, This (indirectly) does, and also If you are creating your own custom archive, and are planning to use it with data binding, then I will call it (even more dependent) However, this is the best utility, both codes, based on an interface which It is not part of the API, it is bad behavior. It is dangerous, and instead of the concrete type instead of the reason for returning an interface is part of the process that allows the implementation to be changed later, by putting dependence on an interface which is not declared, you are making your code fragile. It is being said that whatever I try, I do it, but it is not "difficult" dependence. Instead, I have the IEnumerable & lt; T & gt; Both are interfaces, they are not a definite type, you can design your concrete class to implement both, in which there is no problem. Your API method may return the appropriate interface for the call. In fact, there is nothing good to avoid - a good design.
ILIT and LT; T & gt;
as well as other interfaces. / P> IEnumerable & lt; T & gt;
(or possibly IList & lt; T & gt;
, if appropriate) and INotifyCollectionChanged
.
IEnumerable & lt; T & gt;
, and check INotifyCollectionChanged
- take advantage of it if you use it, however, if the "secondary" interface is not present, then I work with code Allow me to