New Posts New Posts RSS Feed: Null Entities in a Managed List
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Null Entities in a Managed List

 Post Reply Post Reply
Author
sieepvvalk View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2007
Location: United States
Posts: 7
Post Options Post Options   Quote sieepvvalk Quote  Post ReplyReply Direct Link To This Post Topic: Null Entities in a Managed List
    Posted: 13-Aug-2007 at 3:37pm
Hello,

When creating an entitylist, one can add a null entity by calling:

EntityList<Person> personList = pManager.GetEntities<Person>();
personList.Add(pManager.GetNullEntity<Person>());

personList
---------------
John
Sue
Bob
(none)

however, if I attach a list manager to this entitylist, then the null entity
seems to get automatically removed:

EntityList<Person> personList = pManager.GetEntities<Person>();
personList.Add(pManager.GetNullEntity<Person>());
personList.ListManager = new EntityListManager<Person>(...);

personList
---------------
John
Sue
Bob

Is there a way to ensure that the null entity is not automatically removed
from the list when working with a managed list?

Thank you,

Todd
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 13-Aug-2007 at 4:42pm
Todd,
 
It is not true that the Null Entity will automatically be removed from any list owned by a ListManager.  It depends on the filter rule.
 
As an experiment, I tried the solution on EntityListManager in the Intermediate Tutorials under BindableList.  I saw that the NullEntity was added to the EntityList and I also saw that that this NullEntity was removed from the List.However, I then looked at the filter rule:
 

Boolean FilterOrdersByDate(Order pOrder) {

Customer currentCustomer = (Customer)mCustomersBS.Current;

return (pOrder.OrderDate.Value.Year == 1996 && pOrder.Customer == currentCustomer

}

The NulEntity was being filtered out because the NullEntity did not have an OrderDate of 1996.
 
I then changed the filter to allow the NullEntity.
 

Boolean FilterOrdersByDate(Order pOrder) {

Customer currentCustomer = (Customer)mCustomersBS.Current;

return (pOrder.OrderDate.Value.Year == 1996 && pOrder.Customer == currentCustomer

|| (pOrder.IsNullEntity));

}

Now the NullEntity is no longer being filtered out.
 
Back to Top
sieepvvalk View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2007
Location: United States
Posts: 7
Post Options Post Options   Quote sieepvvalk Quote  Post ReplyReply Direct Link To This Post Posted: 13-Aug-2007 at 5:13pm
Hi David,
 
Thanks for the reply.  My experience does not match up.  I have a generic procedure which gets the class I request, sorts, and then attaches a generic list manager to the class.  The code is as follows:
 

public static EntityList<T> RetrieveWithNull<T>(PersistenceManager pManager) where T : Entity

{

EntityList<T> entityList = pManager.GetEntities<T>(pManager);

entityList.Add(pManager.GetNullEntity<T>());

entityList.ApplySort("FullName", System.ComponentModel.ListSortDirection.Ascending, true);

entityList.ListManager = new EntityListManager<T>(pManager, delegate(T pEntity) { return true; }, null, entityList, true);

return entityList;

}

 

because the filter criteria always returns true, the null item should remain in the list...but it doesn't!  I'm still confused as to why this might occur...

Thanks,
Todd
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 14-Aug-2007 at 4:53pm

Actually, we were both right.

You are correct that if you create an EntityListManager with a NullEntity, then the NullEntity will be automatically removed even if the filter always evaluates to true.

My example is also correct in that you must create a filter which accepts the NullEntity.  However, the secret of my example working is that I add the NullEntity after the EntityListManager is created.  I tried this with your code, and now your NullEntity is no longer removed:
 

public static EntityList<T> RetrieveWithNull<T>(PersistenceManager pManager) where T : Entity {

      EntityList<T> entityList = pManager.GetEntities<T>();

      entityList.ApplySort("LastName", System.ComponentModel.ListSortDirection.Ascending, true);

      entityList.ListManager = new EntityListManager<T>(pManager, delegate(T pEntity) { return true; }, null, entityList, true);

      entityList.Add(pManager.GetNullEntity<T>());

      return entityList;

    }

Back to Top
sieepvvalk View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2007
Location: United States
Posts: 7
Post Options Post Options   Quote sieepvvalk Quote  Post ReplyReply Direct Link To This Post Posted: 15-Aug-2007 at 8:15am
I see - thanks for looking into it - I appreciate your help!
 
In the same regard - will the list maintain the null entity when it gets updated?  Say a record is added to the list - will the null entity stay around, or will it disappear?  Can you explain the underlying logic as to why the null entity is removed when we attach the list manager after adding it - I want to make sure that I don't run into this problem again in the future.
 
Thanks,
Todd
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 15-Aug-2007 at 2:18pm
There is a one time scan that happens when the EntityListManager is created.  If that scan finds a NullEntity, the NullEntity is discarded.  However, you can add a NullEntity at any later point, and as long as the filter doesn't discard the NullEntity, it will stick.
 
The underlying reason for discarding the NullEntity is that anything in an EntityList should also exist in the Persistence cache.  The NullEntity is a detached entity and, as such, is not considered to exist in the cache or anywhere else.
Back to Top
sieepvvalk View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2007
Location: United States
Posts: 7
Post Options Post Options   Quote sieepvvalk Quote  Post ReplyReply Direct Link To This Post Posted: 15-Aug-2007 at 2:20pm
Makes sense - thanks for clearing that up! I appreciate all your help David!
 
Todd
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down