Polymorphism and API design


I want to build an event logging class that allows a client to log messages. Windows Event Log and Xml files are the initial storage types I'd like to provide first, adding Database storage later. My question is regarding building an installer class that would prepare the chosen log type. Ideally I would like the developer using the API to be able to do this:

LogInstaller objLogInst = new LogInstaller( LogType.Xml | LogType.EventLog /*[Flags]enum ... */ );
objLogInst.Install();


The missing piece here is, of course, the parameters needed for each type of install (e.g. FilePath, AppName, etc...). I know I'll need an installation class for each type of logging the client will want to install (e.g. Xml, EventLog, etc...). I'll also need a specific implementation of the install() method for each class. I am hoping there is some programming concept that will fit well into this design scenario.

So far, all the options I've considered suffer from a differing method signature. Options I've considered:

1) Use an interface. Define an interface that requires each derived installer class to implement an install() method. I don't think this would work since the install() method signatures will most likely differ.

2) Use an abstract base class. Define an ABC that implements an abstract install method. This allows the base class to version well going forward by not breaking client code, but again the signature problem arises.

3) Forget polymorphism altogether and have no inheritance chain.


Any suggestions are appreciated.

Regards,
Andrew

[1632 byte] By [Andrew-L] at [2007-12-26]
# 1

Andrew,

interface-based programming together with dependency injection will solve your pain. Here you have a couple of articles based on that

Hope it helps

DiegoDagum at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...