GinjaNinja
GinjaNinja

Reputation: 806

WCF Design: Avoiding Interface Duplication

I'm finding myself drawn to using two identical interfaces in this scenario which doesn't seem right. However the alternative appears to be to use a WCF decorated interface from within my service which also feels wrong.

Example follows

Here is the interface for my WCF Service

[ServiceContract(Namespace="http://MyNameSpace")]
public interface IGetDataService
{
    [OperationContract]
    List<MyDataObject> GetData();
}

Now, lets say the service only gets data from the database. I need an implementation to facilitate this retrieval. Should this class also implement IGetDataService as the methods are identical? Or should I have another interface, called for example, IDataRepository:

public interface IDataRepository
{
    List<MyDataObject> GetData();
}

Note that this interface is identical to IGetDataService in terms of signature. The only difference is the lack of WCF related attributes.

class DatabaseDataRepository: IDataRepository
{
    public List<MyDataObject> GetData()
    {
        //  code to query database here
        //  return populated List<MyDataObject>
    }
}

And as for the implementation of the WCF service itself (for completeness):

class DataService: IGetDataService
{
    public List<MyDataObject> GetData()
    {
        var repository = MyIocContainer.GetInstance<IDataRepository>();
        return repository.GetData();
    }
}

So, to summarise: do I need IDataRepository or is IGetDataService reusable internally?

I'm fairly confident both would work - interested in design best practice.

Upvotes: 0

Views: 50

Answers (1)

Derek Van Cuyk
Derek Van Cuyk

Reputation: 953

It's really a matter of preference.

I would use two different interfaces. It's a little more overhead but it allows the two contracts to flex in different directions.

For example, if your WCF service needs to expose a new service, you don't have to change your repository interface as well.

Upvotes: 1

Related Questions