Relation of Travel to Expense

Here we are back on well known territory. A 1:n composite relation is created:

// In the class Travel

[NDORelation(typeof(Expense), RelationInfo.Composite)]

IList expenses = new ArrayList();

public IList Expenses

{

    get { return ArrayList.ReadOnly(expenses); }

    set { expenses = value; }

}

public void RemoveExpense(Expense ex)

{

    if (expenses.Contains(ex))

        expenses.Remove(ex);

}

public void AddExpense(Expense ex)

{

    expenses.Add(ex);

}

Maybe you noticed that there is no function NewExpense(). This factory function would not be useful here because we cannot instantiate objects of the type Expense. Rather, we want to create instances of concrete classes like Receipt and MileageAllowance. The problem that arises here is typical for situations with polymorphy involved. This is why we take care of it with a pattern, which can be used in similar situations.