Level 200
DevForce Express
July 27, 06
Let’s say you’re managing a customer’s orders for the current month. If your form displays this list in several places, how do you ensure that new orders or cancellations placed by this customer are automatically updated in this list?
You can ensure the list stays current by attaching an EntityListManager to it. Attaching a ListManager to the list turns it into a "live" or managed list. The ListManager will ensure that any changes, additions or deletions to entities meeting its filtering criteria are automatically reflected in the list.
Let’s see an example of how this is done.
1. In the Customer class, define a property which returns orders for the current month only.
C#:
public EntityList<Order> OrdersForMonth {…}
VB.NET:
ReadOnly Property OrdersForMonth() As EntityList(Of Order) {…}
- In the getter for the method, retrieve all the customer’s orders.
C#:
// Don’t do this - it will copy the EntityListManager
// from the .Orders ReadOnlyEntityList too.
//this.mOrdersForMonth = new EntityList<Order>(this.Orders);
// Do this instead.
this.mOrdersForMonth = new EntityList<Order>();
this.mOrdersForMonth.AddRange(this.Orders);
VB.NET:
' Don't do this - it will copy the EntityListManager
' from the Orders ReadOnlyEntityList too.
'Me.mOrdersForMonth = New EntityList(Of Order)(Me.Orders)
' Do this instead.
Me.mOrdersForMonth = New EntityList(Of Order)
Me.mOrdersForMonth.AddRange(Me.Orders)
- Define filtering criteria using an anonymous predicate - return true if OrderDate is for the current month.
C#:
Predicate<Order> filter = delegate(Order pOrder) {
return pOrder.OrderDate.Value.Year == DateTime.Now.Year &&
pOrder.OrderDate.Value.Month == DateTime.Now.Month;
};
VB.NET:
Dim filter As New Predicate(Of Order)(AddressOf FilterOrdersByDate)
For VB, the implementation of the predicate occurs outside of the Property method:
Private Shared Function FilterOrdersByDate(ByVal pOrder As Order) _
As Boolean
Return pOrder.OrderDate.Value.Year = DateTime.Now.Year _
And pOrder.OrderDate.Value.Month = DateTime.Now.Month
End Function
- Add a ListManager to make this a "live" list. The PersistenceManager will use the filter to keep the list current. Changes to OrderDate on existing objects or any new customer orders or deletions will automatically be reflected in the list.
C#:
this.mOrdersForMonth.ListManager =
new EntityListManager<Order>(
this.PersistenceManager,
filter,
new EntityColumn[] { Order.OrderDateEntityColumn } );
VB.NET:
Me.mOrdersForMonth.ListManager = _
New EntityListManager(Of Order)( _
Me.PersistenceManager, _
filter, _
New EntityColumn() { Order.OrderDateEntityColumn } )
To test this out, retrieve a customer and call its OrdersForMonth. Add a few new orders and delete some others. You should see the list automatically reflect these changes.