Problem: .NET framework classes don’t use interfaces enough
Specific example: DataSource
/ DataBind()
are separately defined on Repeater
and GridView
, (and Control
, and many, many others), and my abstract base class doesn’t care which option an implementor chooses, it just wants to bind the data however the base control wants it.
Possible Solutions:
- Define interface
IDataBinding
, classMyRepeater : Repeater, IDataBinding
, classMyGridView : GridView, IDataBinding
, etc. Define interfacenope, extension methods can’t do thisIDataBinding
, upgrade to C# 3, use extension methods to addIDataBinding
- Copy/paste identical code from my base class into concrete classes
- Use reflection to set
DataSource
and callDataBind()
, completely sidestepping the C# type system
Problem: .NET framework classes not designed for extension: “cannot override inherited member 'System.Collections.Generic.Dictionary<X, Y>.Add(X, Y)' because it is not marked virtual, abstract, or override
”
Specific Example: Extending System.Collections.Generic.Dictionary
to do some permit a list of values for one key, so adding the first item is stored as a single value, but adding a second value to the same key stores both values in a list. Error message is: “cannot override inherited member 'System.Collections.Generic.Dictionary<X, Y>.Add(X, Y)' because it is not marked virtual, abstract, or override
”
Possible Solutions:
- Define wrapper classes that encapsulates the framework class and implements all the base interfaces, with the vast majority of the code being straight delegation to the framework class:
class MyDict : IDictionary, [other interfaces...] { private Dictionary<X,Y> dict = new Dictionary<X,Y>; public bool Contains(X key){ return dict.Contains(key);} [... other simple wrappers...] }
- Find a less appropriate framework class that is designed for extension, duplicate behavior of the proper framework class manually, eg: add run-time type checks for
Hashtable.Add(object, object)
in place of the letting the compiler handle the types as inDictionary<X,Y>.Add(X, Y)
- Extend Dictionary<X,Y>, define a method
AddList(X,Y)
, and avoid usingIDictionary
in the rest of my code - Upgrade the C# 3, use extension to add function
AddList
toIDictionary
, be sure to include those extensions on every consumer ofIDictionary
Problem: .NET framwork classes not designed for extension: members declared private/ internal / protected internal
Specific Example: Storing additional data in ViewState
on 2 different controls that have different base classes. One is a user control, another extends RadioButtonList
to provide different UI for the same data
Possible solutions:
Upgrade to C# 3, use extension methods to add functionsnope, extension methods can only see public members,ViewState
is protected- Use reflection to set
ViewState
- Copy/paste code into each control
- Define interface
IPublicViewstate
, classMyRadioButtonList : RadioButtonList, IPublicViewState
, classMyUserControl : UserControl, IPublicViewState
, etc
As Nathan said, I’m stuck between a rock and IHardPlace
.
One Comment