duplicating a row in a business object

duplicating a row in a business object

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


pagolas posted on Tuesday, February 17, 2009

hello all

i have a form that executes a search based on the users entries and returns a line of info about the certain business object ex Name of order, date, quantity ...all of this is displayed in a data grid. I would like to be able to duplicate a row. How would i be able to do this. I know i can add a button that says duplicate?

Antonio Sandoval replied on Tuesday, February 17, 2009

BussinesBase implements a method named Clone (Csla.Core.ObjectCloner), but it method uses Serialization, so everything that is not marked as NonSerializable will be copied, including binding status, edit level, primary keys, old, new dirty etc. Depending on what you need to do with the new row you can use clone, or Csla.Data.DataMapper. DataMapper will copy only the public properties of the root, it does not include any children.
How is that you can get the BO from the grid, it depends of the datagrid that you are using, check the documentation, for example for a DataGridView:

if(datagrid.SelectedRows.Count>0){
    BO source= datagrid.SelectedRows[0].DataBoundItem;
    BO dest = source.Clone();
    BO dest2 = BO.NewBO();
   Csla.Data.DataMapper.Map(source,dest2);
}
 
Other way is using a CurrencyManager and it does not depends of the Datagrid

pagolas replied on Wednesday, February 18, 2009

so where would i apply this method...in the search results that bind to the grid?

pagolas replied on Wednesday, February 18, 2009

ohh ok so i should put this in the button that i intend to add in order to clone the object huh...if so im so excited this might work

SonOfPirate replied on Thursday, February 19, 2009

We include the notion of duplication in our object models.  In our case, our primary concern and reason for not directly cloning the object was that we wanted the new instance to have its own unique identifier.

Our collection class includes a DuplicateItem method that accepts the index/identifier of the item to duplicate and does all of the work.  It looks for the ICloneable interface on the item and, if found, does as described above; otherwise, we delegate to the ObjectCloner directly.  Oh, we also automatically append the new item to the collection before returning it to the caller.

So, following your use-case, you would simply pass in the identifier from the row selected in the grid to the DuplicateItem on the grid's datasource (collection) and the rest would happen automatically (because a ListChanged event is raised causing the grid to refresh and show the new, duplicate item).

Hope that helps.

 

pagolas replied on Monday, February 23, 2009

errrrr ok kinda sounds like greek to me i have to do some more studying...................the identifier? meaning the business object...............i want to introduce a button that will be labled duplicate item. I am assuming i will put this code in the event of this button right? if you could explain in more detail i would greatly appreciate.....i am a newbie and amatuer at csla concepts....i understand c# but not the whole framework

thanks

 

SonOfPirate replied on Wednesday, February 25, 2009

Presumably you are persisting your business object data to some data store such as SQL Server, Oracle, etc.  As such, you have some way of uniquely identifying each object (row) so that you can retrieve the information.  This value is typically used in your UI to reference the appropriate record when you want to do something on a specific item (row) like editing, deleting or copying.  When you use data-binding, you will indicate this property as the key value for each item.

This is not to be confused with the index value which is the numeric location of the object within an array or list - which can also be used to identify the item you want to work with.

In our case, all of our business objects have a Guid property as their unique identifier.  We overload methods on our collections that allow us to retrieve items by this value and use this property as the key when binding the collections to UI grids.

When we implement duplication, we also place a button on our form that, when clicked, obtains the key value for the selected item in the grid then calls the DuplicateItem() method on our business collection which does the rest of the work.  For example, in an ASP.NET application with a GridView control bound to our business collection, we would have something like:

void DuplicateButton_Click(object sender, EventArgs e)
{
    myCollection.DuplicateItem(GridView1.SelectedValue as System.Guid);
}

Hope that helps.

 

pagolas replied on Thursday, February 26, 2009

so it seems i have to dig deeper in the code and understand the code behind the databinding...i think i shall do this.....would it help is i showed you some of the code that i am dealing with because i understand what ur saying but i am still kind of lost

pagolas replied on Thursday, February 26, 2009

hey ..... where do you define the duplicateItem method...i have tried to complie and run the program but it failed....becuase it said that the duplicate method is not delcared anywhere. Do you inherit it from the ObjectCloner interface?

pagolas replied on Thursday, February 26, 2009

hey just for you to see...this is an extract of my code and what it contains

private void button1_Click(object sender, EventArgs e)

{

myList.DuplicateItem(myListDataGridView.CurrentRow.DataBoundItem);

}

myList is my collection of objects. It shows up in the datagrid when you do the search.

below is the code that shows the data binding nature of my myList

 

private void SortAndBindResults(myList list)

{

// clear previous results

myListBindingSource.DataSource = null;

if (list.Count > 0)

{

Csla.SortedBindingList<myListInfo> sortedList = new Csla.SortedBindingList<myListInfo>(list);

//sortedList.ApplySort("IsWorkCommitted", ListSortDirection.Descending);

sortedList.ApplySort("myListId", ListSortDirection.Ascending);

myListListBindingSource.DataSource = sortedList;

myListDataGridView.Focus();

}

else

{

MessageBox.Show("No matching lists found",

"No matching lists", MessageBoxButtons.OK);

}

}

Copyright (c) Marimer LLC