Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wcf

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 implementations)?

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

like image 255
Learner Avatar asked Apr 07 '11 15:04

Learner


1 Answers

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 ...
    }
}
like image 102
Chris Wenham Avatar answered Oct 13 '22 10:10

Chris Wenham