0
我已经提出了一些与此相关的问题,但仍然没有确定的答案,所以这里是一个更简单的问题。C#:将一个接口继承到另一个将充当wcf接口的接口
请考虑这一点。我有2个接口。一个描述了一个道。一个是wcf接口。 wcf接口继承了dao接口,以便使用wcf属性公开其方法。因为它是一个wcf接口,我当然必须再次声明这些方法,以便在它们上面放置wcf属性,例如[OperationContract]。因此,我实际上隐藏了以前的接口方法,并且必须在其上放置'new'关键字。这是在这种情况下做的正确的事情。我需要能够通过wcf接口呈现dao接口方法,因此也就是继承它的原因。我想强制wcf接口将这些功能暴露出来。看起来我的手是相当捆绑的,我必须使用这种方法才能让方法暴露出来,但是你怎么看?
在代码方面,它应该是这样的:
//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();
}
}
不确定你的意思。我使用两个接口。一个描述dao对象,另一个继承这个以便通过wcf公开dao功能。它基本上是迫使wcf公开所有dao方法作为操作契约的一种方式。 – CSharpened 2012-07-27 09:51:54
+1。比继承更喜欢构图。 – jeroenh 2012-07-27 09:56:42