Embedding a object of type dynamic and use its properties as backing fields

Embedding a object of type dynamic and use its properties as backing fields

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


JanR posted on Wednesday, August 08, 2012

I have data transfer objects of type dynamic which I want to use as backing store for my CSLA business objects but I have a problem with the SetProperty-method.

I tried this:

[Serializable]
public class MyBusinessObject : BusinessBase<MyBusinessObject>
{
private dynamic _ddto;  

public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id, RelationshipTypes.PrivateField);
public int Id
{
get { return GetProperty(IdProperty, _ddto.ExceptionId); }
set { SetProperty(IdProperty, ref _ddto.ExceptionId, value); }
}
}

The error I am getting is 'ref' argument is not classified as a variable.

Any guesses what am I doing wrong?

JonnyBee replied on Wednesday, August 08, 2012

This is not a good idea.

You need the values to be serializable and preferrably (in the future) with MobileFormatter.

When you register a PropertyInfo CSLA itself will maintain a Dictionary<string, object> as backing store for all property values - so there is no need for a dynamic backing field that will also require extra plumbing code for serializing and deserializing of the object.

JanR replied on Thursday, August 09, 2012

Thanks for your advice, I know that CSLA is able to store the properties in automatic backing fields and we have used this in many situations. But this time we have several reasons to have this dynamic object a backing store. The need depends on severeal other components.Furthermore it is serializable and implements IMobileObject. So serialization is no problem, neither .net nor silverlight.

Evereything already works except for the SetProperty-Method. At the moment we just directly set the properties of the object, but this way business rules checking does not happen.

Besides your advice of not to do what we want to do, are there any other hints to make it work?

JonnyBee replied on Thursday, August 09, 2012

If you do not need AuthorizationRules on the field you may get away with:

public int Id
{
get { return GetProperty(IdProperty, _ddto.ExceptionId); }
set
  {
     //SetProperty(IdProperty, ref _ddto.ExceptionId, value);
     if (_ddto.ExceptionId != value)
     {
         _ddto.ExceptionId = value;
         PropertyHasChanged(IdProperty);
     }
  }
}

You could also create new SetProperty overloads that gets a lambda to set the actual value but maybe you don't need this.

Copyright (c) Marimer LLC