Knowing that C# doesn't support multiple inheritance, is it considered bad form to write a utility base class?
Some initial thoughts:
- I could create an interface and a concretion, but then the implementation isn't portable
- Should the developer need to extend the class in say, a decorator pattern, they could implement this new class and inherit the base class in their new class
EDIT:
N.B. I'm not talking about vanilla inheritance here e.g. poodle<--dog<--mammal<--animal
More this sort of thing (lifted from here):
using log4net;
using log4net.Config;
public class LogTest2
{
private static readonly ILog logger =
LogManager.GetLogger(typeof(LogTest2));
static LogTest2()
{
DOMConfigurator.Configure();
}
static void Main(string[] args)
{
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
}
}
If I were to drop this into say, a WCF service, it would be expedient for the private member variable, constructor and perhaps some wrapper methods to sit in a base class to save cluttering up the service code (and make it reusable).
So far, so dull.
However:
What if I wanted to add some other sort of logging not supported by log4net? E.g. Acme Corp provide some class to iMessage the support team if an issue occurs.
Base class
Had I implemented the log4net stuff in a base class, I'd then have to create some new master base class that supports both classes and inherit this.
Interface
I could implement an interface but the concretion would still be in the service code which we're trying to avoid. Acme Corp's class can of course now drop straight in but we're implementing two sets of code which do very similar things in different ways.
Horse
with green fur does not mean it should inherit fromTree
. Both might haveLeafGreenBrush
s, though. You have to be very careful deciding what gets inherited. Think carefully about whether something really needs to inherit from a second parent. If the class is well designed and does one thing, it probably doesn't need to.