How insert a new child object?

How insert a new child object?

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


shahram posted on Thursday, July 06, 2006

Hi!

I would ask if somebody can show an example code of how to insert a new child editable object.

Regards

shahram replied on Friday, July 07, 2006

I am sure somebody knows the CSLA way to add-insert new child objects with identity id PK, but who?

Let's wait and seeWink [;)]

ajj3085 replied on Friday, July 07, 2006

Usually the new child is added when its internal 'SaveSelf ' is called, after the user indicates they want to save the root object they are working with.

If you MUST insert the row as soon as the user adds a new child, you can override AddNew in the collection, like so:

public class MyList : BusinessListBase<MyList, MyChild> {

        // other code

       protected override object AddNewCore() {
              MyChild result;

              result = MyChild.NewChild();
              OnAddingNew( new AddingNewEventArgs( result ) );
 
              Add( result );
           
              return result;
        }
}

public classs MyChild : BusinessBase<MyChild> {
          public static MyChild NewChild() {
               return DataPortal.Create<MyChild>( new EmptyCriteria() );
          }

          private class EmptyCriteria : CriteriaBase {
                    public EmptyCriteria() : base( typeof ( MyChild ) ) { }
           }

          private void DataPortal_Create( EmptyCriteria crit ) {
                    // ADO.Net code inserts row
                    MyPK = (int)cmd.Parameters[ "@NewId" ];
          }
}


HTH
Andy

matt tag replied on Friday, July 07, 2006

You should not need to insert the child into the database right away - so there is no need to know the child object's PK right away.

Consider this example - when you open Word or Excel and select "New", does anything get written to disk right away?  It does not. The new document stays in memory until the user hits "Save", only then does the object get written to disk and assigned a "primary key" (in this case, a file name in the file system).

CSLA works the same way. When the user clicks "Add new Object" - nothing should get written to the database at that time. A new CSLA object gets created in memory and is ready for editing by the user. Only when the user says "Save" on the root object does anything get written to the database.


matt tag

shahram replied on Friday, July 07, 2006

First I must say thank you for your efforts to answer my questaion.

Probably it is me! but it doesn't matter how long you wait before you save the new child object to DB. Hopfuly you do that eventually. What happens after that is my problem because when I call rootobject.Save() to insert the new child object that reference that I have to child object is invalid and must find a new refference to child object but to get that I must know the identity id that is return by insert SP and UI developer is wondering how he can get it back!

 

ajj3085 replied on Friday, July 07, 2006

What does your UI code look like?  Usually you don't need to keep track of individual children, since the collection they are in is databound to a grid.

shahram replied on Friday, July 07, 2006

I use SCSF(http://www.gotdotnet.com/codegallery/codegallery.aspx?id=941d2228-3bb5-42fd-8004-c08595821170) and my UI is a windows form with textbox and labels and combobox like any other UI.

When user has open the insert new contact form he inputs some data like name and address and then he pushes insert and in code Rootobject.Save() so contact becomes inserted in DB but user expexts after clicking the insert button the form is kept alive(rebounded to contact oblect with the newly identity) so user can continue to edit and do what he just wants!

The issue is after rootobject.save() no reference is left to rebound to the form!

ajj3085 replied on Friday, July 07, 2006

Assuming your text boxes are bound to a binding source, you'd do this:

rootobject = rootobject.Save();
rootBindingSource.DataSource = rootobject;

That will rebind everything.

So where is this child object you're talking about? 

Michael Hildner replied on Friday, July 07, 2006

Don't you have to set .DataSource to null first before setting it to your new object?

I've been doing that because the book says so (C# 2.0 page 492). Honestly I've never tried not doing it, but the book says it will still be bound to the old object if you don't.

Mike

ajj3085 replied on Friday, July 07, 2006

Oh, you may be right there.  I don't use remoting though, so it hasn't been a problem for me (yet).

Andy

RockfordLhotka replied on Friday, July 07, 2006

You need to look at Chapter 9, as I walk through the process of supporting an "Apply" button that does a save and also keeps the form open.

shahram:

The issue is after rootobject.save() no reference is left to rebound to the form!

Just be aware that there is some errata around the content in the book - specifically that you should avoid calling BeginEdit/ApplyEdit directly on the business object, and instead should call EndEdit on the bindingsource instead. Details are in other threads here, or on www.lhotka.net/cslanet

Copyright (c) Marimer LLC