Handling Value Types

In this step we extend the class Employee a little bit. The new lines are typed in bold font.

using System;
using NDO;
using System.Drawing;
namespace TravelExpenses
{
    [NDOPersistent]
    public class Employee
    {
        string FirstName;
        string LastName;
        System.Drawing.Point position;
        public Employee()
        {
        }
        public string FirstName
        {
            get { return FirstName; }
            set { FirstName = value; }
        }
        public string LastName
        {
            get { return LastName; }
            set { LastName = value; }
        } 
        public void SetPosition(int x, int y)
        {
            position = new Point(x, y);
        }
    }
}

In all companies around the world employees ask themselves: »What is my position in the company?« Let's assume for a minute, that a Point structure as part of the class Employee could tell us the truth about this question.

Please note that the assembly System.Drawing needs to be added to the references so that the namespace is available.

The Enhancer detects if an element is a ValueType. It maps public member variables and public properties that can be read and written to separate database fields. This applies as long as they are primitive types like int, bool and likewise. NDO cannot store complex field types like classes (with the exception of System.String).

In our case the field position is of type System.Drawing.Point. This ValueType has two public properties X and Y, which are readable and writable. They are automatically mapped by NDO to the two columns Position_X and Position_Y in the Employee table of the database.

The following client code creates an Employee object, whose position will be stored in the database after the program is started.

Employee e = new Employee();
e.FirstName = "Mirko";
e. FirstName = "Matytschak";
e.SetPosition(10, 5);
PersistenceManager pm = new PersistenceManager();
pm.MakePersistent(e);
pm.Save();

This solution is part of the tutorial samples. You can find the tutorial source code in the Tutorial folder beneath your NDO installation folder. The code up to here is placed in the Directory NDOTravelExpenses-Step 1.