简体   繁体   English

将wcf服务合同与接口定义分开是否有任何价值?

[英]Is there any value in separating wcf service contracts from your interface definition?

I have an interface ICustomerService: 我有一个接口ICustomerService:

public interface ICustomerService
{

  CustomerList FindAll();
}

and a concrete class implementing that interface. 以及实现该接口的具体类。 Now I need to expose the method over the web using wcf/rest and I've had to change the interface definition to this: 现在,我需要使用wcf / rest在Web上公开该方法,并且不得不将接口定义更改为:

[ServiceContract]
public interface ICustomerService
{
  [OperationContract]
  [WebInvoke(
   Method = "GET",
   UriTemplate = "Customers")]
  CustomerList FindAll();
}

My question is if there is any downside to having these attributes attached to your interface if there are clients that want to use the implementation using dll reference instead of using the rest api? 我的问题是,如果有客户端想要使用通过dll引用而不是使用其余api的实现,则将这些属性附加到您的接口是否有不利影响? I am aware of the shortcomings in using REST like having to have your parameters as type string if its in the uri. 我知道使用REST的缺点,例如如果在uri中必须将参数设置为类型字符串。

There should be no downsides of the attributes except maybe in code readability (if your clients have to look at your interface source). 该属性应该没有缺点,除非代码可读性强(如果您的客户端必须查看您的界面源)。

The attributes can be read by anyone interested (like the WCF framework) or will be ignored. 感兴趣的任何人(例如WCF框架)都可以读取该属性,或者将其忽略。 Actually they won't be visible from any implementing class (see this question ). 实际上,它们在任何实现类中都不可见(请参阅此问题 )。

At the architecture level however, consider using 2 interfaces, one for the dll-referencing clients and one for the REST clients. 但是,在体系结构级别,请考虑使用2个接口,一个用于dll引用客户端,一个用于REST客户端。 They might be similar the begin with, they might even share the same base interface and implementation, but you have the ability to make them divert from each other if the business case requires it. 一开始它们可能很相似,它们甚至可能共享相同的基本接口和实现,但是如果业务案例需要,您可以使它们彼此转移。
Also, this gives you the possibility to keep the WCF attribute filled interfaces in the WCF web application project, and the clean interfaces and implementations in a core class library project. 同样,这使您可以在WCF Web应用程序项目中保留WCF属性填充的接口,并在核心类库项目中保留干净的接口和实现。

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

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