Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/445061543638859776
Post Reopened by CommunityBot, Doc Brown, ChrisF
added 1763 characters in body; edited title
Source Link
Robbie Dee
  • 9.8k
  • 2
  • 26
  • 54

Should utility base classes be avoided

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.

Should base classes be avoided

Knowing that C# doesn't support multiple inheritance, is it considered bad form to write a 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

Should utility base classes be avoided

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.

Post Closed as "Needs details or clarity" by Bobson, Doc Brown, user16764, Bart van Ingen Schenau, DougM
edited title
Link
Robbie Dee
  • 9.8k
  • 2
  • 26
  • 54

Should I care that I'm writing a base class when C# doesn't support multiple inheritance?classes be avoided

Source Link
Robbie Dee
  • 9.8k
  • 2
  • 26
  • 54

Should I care that I'm writing a base class when C# doesn't support multiple inheritance?

Knowing that C# doesn't support multiple inheritance, is it considered bad form to write a 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