2011-04-07 64 views
5

我打算有一個核心模塊暴露接口,以便其他大型模塊(不同的客戶端)將通信。比如說,如果有方法組:WCF - 尋找最佳實踐時,有多個合同共享通用的方法

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

以暴露於一種類型的客戶機(模塊「X1」)和:

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

暴露給其他類型的客戶機(模塊「X2 「),並知道Method_AMethod_B應該有確切的實現...那麼我怎樣才能最好地設計服務架構(在服務和合同方面)?

是否有任何機會只實施Method_A和Method_B一次(在不同的合同實施中不是2次)?

使用WCF時,我將如何受益於接口繼承?

謝謝大家,請讓我知道如果我需要更清楚!

@marc_s ......我會很感激你的觀點...

回答

3

類可以繼承滿足的接口中的方法,所以你可以有一個IServiceBase接口和ServiceBase類,只是實現Method_AMethod_B ,然後將獨特的方法分離出單獨的接口,最後將它們組合在繼承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 ... 
    } 
} 
+0

這看起來非常好......謝謝......我會試一試並回復給您 – Learner 2011-04-07 15:40:26

+0

說實話,對於給定的情況,您的解決方案是好的。我只是試圖找出什麼解決方案是最好的和靈活的時候去真實世界的應用程序......你會如何比較你的解決方案marc的?無論你想到什麼想法......寫下來,謝謝! – Learner 2011-04-08 09:02:23

+0

我接受你的回答,因爲這是正確的,可能會幫助其他人。但我想要更多的東西的觀點,而不是解決方案。無論如何,謝謝。 – Learner 2011-05-03 14:35:21

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類,你可以通過絕對共同的類庫來分享兩個獨立的服務實現之間的代碼或任何你找到最好的方法您。

+0

嗨,馬克。非常好的和完整的答案...我真的很尊重你!我發現你的答案更加靈活(即使'A'和'B'方法不再普遍了)。你如何評論「C. Lawrence Wenham」的抽象課程?優點缺點? ... 謝謝! – Learner 2011-04-07 16:02:39

+0

不知何故,我喜歡這種繼承的想法...你選擇不使用繼承?你怎麼看? – Learner 2011-04-07 16:16:12