首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 實戰 (7) Part I:SOA 與 OOP 融合之道 — 服務端 Contract 繼承與實作複用架構設計

     編輯:關於.NET

一、SOA 與 OOP 的定位差異

  • OOP:關注實體屬性與行為封裝,細粒度、狀態化、緊耦合 (繼承/依賴)。
  • SOA:關注業務功能實現,粗粒度、無狀態、自治、鬆耦合 (Contract/Message)。
  • 結論:OOP 適用於程式設計層面;SOA 適用於系統架構層面。WCF 扮演「適配器」角色,將 OO 實現映射為 SOA 契約。

二、WCF 對 OO 特性的支援:繼承、多型、多載

本系列探討 WCF 如何在契約層面實現 OO 語意:

  • Part I (本文):服務端 Contract 繼承與實作類別繼承複用
  • Part II:客戶端 Proxy 繼承架構重構
  • 前文 (5):Operation 多載 — 透過 OperationContract(Name=...) 映射同名方法。

三、案例:天氣預報服務漸進式擴充

專案結構 (4 專案)

Solution 結構

  • BusinessEntity:BasicWhetherInfo、WindInfo 及 Enum
  • Service:Contract 定義與實作
  • Hosting (IIS):.svc + Web.config
  • Client:獨立呼叫端

1. Data Contract (BusinessEntity)

[DataContract] [KnownType(typeof(WhetherConditions))]
public class BasicWhetherInfo
{
    [DataMember] public WhetherConditions Condition { get; set; }
    [DataMember] public double Temperature { get; set; }
    public override string ToString() => $"Conditions: {Condition}; Temperature: {Temperature}";
}

public enum WhetherConditions { Clear, Cloudy, Overcast, Rainy }

[DataContract] [KnownType(typeof(WindDirection))]
public class WindInfo
{
    [DataMember] public WindDirection Direction { get; set; }
    [DataMember] public string Speed { get; set; }
    public override string ToString() => $"Direction: {Direction}; Speed: {Speed}";
}

public enum WindDirection { East, South, West, North, Northeast, Southeast, Northwest, Southwest }

2. Service Contract 階層

[ServiceContract]
public interface ISimpleWhetherForecast
{
    [OperationContract] BasicWhetherInfo GetBasicWhetherInfo(string postalcode);
}

[ServiceContract]
public interface IFullWhetherForecast : ISimpleWhetherForecast
{
    [OperationContract] WindInfo GetWindInfo(string postalcode);
}

關鍵點:ServiceContractAttribute 的 Inherited=false,故子介面必須重新標註 [ServiceContract]。

3. 服務實作繼承複用

public class SimpleWhetherForecastService : ISimpleWhetherForecast
{
    public BasicWhetherInfo GetBasicWhetherInfo(string postalcode)
        => new BasicWhetherInfo(WhetherConditions.Overcast, 23);
}

public class FullWhetherForecastService : SimpleWhetherForecastService, IFullWhetherForecast
{
    public WindInfo GetWindInfo(string postalcode)
        => new WindInfo(WindDirection.Northwest, "12km/h");
}

實作類別繼承實現 GetBasicWhetherInfo 邏輯複用,符合 DRY 原則。

4. IIS 宿主設定 (.svc + Web.config)

<%@ ServiceHost Service="Artech.InheritanceHierarchy.Service.FullWhetherForecastService, Artech.InheritanceHierarchy.Service" %>

// Web.config 端點繫結至最底層 Contract
<endpoint contract="Artech.InheritanceHierarchy.Service.IFullWhetherForecast" binding="wsHttpBinding" />

四、架構優勢

  1. 契約分層:消費者可依需求選擇簡易或完整 Contract。
  2. 實作複用:OO 繼承自然複用邏輯,避免重複代碼。
  3. 單一端點暴露:僅發布完整 Contract 端點,簡化部署與治理。
  4. 版本演進友善:新增功能只需擴充子 Contract/實作,不影響現有消費者。

五、下一步

Part II 將示範如何在 客戶端 重建相同繼承架構,而非接受扁平化 Proxy,達成端到端 OO 體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved