Chapter 7. Object Navigation

7.1. Introduction

(Available in 1.0)

Spring provides an expression language that allows for the easy setting or getting of an object's properties and the invoking of its methods. The simple syntax of this expression language allows one to to easily express the properties or methods to invoke in order to retrieve or set a value on an object. For example to get the name property of an object, the required expression to evaluate this is simply "Name". The chaining of properties is also supported... for example, if a person object contains a date of birth property (DOB) that returns a DateTime, the year maybe retrieved with the expression "DOB.Year". This generic reflection-like functionality has many uses but is often found as the foundation to support a data binding language between GUI elements and model objects. As such, the data binding support for ASP.NET web pages contained in the Spring.Web library uses this expression language.

7.2. Simple Expressions

Consider the simple class shown below with the public field Name

public class Inventor
{
    public string Name;
    private DateTime dob;

    public DateTime DOB
    {
      get { return dob; }
      set { dob = value; }
    }
}

which may have been instantiated in code somewhere and had its Name and DOB set to particular values

Inventor inventor = new Inventor();
inventor.Name = "Nikola Tesla";
inventor.DOB = new DateTime(1854, 10, 9);

The ObjectNavigator is the central class used to set or retrieve the value of an object, and contains the following static methods

object GetValue(object root, string expression)
object GetValue(object root, NavigationExpression expression)

void SetValue(object root, string expression, object newValue)
void SetValue(object root, NavigationExpression expression, object newValue)

To retrieve the name and year of birth we can use the following code

string name = (string) ObjectNavigator.GetValue(inventor, "Name");    
int year = (int) ObjectNavigator.GetValue(inventor, "DOB.Year");
    

The string "DOB.Year" is used to create a NavigationExpression object that forms the basis for the parsing of the string. If you need to evaluate a complex expression frequently, creating a NavigationExpression and reusing it will increase performance. To set the property values of this object instance to that of another famous inventor we would write

ObjectNavigator.SetValue(inventor, "Name", "Michael Pupin");
ObjectNavigator.SetValue(inventor, "DOB", new DateTime(1854, 10, 9));
    

7.3. Navigating Collections

TODO. TestCase shows some example usage. Please check the Spring.NET website for the latest updates to this document.