CSLA, Silverlight , PrivateFields and deserialization problem

CSLA, Silverlight , PrivateFields and deserialization problem

Old forum URL: forums.lhotka.net/forums/t/11366.aspx


mtrtm posted on Friday, May 18, 2012

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

I have a Silverlight 5 project with a WCF remote data portal (CSLA 4.3 code generated by CodeSmith) and have a property in my business object that looks like this:

 

private static readonly PropertyInfo<System.String> _myStringProperty = RegisterProperty<System.String>(p => p.MyString, "MyString", (System.String)null, RelationshipTypes.PrivateField);

private System.String _myString = _myStringProperty.DefaultValue;

public System.String MyString

{

                get { return GetProperty(_myStringProperty, _myString); }

                set { SetProperty(_myStringProperty, ref _myString, value); }

}

 

In Silverlight, when I get the object back from the data portal fetch, all the properties (public and private) are empty/null.

While I am new to Silverlight and CSLA, I have a theory that this may be due to Silverlight’s security restriction on reflection of private members.  I am assuming that CSLA uses reflection to set class properties, and when registering a private field with the RegisterProperty() call CSLA now knows about this field (instead of not knowing about a private auto-generated backing field).   I am guessing that since code compiled with Silverlight cannot access private members but CSLA knows about the property it tries to set it and can’t.

 

When I change the code to have public a public property (see below) the public property’s value in Silverlight is populated after a data portal fetch:

private static readonly PropertyInfo<System.String> _myStringProperty = RegisterProperty<System.String>(p => p.MyString, "MyString");

private System.String _myString = _myStringProperty.DefaultValue;

public System.String MyString

{

                get { return GetProperty(_myStringProperty); }

                set { SetProperty(_myStringProperty, value); }

}

 

Does anyone know:

  1.     Why CodeSmith CSLA templates are using the private field (from a quick google search it sounds like it has something to do with enforcing business rules)
  2.     Is my theory is correct, what is actually happening and why?
  3.     How should properties be defined in business objects with Silverlight?

RockfordLhotka replied on Friday, May 18, 2012

You may want to read the Using CSLA 4 books - at least the data portal book that covers serialization in some detail.

CSLA uses its own serializer for Silverlight, called the MobileFormatter. To avoid the use of reflection (because reflection is too restricted on Silverlight to be useful for serialization) you should generally use managed backing fields (not private fields) for your properties. This allows MobileFormatter to automatically serialize the values. If you do want to use private fields, you must override OnGetState and OnSetState and manually get/set the private field values into the serialization context so they are serialized.

Copyright (c) Marimer LLC