R – Silverlight 3 RIA services and properties being “flattened”

silverlightwcf-ria-services

I have a model that looks roughly like this:

private bool IsProduct {get; set;}
private decimal ProductPrice {get; set;}
private decimal TimedRate {get; set;}

    public decimal SingularAmount {
        get {
            if (this.IsProduct) {
                return ProductPrice;
            }
            else {
                return TimedRate;
            }
        }
        set {
            if (this.IsProduct) {
                this.ProductPrice = value;
            }
            else {
                this.TimedRate = value;
            }
        }
    }

I'm binding to this SingularAmount property via RIA Services to Silverlight 3 DataGrid. What I'm finding is that, when I change the property – the respective properties on the model do not get updated. When I step through the code, I can see on the client side, that SingularAmount is set to say 5 for example, the other properties do not get updated.

Seems like when RIA makes the client side version of the classes, this sort of functionality isn't ported over. Any ideas on how to tackle this?


Update

Here is the RIA generated code for that property:

    [DataMember()]
    public decimal SingularAmount
    {
        get
        {
            return this._singularAmount;
        }
        set
        {
            if ((this._singularAmount != value))
            {
                this.ValidateProperty("SingularAmount", value);
                this.OnSingularAmountChanging(value);
                this.RaiseDataMemberChanging("SingularAmount");
                this._singularAmount = value;
                this.RaiseDataMemberChanged("SingularAmount");
                this.OnSingularAmountChanged();
            }
        }
    }

Obviously, that doesn't look much like the original server side property.

Best Answer

1) If the entity model is within the same project as the DomainServices:

In order to get this to work you need to create a new file with the extension

.shared.cs (or vb).

This extension tells RIA Services to copy the file to the client side.

Within the file you can extend the entity (using partial) and add the

new property. Don't forget to call "this.RaisePropertyChanged("SingularAmount")" so

that any control bound to this property will get notification of the change.

Because you are using partial classes, and the way RIA shared code works, the

new file has to be within the same project/assembly as the original entity.

...

2) If the entity model is in a different project:

Create a partial class in the client side project with the new property.

Same concept as above but new property will only be visible in the client side.

...

You can find more information on sharing code on the overview document here. Page 34.

Chapter 3 is about client side code generation. Good info to know.

Also, chapter 12 (page 97-103) on how to share code accross tiers, chapter 17

(pages 122-125) Code generation hook points, and chapter 18 (pages 126-128) How to add

how to add computed properties.

If you are serious on using .NET RIA Services, you probably should know this

document by heart. :-)