tags:

views:

67

answers:

4

Hi All,

I have a WCF service that needs to expose a custom collection to it's clients. I wondered what would be the best way to expose this object to my clients?

Object Code:

  public class ListBoxDataCollection : System.Collections.CollectionBase
{
    public ListBoxDataCollection()
    {
    }

    public ListBoxData this[int index] 
    {
        get { return (ListBoxData)this.List[index]; }

        set { this.List[index] = value; }
    }

    public int IndexOf( ListBoxData item )
    {
        return base.List.IndexOf(item);
    }

    public int Add( ListBoxData item )
    {
        return this.List.Add(item);
    }

    public void Remove( ListBoxData item )
    {
        this.InnerList.Remove(item);  
    }

    public void CopyTo( Array array, int index )
    {
        this.List.CopyTo(array, index);
    }

    public void AddRange( ListBoxDataCollection collection )
    {
        for (int i = 0; i < collection.Count; i++)
        {
            this.List.Add(collection[i]);
        }
    }

    public void AddRange( ListBoxData[] data )
    {
        this.AddRange(data);
    }

    public bool Contains( ListBoxData item )
    {
       return this.List.Contains(item);
    }

    public void Insert( int index, ListBoxData item )
    {
        this.List.Insert(index, item);
    }
}
A: 

I think it would be better if you just expose an array. Custom collection implies that there is some special functionality in it, like special sorting or behavior. SOA/WCF interface should contain only data, and not a behavior or functionality.

Exposing functionality over the wire in any distributed system is kind of antipattern. This forces potential clients that reside on other platforms to copy your code and this in it's turn violates the famous DRY (Don't Repeat Yourself) principle.

Vitaliy Liptchinsky
-1 WCF has 'multiple personalities'. It can be used to serve up web services, but is also meant to replace .NET Remoting...which served remote objects (state, functionality, everything). Don't assume he's talking about a SOA style service.
Justin Niessner
+1  A: 

Decorating it with [DataContract] attribute.

Ismail
+2  A: 

Are you on .NET 3.5 or up? In that case, I would recommend List<ListBoxData> which seems like the easiest way to go. WCF was no trouble at all with generic lists - worst case it'll model them as an array of ListBoxData on the client.

If you own both ends of the communication, you could put the data contracts (and service contracts) into a shared assembly and use it on both ends, thus getting around this problem - in that case, you could use List<ListBoxData> on both ends of the communication.

Marc

marc_s
Yes I'm using 3.5. So I just not bother with the custom collection, but just create a generic collection. But then how do I wire that generic collection to the client? Just as a DataContract?? Or through a method that returns the collection?
Tony
I have control over the server, as it's a website I will be hosting through a hosting company, but the clients I don't have control over, as it could be anyone viewing the website.
Tony
As long as your `ListBoxData` is decorated with `[DataContract]`, you're good to go - just return `List<ListBoxData>` from your service method.
marc_s
So most clients will see an array of ListBoxData on their end - not a problem at all
marc_s
+1  A: 

Well, WCF is really not about exposing objects -- in fact, exposing an object/custom type violates the tenets of SOA

If you need the class to be available on both sides of your service boundary, you will need to provide a library that has that class defined in it that both the service and client can reference directly.

To transfer the data from the custom collection over WCF, the best practice would be to define data contracts/data contract collections that can be used to move the data (without any behavior), and to convert the data to/from your custom collection type on either side of the service call.

So when someone called the service, the service would convert the collection to a DataContract, provide it to the client, and the client would convert it into the custom collection.

Guy Starbuck