New Posts New Posts RSS Feed: Tech Tip: Keep Your Lists Current With the EntityListManager
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Tech Tip: Keep Your Lists Current With the EntityListManager

 Post Reply Post Reply
Author
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Topic: Tech Tip: Keep Your Lists Current With the EntityListManager
    Posted: 06-Jun-2007 at 2:42pm

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) {…}

  1. 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)

 

  1. 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

  1. 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.

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down