Editable Root Collection

Editable Root Collection

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


st3fanus posted on Tuesday, January 19, 2010

Hi All...

I've tried to create two classes : Editable Root Collection and their child But in my case i make that child class as ROOT Object. I'm sorry about my post which is too long before :)

Editable Root Collection : TableList
Root Object : Table which is child of TableList, BUT the scenario is below :
I need to create a lot of Table object once --> So I used DataPortal_Create() in TableList to create them.

After retrieved Tables object and manipulated those table object, I need to save them all in once click.

My Decision in this situation is : I create DP_Create(), DP_Fetch(), DP_Insert(), DP_Update(),DP_Delete() so I suppose Table  as Root Object although it's Child of TableList ( I choose this way because I still not found a way to save Table object as Child under TableList), And then I HAVE TO OVERRIDE DP_UPDATE() on TableList object (cause i have tried to save them without override but error)

Is there any another solution which is better than I choose OR may be any feed back about my decision ??
Is My Decision is suitable with Csla Stereotype is created for ?? ( Or Is any another suitable stereotype?)

Thanks a lot

Stefanus

Marjon1 replied on Tuesday, January 19, 2010

Hi Stefanus,

I've not seen your previous post(s), but I think you may be getting confused as to how to when to use the EditableRootCollection. This is specifically designed to handle saving each item directly within a grid as the user makes changes and then moves away from a row. In this scenario you cannot think of the Root object as a child as it does behave like one, the normal Save() method is called on the object to perform it's save and call the standard DP methods as required.

Because you want to save all the items that have been created / edited at once you need to create a class that inherits from BusinessListBase and within that class you will override the DP_Create() method just like you've done and then will probably override DP_Update to add the transactional attribute, open your connection and depending on which version of CSLA you are using will do one of the following:

If you are on CSLA 3.2 or above (from memory) create Child_Insert, Child_Update, Child_Delete methods on the child object and then within you DP_Update in the list call Mybase.ChildUpdate()

If you are on an older version of CSLA then you will need to create your own methods on the child object and then call them manually as you loop through the deletedList and then through the items within the list and check if they need to be inserted/updated.

I would recommend either getting the latest CSLA training videos as they go into explaining how to do all of the above and when to use each business stereotype; the other option is the Expert Business Object book, which is an excellent resource but the videos are much easier to absorb especially if you are new to the framework.

If you need any other assistance, please feel free to email me directly at john.kennison@sybiz.com and can send you an example; just not at work at the moment.

John

st3fanus replied on Tuesday, January 19, 2010

Hi John..
I'm very appreciate your respond and your welcome for me.. :)
I used Csla 3.7

Yes I'm still confuse to use Csla correctly... I just have Business Object Book 2008 but i don't have video (I don't have enough money to buy it now :(

Sorry John I have wrote a mistake in my post probably..
What I mean with Editable Root Collection is same as what you suggest to me ( I take Editable Root Collection term from Business Object Book 2008 page 187 , as stereotype terms that Csla is support.)

"Because you want to save all the items that have been created / edited at once you need to create a class that inherits from BusinessListBase and within that class you will override the DP_Create() method just like you've done and then will probably override DP_Update to add the transactional attribute, open your connection and depending on which version of CSLA you are using will do one of the following:
If you are on CSLA 3.2 or above (from memory) create Child_Insert, Child_Update, Child_Delete methods on the child object and then within you DP_Update in the list call Mybase.ChildUpdate() "

I acctually want to do this way first, but  I don't know a way to called Child_Insert, Child_Update, Child_Delete from my TableList which is inherit from BusinessListBase as you suggest for me ( call MyBase.ChildUpdate() ) So I force my design to change my Table class to act as a Root that become a child of TableList. So within my Table class I implement DP_Create, DP_Fetch,DP_Insert, DP_Update, DP_Delete. And then in TableList within DP_Update() I call :

This Code is located in Table Class :
        protected override void  DataPortal_Update()
        {
            int _noT = ReadProperty(NoProperty);
            string _nameT = ReadProperty(NameProperty);
            short _floorT = ReadProperty(FloorProperty);
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("Resto_DB"))
            {
                using (TableDal dal = new TableDal(ctx))
                {
                    dal.UpdateTable(new TableDTO(_noT, _nameT, _floorT));
                }
            }
        }


This Code is Located In TableList:
[Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            foreach (Table tbl in this)
            {
                tbl.Save();
            }
        }


Sorry John I just want to get a confirmation ChildUpdate() you wrote above , Is it Child_Update() ?
Am I really can contact you privately ? :)

Thanks a lot John  about your respond and your welcome :)


stefanus

Marjon1 replied on Wednesday, January 20, 2010

You can feel free to contact me privately if you'd like, but have made some changes below. I apologize that the formatting isn't very nice. The Child_Insert, Child_Update, Child_Delete methods are just methods that you create yourself and are called on your behalf by the parent list (TableList) .

This Code is located in Table Class :
internal void Child_Update()
{
int _noT = ReadProperty(NoProperty);
string _nameT = ReadProperty(NameProperty);
short _floorT = ReadProperty(FloorProperty);
using (var ctx = ConnectionManager.GetManager("Resto_DB"))
{
using (TableDal dal = new TableDal(ctx))
{
dal.UpdateTable(new TableDTO(_noT, _nameT, _floorT));
}
}
}

This Code is Located In TableList:
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
using (var ctx = ConnectionManager.GetManager("Resto_DB"))
{
base.ChildUpdate()
}
}

An object that is marked as a child can never be saved with a call like tbl.Save(), the framework will not allow it. The above example, will only allow an object to be updated, never actually inserted or considered new. This may be perfectally fine for your application, but should be highlighted none the less.

Depending on how you are creating your table objects to begin with you may have code similar to this:


This Code is located in Table Class :
protected override void Child_Fetch()
{
//do whatever you need to as part of your fetch
}

This Code is Located In TableList:
protected override void DataPortal_Create()
{
//This is not a complete example, do you use verbatim
using (var ctx = ConnectionManager.GetManager("Resto_DB"))
{
this.Add(DataPortal.FetchChild())
}
}

The important part is the use of the DataPortal.FetchChild which will call your Child_Fetch method on the Table object and return (and you can provide paramaters if required).

Copyright (c) Marimer LLC