Rockford Lhotka
    CTO at Magenic
    Author, speaker, software architect
    Creator of the CSLA .NET framework

Home
Blog
CSLA .NET
Magenic
Speaking
Publications
About me
Contact me

Login

CSLA .NET 3.5 property declaration syntax

 

CSLA .NET 3.5 implements a powerful new way of implementing properties, where you don't need to declare a field to store the property's value. The field values are managed by CSLA .NET and so are called managed fields. In the future some advanced features of CSLA .NET may be unavailable unless you use managed fields. The syntax for declaring such a property is:

[VB]

Private Shared NameProperty As PropertyInfo(Of String) = _
  RegisterProperty(Of String)(GetType(ObjectType), New PropertyInfo(Of String)("Name"))

Public Property Name() As String
  Get
    Return GetProperty(Of String)(NameProperty)
  End Get
  Set(ByVal value As String)
    SetProperty(Of String)(NameProperty, value)
  End Set
End Property

[C#]

private static PropertyInfo<string> NameProperty =
  RegisterProperty<string>(typeof(ObjectType), new PropertyInfo<string>("Name"));

public string Name
{
  get { return GetProperty<string>(NameProperty); }
  set { SetProperty<string>(NameProperty, value); }
}

CSLA .NET 3.5 also supports a different syntax where you use private fields to store the values. This technique is faster than using managed fields, but requires that you declare and maintain your own fields. The syntax for declaring such a property is:

[VB]

Private Shared NameProperty As PropertyInfo(Of String) = _
  RegisterProperty(Of String)(GetType(ObjectType), New PropertyInfo(Of String)("Name"))
Private _name As String = NameProperty.DefaultValue

Public Property Name() As String
  Get
    Return GetProperty(Of String)(NameProperty, _name)
  End Get
  Set(ByVal value As String)
    SetProperty(Of String)(NameProperty, _name, value)
  End Set
End Property

[C#]

private static PropertyInfo<string> NameProperty =
  RegisterProperty<string>(typeof(ObjectType), new PropertyInfo<string>("Name"));
private string _name = NameProperty.DefaultValue;

public string Name
{
  get { return GetProperty<string>(NameProperty, ref _name); }
  set { SetProperty<string>(NameProperty, ref _name, value); }
}

Finally, for backward compatibility CSLA .NET 3.5 continues to support the CSLA .NET 2.0/3.0 syntax:

Private _name As String = ""

Public Property Name() As String
  Get
    CanReadProperty("Name", True)
    Return _name
  End Get
  Set(ByVal value As String)
    CanWriteProperty("Name", True)
    If String.IsNullOrEmpty(value) Then value = ""
    If _name <> value Then
      _name = value
      PropertyHasChanged("Name")
    End If
  End Set
End Property

[C#]

private string _name = string.Empty;

public string Name
{
  get
  {
    CanReadProperty("Name", true);
    return _name;
  }
  set
  { 
    CanWriteProperty("Name", true);
    if (string.IsNullOrEmpty(value)) value = string.Empty;
    if (_name != value)
    {
      _name = value;
      PropertyHasChanged("Name");
    }
  }
}

Please note that the overloads for CanReadProperty(), CanWriteProperty() and PropertyHasChanged() that infer the name of the calling property have been marked as obsolete, so using those older overloads will result in compiler warnings. The syntax shown here is the correct way to use these methods.

 

(Updated 1/29/2008)