C#: Inheriting one interface to another that will act as a wcf interface

1k views Asked by At

I have asked some questions related to this but have still no conclusive answers so here is a more simple question.

Consider this. I have 2 interfaces. One describes a dao. One is a wcf interface. The wcf interface inherits the dao interface in order to expose its methods with wcf attributes. Because it is a wcf interface I must of course declare the methods again in order to place wcf attributes on them such as [OperationContract]. As a result I am in effect hiding the previous interfaces methods and must place the 'new' keyword on them. Is this the correct thing to do in this situation. I need to be able to present the dao interface methods via the wcf interface hence the reason for inheriting it. I want to force the wcf interface to have these functions exposed. It seems my hands are rather tied and that I must use this approach in order to get the methods exposed but what do you think?

In code terms it would look like this:

//The dao interface
public interface IMyDao
{
    void Foo();
}

//The wcf interface which implements/extends the IMyDao interface in order to add wcf attributes
[ServiceContract]
public interface IMyWCF : IMyDao
{
    [OperationContract]
    new void Foo();
}

//The dao class that implements the dao interface
public class MyDao : IMyDao
{
    public void Foo()
    {
        //Do something
    }
}

//The class implements the wcf interface. Makes calls to the dao.
public class MyWCF : IMyWCF
{
    public void Foo()
    {
        MyDao myDao = new MyDao();

        myDao.Foo();  
    }
}
1

There are 1 answers

1
BonyT On BEST ANSWER

Use 2 interfaces - don't share one interface for a DAO and a WCF contract

Include a wrapper that passes through the WCF service calls to the DAO.

So for example if your DAO has the following method:

public interface IMyDAO
{
    MyObject GetMyObjectById(int Id);
}

You'd have your Operation contract with the same signature:

public interface IMyWCFContract 
{
    [OperationContract]
    MyObject GetMyObjectById(int Id);
}

And then you'd have a wrapper class:

public class MyWCFService : IMyWCFContract
{
     private MyDAO dao;

     public MyWCFService()
     {
        dao = new MyDAO(); 
     }

     public MyObject GetMyObjectById(int id)
     {
        return dao.GetMyObjectById(id);
     }
}

An alternative plan may be to consider using a RESTFUL service instead.