简体   繁体   English

WCF设计:避免接口重复

[英]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. 但是,替代方法似乎是在我的服务中使用WCF装饰的界面,这也感觉不对。

Example follows 示例如下

Here is the interface for my WCF Service 这是我的WCF服务的界面

[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? 此类是否也应实现IGetDataService,因为方法相同? Or should I have another interface, called for example, IDataRepository: 还是应该有另一个接口,例如IDataRepository:

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

Note that this interface is identical to IGetDataService in terms of signature. 请注意,就签名而言,此接口与IGetDataService相同。 The only difference is the lack of WCF related attributes. 唯一的区别是缺少WCF相关属性。

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): 至于WCF服务本身的实现(为了完整性):

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? 因此,总结一下:我需要IDataRepository还是IGetDataService在内部可重用?

I'm fairly confident both would work - interested in design best practice. 我非常有信心两者都能奏效-对设计最佳实践感兴趣。

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. 例如,如果您的WCF服务需要公开一项新服务,那么您也不必更改存储库界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM