Unmanaged property declaration question.

Unmanaged property declaration question.

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


rxelizondo posted on Saturday, September 26, 2009

Hello.

On page 176 of the book the author says:

--------------------------------------------------
I have included examples for the two most common types of property declaration. The first
example uses a private backing field, which you declare directly in code:

// example with private backing field
private static PropertyInfo IdProperty = RegisterProperty(typeof(EditableRoot), new PropertyInfo("Id"));
private int _Id = IdProperty.DefaultValue;
public int Id
{
get { return GetProperty(IdProperty, _Id); }
set { SetProperty(IdProperty, ref _Id, value); }
}

This approach performs better than the alternative, but does require slightly more complex
code. Additionally, if you intend on using CSLA .NET for Silverlight to create a Silverlight version of
your application, this approach will require extra coding to support that environment.
--------------------------------------------------

The problem is that in the ProjectTracker example he uses a private field for a time stamp and completely bypasses any type of property registering process.

private byte[] _timestamp = new byte[ 8 ];

So regarding the part of his statement “if you intend on using CSLA .NET for Silverlight to create a Silverlight version of your application, this approach will require extra coding to support that environment.” Is the fact that the _timestamp is not a managed field will cause any problems with Silverlight?


Thank you.

JonnyBee replied on Sunday, September 27, 2009

HI,

The ProjectTracker application is not completely up-to-date on all the propert declarations for use in Silverlight. I'd recommend to change these properties into managed properties if you want to write ProjectTracker in Silverlight.

Managed properties require no extra code for serialization in Silverlight, private backing fields do.

You may also look at varieos implementations of ProjectTracker on CslaContrib (versions for ASP.NET MVC and Prism(WPF))

/jonnybee

ajj3085 replied on Monday, September 28, 2009

I believe it would. I think the issue is that SL doesn't have reflection, and thus no way to serialize BOs by looking at the fields directly. I think there are two ways around this: 1) make the field a managed property with managed field. 2) override some methods of MobileObject to handle serializing / deserializing the field.

RockfordLhotka replied on Monday, September 28, 2009

Yes, the _timestamp field would need to be a managed property (though it could be private) to work transparently with Silverlight.

ajj3085 replied on Monday, September 28, 2009

Would it be possible to have a private backing field if you override the serialize / deserialize on MobileObject? I know that's probably not the recommended approach.. just curious if it would work though.

RockfordLhotka replied on Monday, September 28, 2009

That would work just fine. And it is a supported technique - the only
problem with it is that I think it increases the maintenance burden because
it is too easy to forget to update the getstate/setstate methods when you
add a new field to a class.

Copyright (c) Marimer LLC