Skip to main content

Microsoft Silverlight

Answered Question Property genration in Silverlight for WCF service proxy class.RSS Feed

(0)

spatemp
spatemp

Member

Member

7 points

36 Posts

Property genration in Silverlight for WCF service proxy class.

I have silverlight project that uses WCF services.  This wcf service returns an object back to client and has following property.

[DataMember]

private decimal refurbCost;

[DataMember]

public decimal RefurbCost {

get

{

return refurbCost;

}

set

{

if (value > NewCost)

{

throw new ValidationException("Refurb cost can not be greater then New Cost.");

}

refurbCost =
value;

}

}

 

When I look at the proxy class generated in WCF service in silverlight, I see property generated like this.

[System.Runtime.Serialization.DataMemberAttribute(Name="refurbCost")]

public decimal refurbCost1 {

get {

return this.refurbCost1Field;

}

set {

if ((this.refurbCost1Field.Equals(value) != true)) {

this.refurbCost1Field = value;this.RaisePropertyChanged("refurbCost1");

}

}

}

[System.Runtime.Serialization.DataMemberAttribute()]

public decimal RefurbCost {

get {return this.RefurbCostField;

}

set {

if ((this.RefurbCostField.Equals(value) != true)) {

this.RefurbCostField = value;

this.RaisePropertyChanged("RefurbCost");

}

}

}

 

Question is whey is it stripping out my validation in the property of the object that I am returning from WCF service.

 

 

ccoombs
ccoombs

Contributor

Contributor

5168 points

758 Posts

Re: Property genration in Silverlight for WCF service proxy class.

 have a look at yavor's response here:

http://forums.silverlight.net/forums/p/37731/154037.aspx

basically the cause is that the proxy generator doesn't copy your logic.  the solution is to bypass the proxy generator by explicitly providing the type through a separate assembly.

spatemp
spatemp

Member

Member

7 points

36 Posts

Re: Re: Property genration in Silverlight for WCF service proxy class.

thanks for this post.  So, I do have my types in a seprate class library.  However there are two issues with this.

 1) when I try to add this type class library(Add reference, Projects, types project) to silverlight project, it tells me that "You cna only add project references to othe Silverlight project in the solutions.

2) by leting proxy generate type you have benefit of each type clas generated is inherited from INotifyPropertyChanged  and each property has this.RaisePropertyChanged("someproperty"); call.  I have hundreads of types and to make this changes in class library will be hughe. 

Is there some other options that you can think of that might accomplish the validation in silverlight using the proxy generated classes.

microsoft_kc
microsof...

Contributor

Contributor

2890 points

564 Posts

Re: Property genration in Silverlight for WCF service proxy class.

Hey spatemp.... You don't need to set the DataMember attribute to the Private member. All you have to do is: Set the DataMember attribute to the public property which u already done. So, just remove the attribute from the private member declaration & then see...

Hope, this will resolve your problem..

Remember: Please click on "Mark As Answer", if this answered your query partially or fully.


Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog

spatemp
spatemp

Member

Member

7 points

36 Posts

Re: Re: Property genration in Silverlight for WCF service proxy class.

thats fine.  I can take out the [DataMember] but the question is how do you retain the validation in property.  Becuase during adding wcf service to silverlight and the type generated in proxy does not have this validation.  It removes it.

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Re: Property genration in Silverlight for WCF service proxy class.

spatemp:

Is there some other options that you can think of that might accomplish the validation in silverlight using the proxy generated classes.

 

With ValidationAttribute from System.ComponentModel.DataAnnotations you can tag the property with ValidationAttribute Tag without putting any code logic in the Setter.  But that probably won't work for your case:

If you need to limit the data range on a field, you could tag your Property with RangeAttribute :

[Range(min, max,  ErrorMessage = "Value for {0} must be between {1} and {2}.")]
public int YourProperty {get;set;} 
But in your case you are trying to compare the value with the value of another Field. I don't think using RangeAttribute can work.  


sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

spatemp
spatemp

Member

Member

7 points

36 Posts

Answered Question

Re: Re: Re: Property genration in Silverlight for WCF service proxy class.

I solved this problem by simply creating child class in UI(silverlight) that inherits from proxy class generated from wcf service.  In the child class I overidden the properties that I wanted to validate on.  I then used the following extension method that copies properties from parent proxy class to the child class.  And then I used the child class to bind to silverlight elements such as DataGrid.  The reason for doing all these is simply I did not wated to change 100's of Type classes with this.RaisePropertyChanged("")  etc that Silverlight utilizes.

 

public static void CloneProperties<T1,T2>(this T1 origin, T2 destination)

{

// Instantiate if necessary

if (destination == null)

throw new ArgumentNullException("destination", "Destination object must first be instantiated.");

// Loop through each property in the destination

foreach (var destinationProperty in destination.GetType().GetProperties())

{

// find and set val if we can find a matching property name and matching type in the

//origin with the origin's value.

if (origin != null && destinationProperty.CanWrite)

{

origin.GetType().GetProperties().Where(x => x.CanRead &&

(x.Name == destinationProperty.Name && x.PropertyType == destinationProperty.PropertyType)).ToList()

.ForEach(x => destinationProperty.SetValue(destination, x.GetValue(origin,
null), null));

}

}

}

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities