Dynamic in silverlight

Dynamic in silverlight

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


rfcdejong posted on Monday, October 18, 2010

Is here someone with experience with implimenting IDynamicMetaObjectProvider in a Csla business class?

Some background:
In WPF i implimented dynamic properties which were available using the ITypedList or by using GetProperty("aPopertyName"). But in silverlight the ITypedList is an issue, it is not supported. Even the ICustomTypeDescriptor is an issue.

A possible solution:
As far as i can see i like the new dynamic options in .NET 4.0.
ExpandoObject and DynamicObject are classes which i could override but imho it would be better to have a mixed mode businessobject by implimenting IDynamicMetaObjectProvider and provide a custom DynamicMetaObject.

Anyway, this post is to start a discussion about anything relative to the new dynamic extension in .NET 4.0
I hope there are more people intrested adding dynamic properties which extend Csla objects.

rfcdejong replied on Tuesday, October 19, 2010

ok, another day has passed and i'm kinda getting frustrated hehe... implimenting it directly on the desendant business object isn't a problem.. but in the abstractionlayer business object i get messed up with the Expressions in the DynamicMetaObject code. argh..

I guess noone implimented this kind of functionality yet.

Anyway i found some intresting links:
dlr example by the dlr project itself: https://dlr.svn.codeplex.com/svn/DLR_Main/Runtime/Samples/LibraryAuthors/Program.cs
an article about dynamic interfaces from Bill Wagner on msdn: http://msdn.microsoft.com/en-us/vcsharp/ff800651.aspx

As i want to assign the business object to a dynamic it must be inherited from DynamicObject or impliment IDynamicMetaObjectProvider.

this works without a problem:

public class Person : MyBusinessBase<Person>, IDynamicMetaObjectProvider
{
  public static RegisterProperty.. standard csla code
  public string Name()
    { get { GetProperty... standard csla code
      set { SetProperty... standard csla code
  ...
 }

dynamic person = Person.GetPerson(1);
string name = person.Name;
string aDynProperty = person.AnDynamiclyAddedProperty;

but what i want to do:

public class MyBusinessBase<T> where T: MyBusinessBase<T>, IDynamicMetaObjectProvider
{
 ... lots of dynamic code
}

rfcdejong replied on Wednesday, October 20, 2010

Yes!!!

Using dynamic csla objects is possible, for now just the basics... no authorization checks nor events yet..
Thru you are required to have silverlight 4.

Just think in metadata, not in instances, and forget strongly typed code! :)

An example in of testing code (not at all the final code!)

public class DynamicBusinessBase<T> : BusinessBase<T>, IDynamicMetaObjectProvider
 
where T : DynamicBusinessBase<T>
{
  p
rivate IList<IDynamicPropertyInfo> _dynamicProperties = new List<IDynamicPropertyInfo>();
 
private IList<object> _dynamicPropertyValues = new List<object>();

  public
object GetPropertyValue(string propertyName)
  {
   
var props = FieldManager.GetRegisteredProperties();
   
IPropertyInfo property = props.FirstOrDefault(x => x.Name == propertyName);
   
if (property != null)
    {
     
return base.GetProperty(property);
    }
   
var dynProperty = GetDynamicProperty(propertyName);
   
return _dynamicPropertyValues[dynProperty.Index];
  }

 
public void SetPropertyValue(string propertyName, object newValue)
  {
   
// if registered csla property
   
var props = FieldManager.GetRegisteredProperties();
   
IPropertyInfo property = props.FirstOrDefault(x => x.Name == propertyName);
   
if (property != null)
    {
      SetProperty(property, newValue);
     
return;
    }
   
// if not csla property then it's an dynamic property
   
lock (_dynamicProperties)
    {
     
IDynamicPropertyInfo dynPropertyInfo = _dynamicProperties.FirstOrDefault(x => x.Name == propertyName);
     
if (dynPropertyInfo == null)
      {
        dynPropertyInfo =
new DynamicPropertyInfo(propertyName);
      }
    }
  }

  public
class MyMetaObject : DynamicMetaObject
  {
   
public MyMetaObject(Expression expression, DynamicBusinessBase<T> value)
      :
base(expression, BindingRestrictions.Empty, value)
    {
    }

    public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
    {
     
var self = this.Expression;
     
var bag = (DynamicBusinessBase<T>)base.Value;

      var propertyNameExpr = Expression.Constant(binder.Name);
      var valueExpr = Expression.Convert(value.Expression, typeof(object));

      var target = Expression.Block(
                       
Expression.Call(
                            
Expression.Convert(self, typeof(DynamicBusinessBase<T>)),
                            
typeof(DynamicBusinessBase<T>).GetMethod("SetPropertyValue"),
                             propertyNameExpr,
                             valueExpr
                        ),
                        valueExpr);

     
var restrictions = BindingRestrictions.GetInstanceRestriction(self, bag);

     
return new DynamicMetaObject(target, restrictions);
    }

 
public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
  {
   
var self = this.Expression;
   
var bag = (DynamicBusinessBase<T>)base.Value;

   
var propertyNameExpr = Expression.Constant(binder.Name);
   
   
var target = Expression.Block(
                      
Expression.Call(
                          
Expression.Convert(self, typeof(DynamicBusinessBase<T>)),
                          
typeof(DynamicBusinessBase<T>).GetMethod("GetPropertyValue"),
                           propertyNameExpr
                          ));

   
var restrictions = BindingRestrictions.GetInstanceRestriction(self, bag);
   
    return new DynamicMetaObject(target, restrictions);
  }

 

 

 


I'm glad that binding in Silverlight against dynamic objects is possible in Silverlight 4 using indexed binding. More info:http://msmvps.com/blogs/theproblemsolver/archive/2010/04/12/using-dynamic-objects-in-silverlight-4.aspx

OscarLauroba replied on Wednesday, March 09, 2011

Hi rfcdejong,

I'm very interesting in how to use csla with expandoObjects. Have you achieved your dynamic business base fully works? I mean data binding, validation rules, dynamic business list base and so on. Thank you for sharing your prototype code.

Oscar.

rfcdejong replied on Thursday, April 28, 2011

Sorry for the late response, i didn't spend any more time into it. However i still need to impliment it. I think implimenting the IDynamicMetaObjectProvider interface in a own business abstraction layer is the way to go.

Did u get any further until now?

Copyright (c) Marimer LLC