开发者

how to add extensions that will visible to the client only when she needs it?

开发者 https://www.devze.com 2023-02-21 20:06 出处:网络
I have Order class: public class Order : IOrder { public long Id { get; private set; } public string Serial { get; private set; }

I have Order class:

public class Order : IOrder
    {
        public long Id { get; private set; }
        public string Serial { get; private set; }
        public long CustomerId { get; set; }

        public Order() { }

        public IOrderItems GetOrderItems()
        {
            return new OrderItems(Id);
        }

    }

And I wanted to add and extension to order: OrderItems, so I used the Extension Object Patterns - as you can see Order have GetOrderItems(). OrderItems looks like:

public class OrderItems : IOrderItems
{
    public IItem[] Items { get; set; }

    public OrderItems(long orderId)
    {
        Items = Item.GetItems(orderId);
    }
}

Since I am writing web application, I am using web services. My problem is that sometimes I need to retrieve the Order data without its Items and somwtimes I need to retrieve the Order data with it's Items. Retrieving Order object doesn't help much since it don't retrieve to the client it's Items. If I'll convert GetOrderItems to a property - I'll always return the OrderItems to the client which is not what I am looking for.

In addition - sometimes the client would want to call one webserv开发者_StackOverflowice with only Order as parameter and sometimes the client would want to call other webservice with Order and Items as parameters.

How can I solve this?


Perhaps I am misunderstanding your question, but can't you just provide two methods in your web service to cover both options? So if we change your Order class like this:

public class Order : IOrder 
{ 
    public long Id { get; private set; } 
    public string Serial { get; private set; } 
    public long CustomerId { get; set; }
    public IOrderItems OrderItems { get; set; }

    public Order() { } 

    internal IOrderItems LoadOrderItems() 
    { 
        OrderItems = new OrderItems(Id); 
    } 
}

You can then add a web service implementation which would be roughly as follows:

public class OrdersService : IOrderService
{
    public IOrder LoadOrderOnly(long id)
    {
        var order = someDataAccessInstance.LoadOrder(id);
        return order;
    }

    public IOrder LoadOrderWithItems(long id)
    {
        var order = someDataAccessInstance.LoadOrder(id);
        order.LoadOrderItems();
        return order;
    }
}
0

精彩评论

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