WCF - 如何以面向对象的方式定义服务?

时间:2012-03-26 13:47:35

标签: wcf oop datacontract servicecontract

通过WCF访问远程对象模型的最佳方法是什么?

我有一个带有面向对象模型的系统层(A),并希望从另一层访问此模型(B)

B按需加载所需对象。例如,假设我有C1和C2类,其中C1持有C2列表。只有在访问该列表时才应加载该列表。

由于数据合同不能保存运营合同,我会用一个服务合同实现这个,它有两个方法“getC1”和“getListC2(C1)”

但是,我真正想要的是访问面向对象的模型,例如在C1上调用一个函数:C1.getListC2

如何以更加面向对象的方式使用WCF?

1 个答案:

答案 0 :(得分:3)

解决此问题的一种方法是使用您自己的延迟加载(和其他)业务逻辑来包装代理对象。换句话说,假设您有一个名为Order的WCF代理和一个服务方法GetOrderLineItems()。

public class Order
{
    private Proxies.Order _order;
    private List<OrderLineItem> _lineItems;

    public string Name 
    {
        get { return _order.Name; }
    }

    public List<OrderLineItem> LineItems
    { 
        if (_lineItems == null)
        {
             _lineItems = //Make the service call to get these objects
        }
    }
}

另一种合成方法是向代理对象添加扩展方法:

public static List<Proxies.OrderLineItem> GetLineItems(this Proxies.Order order)
{
     //Make the service call to get the line items
}

允许你这样做:

var lineItems = order.GetLineItems();