WCF - 寻找多个契约共享公共方法的最佳实践

5

我打算创建一个核心模块,暴露接口以便其他大模块(不同的客户端)进行通信。比如说,如果有一组方法:

void Method_A();
void Method_B();
void Method_X1();

将其暴露给一种类型的客户端(模块“X1”)并:

void Method_A();
void Method_B();
void Method_X2();

如果要向其他类型的客户端(模块“X2”)公开,并且知道Method_AMethod_B应具有相同的实现...那么我应该如何最好地设计服务架构(以服务和合同为衡量标准)?

是否有可能仅实现一次Method_AMethod_B(而不是在不同的合同实现中实现两次)?

在使用WCF时,如何受益于接口继承?

提前感谢您所有人,并请让我知道是否需要更清楚!

@marc_s...我真的很感激您的观点...

2个回答

3

类可以继承满足接口的方法,因此您可以拥有一个 IServiceBase 接口和一个只实现 Method_AMethod_BServiceBase 类,然后将独特的方法分离到单独的接口中,最后将它们组合在继承 ServiceBase 并实现 Interface1 或 Interface2 的类中。例如:

[ServiceContract]
public interface IServiceBase
{
    [OperationContract]
    void Method_A();

    [OperationContract]
    void Method_B();
}

[ServiceContract]
public interface IService1 : IServiceBase
{
    [OperationContract]
    void Method_X1();
}

[ServiceContract]
public interface IService2 : IServiceBase
{
    [OperationContract]
    void Method_X2();
}

public abstract class ServiceBase : IServiceBase
{
    void Method_A()
    {
        ... implementation here ...
    }

    void Method_B()
    {
        ... implementation here ...
    }
}

public class Service1 : ServiceBase, IService1
{
    void Method_X1()
    {
        ... implementation here ...
    }
}

public class Service2 : ServiceBase, IService2
{
    void Method_X2()
    {
        ... implementation here ...
    }
}

这看起来非常不错...谢谢...我会试一试并回来告诉你。 - Learner
说实话,对于给定的场景,你的解决方案不错。我只是想找出在实际应用中哪种解决方案最好、最灵活... 你会如何比较你的解决方案和马克的解决方案?无论你心里想什么都写下来,谢谢! - Learner
我接受你的答案,因为它是正确的,可能会帮助其他人。但是我更想要一个关于事情的观点,而不是一个解决方案。无论如何,谢谢。 - Learner

2

我是一个有用的助手,可以为您进行翻译。

我被召唤了!?!?!:-)

如果你只有一个接口

public interface IServiceA
{
  void Method_A();
  void Method_B();
  void Method_X1();
}

还有第二个

public interface IServiceB
{
  void Method_A();
  void Method_B();
  void Method_X2();
}

你完全可以在服务器端共享两种常见方法的实现代码。

你需要在服务器上创建两个类MethodAHandlerMethodBHandler,它们位于通用类库中(或者在两个单独的通用程序集中),然后你可以使用以下代码:

using MethodHandlers;  // contains the two method handlers

public class ServiceA : IServiceA
{
   public void Method_A()
   {
       MethodAHandler hnd = new MethodAHandler();
       hnd.HandleMethodCall();
   }

   public void Method_B()
   {
       MethodBHandler hnd = new MethodBHandler();
       hnd.HandleMethodCall();
   }

   public void Method_X1()
   {
       // handle method X1 call here or delegate to another handler class
   }
}

而对于第二项服务:
using MethodHandlers;  // contains the two method handlers

public class ServiceB : IServiceB
{
   public void Method_A()
   {
       MethodAHandler hnd = new MethodAHandler();
       hnd.HandleMethodCall();
   }

   public void Method_B()
   {
       MethodBHandler hnd = new MethodBHandler();
       hnd.HandleMethodCall();
   }

   public void Method_X2()
   {
       // handle method X2 call here or delegate to another handler class
   }
}

在服务器端,您有.NET类,可以通过共享公共类库或其他最适合您的方法来绝对共享两个不同服务实现之间的代码。


嗨,马克。非常好而且完整的回答...我真的很尊重你!我发现你的回答更加灵活(即使'A'和'B'方法不再普遍)。您如何评论"C. Lawrence Wenham"使用抽象类的方法?优缺点呢?...谢谢! - Learner
不知怎么的,我很喜欢继承这个概念...你选择不使用继承吗?你有什么想法? - Learner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接