Editable Root Collection

Editable Root Collection

Old forum URL: forums.lhotka.net/forums/t/927.aspx


RangerGuy posted on Thursday, August 17, 2006

I need to make Root object that this is a collection of other objects.  So in the update method would it be ok to create the connection to the database in the overridden method DataPortal_Update() of the Collection object then reuse that connection to work with the objects in the collection like so:

 

protected override void DataPortal_Update()

{

RaiseListChangedEvents = false;

using (SqlConnection cn = new SqlConnection(Database.CustomerDB))

{

foreach (Customer cust in DeletedList)

cust .DeleteSelf(cn);

DeletedList.Clear();

foreach (Customer cust  in this)

if (cust .IsNew)

cust .Insert(cn,this);

else

cust .Update(cn,this);

RaiseListChangedEvents = true;

}

}

vargasbo replied on Thursday, August 17, 2006

Since a root object owns the collection of X. I would put the logic in X as such
internal void Update()
        {
          this.RaiseListChangedEvents = false;
          // update (thus deleting) any deleted child objects
          foreach (Unit obj in DeletedList)
            obj.DeleteSelf(obj);
          // now that they are deleted, remove them from memory too
          DeletedList.Clear();

          // add/update any current child objects
         
          foreach (Unit obj in this)
          {
              if (obj.IsNew)
                  obj.Insert(obj);
              else
                  obj.Update(obj);
          }
          this.RaiseListChangedEvents = true;
        }

Regards,

rasupit replied on Thursday, August 17, 2006

RangerGuy,

All root objects including editable root list uses DataPortal_Update method so your implementation is correct.

Create connection in this method is also an acceptable method.  In fact its a good way to avoid dtc from kicks in sql2k5 when using TtransactionScope.

You don't need to pass your current object (list object) when calling insert or update method though.

Ricky

Copyright (c) Marimer LLC