If collection of business objects inherits from Collection <T>, when does it not extend it?

I have a collection of business objects (representing data from a database) that inherits from Collection and has a static method that calls the stored proc and then populates its properties with the returned data.

My question is: is it wrong to inherit from Collection since it really does not extend the class? or would it be better not to inherit anything, but instead to save a private variable of type Collection?

+3
source share
4 answers

It is difficult to answer this without seeing much more details about what you are trying to do. However, I would usually not inherit from Collection, as it actually does not provide enough virtual methods to override. This may change the behavior of your class a little more complicated in the future.

The problem with inheritance is that if you use it, you will depend on the behavior of the Collection base class forever or face complex refactoring. For example, you might want to add filtering, searching, and sorting operations, after which you will need to switch to “List from collection”. You might also want to bind data where you need a BindingList instead of a collection.

ICollection IList <T> . , . , .

, , , - , , , , , .

PS: IList <T> , true IsReadOnly, NotSupportedException , .

+2

-, , Collection<T>. , , - - T.

Udate:
, , . .

+2

, "" -, . - , . , , " " , , - .

. , "" . (: "" "" , . ).

0

, . , . , , . Blah(). Foo(). JIT .

In addition, I think that expanding the collection is 99% wrong :) Usually, when people expand the collection, what they really need to do is simply make a list and display only what customers really need or just use a simple list.

0
source

Source: https://habr.com/ru/post/1705852/


All Articles