Using custom FieldData in Csla 3.1.7 for Net 2.0

Jason Bock wrote a great article on his blog about Creating custom Fielddata classes in Csla that requires Csla 3.7.1 of newer for .Net 3.5 Sp1. If you havent already read his article please do to get a better understanding of what custom FieldData can bring to your Csla solution.

However most of my worktime is using the Csla 3.71. for .Net 2.0 so how could I use these custom fielddata in a .Net 2.0 solution and still be able to port/recompile my code with Csla 3.7.1 for .Net 3.5?

Our customers/users have also requested our apps to be more precise in determining if data has been changed or not, ie has the propertyValue actually changed from the originalvalue. The introduction of custom field data would give us this with minimal effort. I can also vision custom FieldData beein used for auditing purposes because they may track both original and new field value.

The business objects in Jasons sample uses RegisterProperty with lambdas that are actually Expressions. So I had to alter my intermediate classes in MyCsla to add new registerProperty overloads that take a string propertyName instead of a lambda expression.  Latest version of MyCsla for Csla 3.7.1 N2 and the entire CunstomFieldData sample project is available for download on the CslaContrib project on CodePlex.

  [Serializable]
  public class BusinessBase<T> : Csla.BusinessBase<T> where T : BusinessBase<T>
  {
    #region RegisterProperty
    protected static PropertyInfo<P> RegisterProperty<P>(string propertyName)
    {
      return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName));
    }

    protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName)
    {
      return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName));
    }

    protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, RelationshipTypes relationship)
    {
      return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, relationship));
    }

    protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, P defaultValue)
    {
      return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue));
    }

    protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, P defaultValue, RelationshipTypes relationship)
    {
      return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue, relationship));
    }
    #endregion
  }

Next we need to alter BusinessCore base class in CustomFieldData project to inherit from MyCsla.BusinessBase  to get the new RegisterProperty overloads:

  [Serializable]
  public abstract class BusinessCore : MyCsla.BusinessBase where T: BusinessCore

And finally change the test business object to use the new RegisterProperty.

  [Serializable]
  public sealed class Person : BusinessCore
  {
    private static PropertyInfo ageProperty =
	                    RegisterProperty("Age");
    private static PropertyInfo firstNameProperty =
			    RegisterProperty("FirstName", "Friendly first name");
    private static PropertyInfo lastNameProperty =
			    RegisterProperty("LastName", "Friendly lastname", String.Empty);

And thats just about it. The custom FieldData works great!!

Download the latest version of MyCsla for Csla 3.7.1 for N2 with CustomFieldData project from CslaContrib

[Serializable]
public class BusinessBase<T> : Csla.BusinessBase<T> where T : BusinessBase<T>
{
#region RegisterProperty
protected static PropertyInfo<P> RegisterProperty<P>(string propertyName)
{
return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName));
}protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName)
{
return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName));
}protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, RelationshipTypes relationship)
{
return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, relationship));
}

protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, P defaultValue)
{
return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue));
}

protected static PropertyInfo<P> RegisterProperty<P>(string propertyName, string friendlyName, P defaultValue, RelationshipTypes relationship)
{
return RegisterProperty(Csla.Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue, relationship));
}
#endregion

protected override void DataPortal_OnDataPortalInvoke(DataPortalEventArgs e)
{
Debug.Print(“DataPortalInvoke object:{0}, operation:{1}”, e.ObjectType, e.Operation);
base.DataPortal_OnDataPortalInvoke(e);
}

protected override void DataPortal_OnDataPortalInvokeComplete(DataPortalEventArgs e)
{
Debug.Print(“DataPortalInvokeCompleted object:{0}, operation:{1}”, e.ObjectType, e.Operation);
base.DataPortal_OnDataPortalInvokeComplete(e);
}

protected override void DataPortal_OnDataPortalException(DataPortalEventArgs e, Exception ex)
{
Debug.Print(“DataPortalExeption object:{0}, operation:{1}, exception:{2}”, e.ObjectType, e.Operation, ex);
base.DataPortal_OnDataPortalException(e, ex);
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s