views:

276

answers:

2

I have a situation which I think I need the functionality provided by an interface and an abstract class.

I am creating models to be used in a simulation. Each model is contained in a C# class that implements an IModel interface (of my design so it can be modified). I also created a c++ unmanaged Win32 DLL that acts as a bridge between my C# model classes and the simulation environment. The model classes are called via COM, hence the requirement for an interface.

I have just received a new requirement that these model classes should be callable via a web service independently of the simulation environment. Now these models are rather sensitive, and the client has requested that they be configured by the client and executed on the server*. I figured this would be possible if I made the model classes inherit from an abstract class, then created a web service method on the server:

public AbstractModel executeModel(AbstractModel modelForExecution)

that receives a model object from the client, executes it and populates it with results.

BUT - I cant use an abstract class because the models require an interface for the COM implementation.

How can I reconcile these two requirements? I've been told that it is possible to have a class implement an interface AND have that class derive from an abstract class, but I cant find any examples of that. If its possible, what is the syntax? If its not possible, what else should I do?

*the model classes actually call an ancient fortran executable, so the model configuration of the classes does not give away too much information about how the model works.

+5  A: 
public abstract class Base{}
public interface IBase{}
public Something: Base, IBase{} 
// the interface needs to go after the base class
eglasius
+1  A: 

I would try having your abstract class implement the Interface (and have the method as abstract). Then each implementation just subclasses the abstract class. I think that should work.

public interface IModel {
  void doSomething();
}

public abstract class AbstractModel : IModel {
  public abstract void doSomething();
}

public class MyModel : AbstractModel {
  public void doSomething() {
    // code goes here
  }
}

You might have to use the 'overrides' keyword on the implementation method (the compiler will tell you).

Travis
The only problem that might occur with this is calling the derived class via COM from my unmanaged class. It might work, but I think the other solution is simpler at this point. Thanks for your response.
Alex