A
aa7im
I have a similiar thread going about this topic but I decided to break
it off into a seperate discussion.
Question:
What is the best way to determine if an object "IsNew" to a collection?
Problem:
In my application I load a collection of objects from the database and
then pass that collection to the user.
Order.OrderRows // Returns a collection of OrderRows from DB.
If a user does something like: Order.OrderRows.Add(newRow) what is the
best way to determine which rows have been added? I need to this so
that I know whether to persist the relation to the database...
Does my collection need to internally maintain lists of "Added" and
"Deleted" objects so that I can then request a "Changed" list from the
collection like:
Order.OrderRows.GetAdded();
Order.OrderRows.GetDeleted();
===================================================
Or would it be better to wrap all objects in the collection in some
sort of "Item Wrapper" like:
CollectionItem item = new CollectionItem();
item.Value = OrderRow;
item.Status = ItemStatus.Deleted;
So that collection internally would always internally be storing a
collection a CollectionItems (Ok I need better names ;-) ).
So an Add() method on the strongly-typed collection would look like:
public void Add(OrderRow row)
{
List.Add(new CollectionItem(row,item.Status.Added);
}
and to get at items in the collection would look like:
public OrderRow this[int index]
{
get
{
return (OrderRow)((CollectionItem)List[index]).Value;
}
}
Methods like Contains and IndexOf get kind of weird though because then
I have to manually loop through the collection like:
public int IndexOf(OrderRow row)
{
for(int i = 0; i < List.Count; i++)
{
if(List.Value.Equals(row))
{
return i;
}
}
}
Any other ideas? Or am I going about this all wrong?
it off into a seperate discussion.
Question:
What is the best way to determine if an object "IsNew" to a collection?
Problem:
In my application I load a collection of objects from the database and
then pass that collection to the user.
Order.OrderRows // Returns a collection of OrderRows from DB.
If a user does something like: Order.OrderRows.Add(newRow) what is the
best way to determine which rows have been added? I need to this so
that I know whether to persist the relation to the database...
Does my collection need to internally maintain lists of "Added" and
"Deleted" objects so that I can then request a "Changed" list from the
collection like:
Order.OrderRows.GetAdded();
Order.OrderRows.GetDeleted();
===================================================
Or would it be better to wrap all objects in the collection in some
sort of "Item Wrapper" like:
CollectionItem item = new CollectionItem();
item.Value = OrderRow;
item.Status = ItemStatus.Deleted;
So that collection internally would always internally be storing a
collection a CollectionItems (Ok I need better names ;-) ).
So an Add() method on the strongly-typed collection would look like:
public void Add(OrderRow row)
{
List.Add(new CollectionItem(row,item.Status.Added);
}
and to get at items in the collection would look like:
public OrderRow this[int index]
{
get
{
return (OrderRow)((CollectionItem)List[index]).Value;
}
}
Methods like Contains and IndexOf get kind of weird though because then
I have to manually loop through the collection like:
public int IndexOf(OrderRow row)
{
for(int i = 0; i < List.Count; i++)
{
if(List.Value.Equals(row))
{
return i;
}
}
}
Any other ideas? Or am I going about this all wrong?