I have a question regarding the approach used when constructing a set of base classes with slightly different behavior. In my experience, the reason you would use inheritance is to achieve code reuse and leverage the behavior that exists in the base class while adding some new functionality (of value) in the derived class. However, there has been a lot of talk in this forum about shying away from inheritance unless it is clear that this is true - I get that.
Where my confusion comes from is the contradiction that I see in the core .NET framework and the approach Rocky has taken with regards to collection classes.
As I see it, you have four basic types of collections:
Given this explanation, a List is simply a Collection that adds indexing and a KeyedCollection is a Collection that adds support for keys. (Likewise, a KeyedList would be a List with support for keys). A Dictionary is out there on its own except that it is a Collection whose items are Key/Value pairs instead of the actual object itself.
For the Dictionary, it is clear that inheritance is not appropriate as most of the Collection class' methods would have to be overridden to support the key/value nature of the Dictionary. So, very little code reuse and/or behavior is being preserved. However, in the case of the Collection->List->KeyedList (let's say), I can see how all of the behavior in the Collection class is reused by the List class with the value added indexing capability. And likewise with the KeyedList to the List class where you would need to override the Add() method but still delegate to the base class functionality to actually add the item once the key was pulled out and stored.
In CSLA, inheritance is used by having the ReadOnlyListBase inherit ReadOnlyBindingList. I am curious why BusinessListBase does not inherit ReadOnlyListBase and add write capability? Isn't this a candidate for inheritance based on the same concepts? It would leverage the behavior that already exists in ReadOnlyListBase but add new behavior such as Add(), Insert(), Clear(), Remove(), etc. Can anyone explain the rationale behind not inheriting?
To further confuse me, Microsoft almost never uses inheritance for the collection classes in the FCL. With the exception of System.Collections.ObjectModel.KeyedCollection<K,T>, System.Collections.Specialized.NameValueCollection and System.ComponentModel.BindingList<T>, none of the myriad of collection classes use inheritance. I'm thinking there is a ton of code duplicated amongst these objects and am struggling to understand why inheritance wouldn't be used here.
So, as I am working on my own classes, I am struggling with when it is and when it isn't appropriate to use inheritance in cases when it seems intuitive to me to do so. Any thoughts?
Thanks in advance.
SonOfPirate:In CSLA, inheritance is used by having the ReadOnlyListBase inherit ReadOnlyBindingList. I am curious why BusinessListBase does not inherit ReadOnlyListBase and add write capability? Isn't this a candidate for inheritance based on the same concepts? It would leverage the behavior that already exists in ReadOnlyListBase but add new behavior such as Add(), Insert(), Clear(), Remove(), etc. Can anyone explain the rationale behind not inheriting?
The main focus of ReadOnlyLists is to be lean and mean. This not only involves the list, but also the objects contained in the list (ReadOnlyBase sub-classes). The main driving force behing BusinessListBase and BusinessBase is all the fancy CSLA stuff: authorization, validation, CRUD, etc. etc. Two (highly) different scenario's, so there is your argument in favor of two distinct objects.
SonOfPirate:To further confuse me, Microsoft almost never uses inheritance for the collection classes in the FCL. With the exception of System.Collections.ObjectModel.KeyedCollection<K,T>, System.Collections.Specialized.NameValueCollection and System.ComponentModel.BindingList<T>, none of the myriad of collection classes use inheritance. I'm thinking there is a ton of code duplicated amongst these objects and am struggling to understand why inheritance wouldn't be used here.
Copyright (c) Marimer LLC