Calanus
Calanus

Reputation: 26287

Silverlight RIA: How do I implement change notification on my shared code

I have extended my domain service using a "shared" code file to add an additional property to my "Booking" class that calculates mileage from StartMileage and FinishMileage as follows:

 public int? JourneyMileage
        {
            get
            {
                if (StartMileage.HasValue && FinishMileage.HasValue)
                {
                    return (FinishMileage - StartMileage);
                }
                else
                {
                    return null;
                }
            }
        }

StartMileage and FinishMileage are properties in my autogenerated domain service. The property JourneyMileage is bound to the UI, as are StartMileage and FinishMileage.

How do I update the JourneyMileage property (and therefore the UI as well) when StartMileage or FinishMileage are changed? I have been looking for something like NotifyPropertyChanged but seem to have drawn a blank.

Upvotes: 1

Views: 582

Answers (3)

kyriosity-at-github
kyriosity-at-github

Reputation: 51

This should work. Don´t tested it.

1) This won't be compiled with Silverlight, if you need property changed notification. The generated shared class on the client side is from another namespace (other .dll in SL) and has other method signatures for raising property changed :-(

Another way is, if you don´t need the JourneyMileage property on the server side, to create a partial "Booking" class on the client side, define your property and put my code without the conditional statement in the partial class.

2) This will work and could be a reasonable solution, however you can't then share the business logic with the EDM :-(

3) One other solution could be to add the property to generated metadata class with [DataMember] attribute. The disadvantage (or in some use cases advantage) is that changing such property will set context as changed.

Upvotes: 0

Jehof
Jehof

Reputation: 35544

Add to your shared Booking class a conditional silverlight code block like this.

#if SILVERLIGHT
  partial void OnStartMileageChanged(){
    RaisePropertyChanged("JourneyMileage");
  }

  partial void OnFinishMileageChanged(){
    RaisePropertyChanged("JourneyMileage");
  }

#endif

This should work. Don´t tested it.

Another way is, if you don´t need the JourneyMileage property on the server side, to create a partial "Booking" class on the client side, define your property and put my code without the conditional statement in the partial class.

Upvotes: 2

Emond
Emond

Reputation: 50682

You do NOT update the JourneyMilage property. It changes when the other fields change.

If you want to notify others that its value has changed, implement INotifyPropertyChanged and raise the PropertyChanged event for JourneyMilage too when either StartMilage or FinishMilage change.

EDIT

See this post

Upvotes: 1

Related Questions