What's the best way to store an unsaved object graph in memory?

Posts   
 
    
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 24-Jul-2007 19:49:34   

Basically I want to be able to hold related entities of the SelectedOrderEntity which are not yet saved.

I think a code example explains best what i'd like to do but which is not possible, written this way at least. Ideally I would like to be able to write something like this:



OrderEntity o = control.SelectedOrderEntity;

//this is not currently possible as this is a read-only collection used for returning object graphs
o.OrderLineItemDetailCollectionViaOrderDetails.Add(orderlineitemControl.SelectedOrderLineItem)


Again for clarity, what i'd really like to have when i'm done asking for the control.SelectedEntity is an entity which contains the entire graph of objects needed:



OrderEntity o = control.SelectedOrderEntity


//now control.SelectedOrderEntity would execute some code such as



public OrderEntity SelectedOrderEntity
{
    get 
        {
              OrderEntity e = (OrderEntity)ViewState["OrderEntity"];
              foreach (OrderItem item in myListOfSavedItems)
              {
                   e.OrderItemCollectionViaSomeIntermediary.Add(item);
              }
              return e;
        }

}


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2007 09:42:06   

I think you are looking for a way to add an Entity A to another entity B when they have m:n relationship, right?

Please check the following question, in the manual's section "Best practises - How do I ... ?" How do I add an entity A to an entity B's collection of A's if A and B have an m:n relation ? Which shows the following solution:

DepartmentEmployees newDepEmp = new DepartmentEmployees();
newDepEmp.Employee = employee;
newDepEmp.Department = department;
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 26-Jul-2007 00:51:08   

That doesn't really answer the question. I don't want to rewrite the whole thing, please read and get back to me.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Jul-2007 08:40:58   

You can do something like:

OrderEntity o = control.SelectedOrderEntity;

//this is not currently possible as this is a read-only collection used for returning object graphs o.GetMultiOrderLineItemDetailCollectionViaOrderDetails.Add(orderlineitemControl.SelectedOrderLineItem, false)

the second parameter says: not force a refetch, leave the entityCollection as it is actually.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 26-Jul-2007 11:32:04   

KastroNYC wrote:

That doesn't really answer the question. I don't want to rewrite the whole thing, please read and get back to me.

IMHO it does answer your question, as you want to add an entity to an m:n relation, correct? You can also store unsaved entities into a Unitofwork object, which is designed for that purpose.

Frans Bouma | Lead developer LLBLGen Pro
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 27-Jul-2007 18:35:44   

daelmo, that's what i've been looking for, thanks.