开发者

WCF - looking for best practice when having multiple contracts sharing common methods

开发者 https://www.devze.com 2023-02-22 15:09 出处:网络
I intend to have one core module exposing interfaces so that other big modules (different clients) will communicate with. If, say, there are group of methods:

I intend to have one core module exposing interfaces so that other big modules (different clients) will communicate with. If, say, there are group of methods:

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

to expose to one type of clients (module "X1") and:

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

to expose to other type of clients (module "X2") and knowing that Method_A and Method_B should have the exact implementation ... then how can I best design the service(s) architecture (in terms of services and contracts) ?

Is there any chance to implement Method_A and Method_B only once (not 2 times in different contract implem开发者_如何学JAVAentations)?

How shall I benefit of interface inheritance when using WCF ?

Thank you all in advance and please let me know if I need to make it more clear!

@marc_s... I would really appreciate your point of view...


Classes can inherit the methods that satisfy an interface, so you could have a IServiceBase interface and ServiceBase class that just implements Method_A and Method_B, then single out the unique methods into separate interfaces, finally combining them together in classes that inherit ServiceBase and implement either Interface1 or Interface2. eg:

[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 ...
    }
}


I've been called!?!?! :-)

If you have one interface

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

and a second one

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

you can absolutely share the implementation code for the two common methods on the server side.

You'd create two classes MethodAHandler and MethodBHandler on your server, in a common class library (or in two separate, common assemblies), and then you could use something like:

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
   }
}

and for the second service:

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
   }
}

On the server side, you have .NET classes and you can absolutely share code between two separate service implementation by means of a common class library or whatever you find the best approach for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消