views:

584

answers:

2

Can you override the ToString function in a WCF DataContrat? Right now I have:

[DataContract]
public class Keyword
{
    public int ID { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

But it doesn't seem to work. Anyway to get this working?

A: 

Where do you want to be able to invoke ToString()? Methods are not part of the DataContract so they won't be available when you create the proxy for the client.

Of course, nothing is stopping you from coding that method in the client's proxy yourself.

Andrew Hare
How would you do it in the client;s proxy?
Eric P
You would have to have access to the client's proxy and code the method into the class by hand. Remember that a data contract is just that - a contract - it is not designed to have any behavior as the behaviors ought to be in the operation contracts. That being said there is nothing stopping you from adding any methods you wish in both the service and the client.
Andrew Hare
A: 

Remember too that if you own both the server and the client, that often you can use a shared library for data contracts rather than generating a client proxy. If you do that, then you can have the same method on both the server and client as they're exactly the same type.

Drew Noakes
Yes, and this is something to be avoided, as a general rule, as it tightly binds the client to the server; even to the _version_ of the server software.
John Saunders
@John this is not the case. This is not a binary dependency (like .NET remoting has). I do this for my WCF client-server app and can roll out different assembly versions on one side without rolling out the other. The only issue to take heed of is ensuring that the data contracts match (contract name, namespace, member names), which is an issue common to both shared binaries or generated proxies.
Drew Noakes
One more thing -- if your data contract implements IExtensibleDataObject you can even have old versions round-trip data members that were not included at compile time! So if I add a new field 'Foo' to my data contract, send it to an old version of some application which then sends it back, the original value of 'Foo' will still be populated in the response, even though the old application never new the property existed.
Drew Noakes
You are correct that it's not the same sort of version dependency as Remoting has. Still, it is a dependency. The version of the assembly used on the client needs to be compatible with the version used on the service. No big deal for ToString, but a bigger deal if the server is making assumptions about how the client side works or vice versa.
John Saunders
Even if you use a proxy, the version of the client proxy must be compatible with the service. There's no way around that. If your data contracts behave primarily as DTOs (data transfer objects) then there's not likely to be any logic other than ToString or whatever. I prefer having the majority of DTOs in shared libraries because thankfully WCF does not create binary coupling. As with everything, it depends upon your scenario.
Drew Noakes