views:

402

answers:

3

I have a Silverlight 3 application which gets data from a web service. When I add a reference to my web service, all the classes used by the web service are defined in a proxy class. So far, so good... Now, I would like to use the DataForm offered in Silverlight 3. To use that component, I must add metadata to my class (e.g. see code below).

public class Movie : IEditableObject
{
    public int MovieID { get; set; }

    [Display(Name="The Movie Name")]
    public string Name { get; set; }    
}

My question is, considering the class Movie is defined in the proxy class generated by .NET, how do I add the attributes/metadeta to the properties of the Movie class without modifying the class generated?

Thanks

+2  A: 

In short; you don't. Don't edit the generated code, since your changes will be lost the next time it is generated.

Instead, create your own Movie class inside your application and create methods that translates the generated Movie proxy class to your internal Movie class (and back), and then make your internal class have the desired behavior. I usually wrap this together at a low level, so that most of my code never see the generated proxy classes.

Fredrik Mörk
A: 

If in case you really need to add something to the generated class, you can still use the partial class to achieve without modifying anything within the generated class.

xandy
To be clear: you can add members, but you can't add metadata to *existing* members.
Marc Gravell
@Marc, I am not sure if this is a way, http://davidhayden.com/blog/dave/archive/2008/01/06/ASPNETDynamicDataTutorialBuddyMetadataProviderCustomMetadataProviders.aspx
xandy
+1  A: 

If converting your web service to use .NET RIA Services is a possibility you can use this technique to apply attributes to properties without using a wrapper class:

http://blogs.msdn.com/brada/archive/2009/07/21/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-7-services-based-data-store.aspx

With RIA Services you apply the attributes on the server side - the code generator is intelligent enough to pick them up and apply them on the client side as well when it generates code.

James Cadd