Silverlight - Extend WCF Class in the Client

771 views Asked by At

I've seen this question asked in various incarnations around the web, but never a definitive answer. And since this is the forum for definitive answers....

I am using WCF to provide the Entity objects for my Silverlight client, based on an SQL database.

I'd like to extend the class for this object on the client side only. I am interested in creating properties that make sense for binding only, and don't make sense to store in the main object. Is it possible to extend this class? If not, is there a "best practice" for doing this sort of thing?

Thanks, wTs

1

There are 1 answers

1
vortexwolf On BEST ANSWER

Generated classes actually have the modifier 'partial', so it is possible to extend them.

For example, this DataContract:

[DataContract]
public class TestObject
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Title { get; set; }
}

Can be extended on the client's side in this way:

namespace SilverlightTest.ServiceReference1
{
    public partial class TestObject
    {
        public string ExtendedTitle
        {
            get { return this.Id+" "+this.Title; }
        }
    }
}

Take notice of the name of namespace, it must be the same as the namespace of a service reference.