views:

102

answers:

4

Hey guys,

I have a list i'm filling at server side. It's a list of "User", which implements IComparable. Now when WCF is serializing the data, i guess it's not including the CompareTo method. This is my Object class :

[DataContract]
public class User : IComparable
{
    private string e164, cn, h323;
    private int id;
    private DateTime lastActive;

    [DataMember]
    public DateTime LastActive
    {
        get { return lastActive; }
        set { laatstActief = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string H323
    {
        get { return h323; }
        set { h323 = value; }
    }
    [DataMember]
    public string Cn
    {
        get { return cn; }
        set { cn = value; }
    }
    [DataMember]
    public string E164
    {
        get { return e164; }
        set { e164 = value; }
    }

    public User()
    {

    }

    public User(string e164, string cn, string h323, DateTime lastActive)
    {
        this.E164 = e164;
        this.Cn = cn;
        this.H323 = h323;
        this.LastActive= lastActive;
    }
    [DataMember]
    public string ToStringExtra
    {
        get
        {
            if (h323 != "/" && h323 != "")
                return h323 + " (" + e164 + ")";
            return e164;
        }
        set { ;}
    }

    public override string ToString()
    {
        if (Cn.Equals("Trunk Line") || Cn.Equals(""))
            if (h323.Equals(""))
                return E164;
            else
                return h323;
        return Cn;
    }

    public int CompareTo(object obj)
    {
        User user = (User)obj;
        return user.LastActive.CompareTo(this.LastActive);
    }
}

Is it possible to get the CompareTo method to reach the client? Putting [DataMember] isn't the solution as i tried it ( i know...).

Thanks in advance.

+1  A: 

No, CompareTo is a method not a member.

If you want to replicate this on the client side, either provide a client side library that adapts the client object as well as implements IComparable.

@frogbot does have a valid suggestion but passing objects is against the true nature of SOA, the goal is to talk interfaces and this is why they have made it harder to use the NetDataContractSerializer.

Nix
+2  A: 

Interfaces (code) are not serialized. You may consider switching to NetDataContractSerializer. In this case type info will be included in the stream and you can obtain the same object on the client (if the assembly containing the type is in AppDomain).

UserControl
A: 

Because your client and server are talking the same technology stack (i.e. both are using .Net), have your client side code reference the same (data object containing) assembly as what the server uses*. Then your interface implementations will be intact, both assemblies will be using the same data object definitions, rather than the server using the regular definition and the client using the definition that is generated as part of the proxy.

Sharing or "reusing" these assemblies is a topic that has been well covered on SO.

*this means your data objects like User are contained in a separate assembly, that is the sole task of that assembly. Then both your client and your server (webservice) can reference it.

slugster
+1  A: 

You do not need to implement comparable.

You can use delegates to sort a list as well, without the icomparable in the class. Try following code.

users.Sort(delegate(User u1, User u2) { return u1.LastActive.CompareTo(u2.LastActive); });

You can use this directly in your client.

thanks, this is even better then what i was using before
djerry