Copy & Paste Business Object on Clipboard

Copy & Paste Business Object on Clipboard

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


RolandoTonin posted on Wednesday, May 29, 2013

Hy,

I want to implement in my application (WPF user interface) copy and paste from/to separated views where Items view are business object (or better ui element with BO behind).

My business layer is based on CSLA that has a specific factory object as root of each different chain where business object are child elements (child XXXBOList with child XXXBO object with a classic tree structure). When a specific BO has some other child BO objects (through child BOList) it demand to factory to load and get those child BO objects, passing ID as parameter. 

So, when user select items from a listview (for example) and clicks copy (command) my code extracts child BOs from a BOList, it clones (Clone() method) them, and it copy  them into an ArrayList before load this list on Clipboard with Clipboard.SetaData() method.

Next, the user moves to another view and clicks paste. My code extracts BO items from Clipboard.GetData() method and then tries to rebuild new BOs with the same property values of source BOs.

All work fine except when source BO contains lazyload properties that require a factory call to get those child object. In this case factory root object is no longer "Parent" of those pasted objects and so factory parent object is null and I cannot rebuild those objects.

I hope I was clear to explain my issue. How cai I resolve it?

Best regards

 

ColourHaze replied on Thursday, May 30, 2013

Just a question and a quick thought:

Does the user only select the child items, or also modify them?

in the first case couldn't you just simply copy the object path, for example by the identities of the object path? This way you can simply request the root and its children in order of identity.

Another option would maybe, also interesting if you do modify the data, is to add an initializer method for setting the properties from the arraylist data. Of course for this to work you need to solve the null root first. 

HTH

JonnyBee replied on Tuesday, June 04, 2013

Hi,

I don´t quite understand the entire problem. Can you provide us a small working sample to showcase the issue?

RolandoTonin replied on Wednesday, June 05, 2013

Hy,

My Business Framework layer is based on factory root object and many child objects "linked" to root object by childList. Child object, like TaskBO for example contains reference to other objects that are neither Parent nor TaskBO . Therefore to resolve loading of those objects in my framework I use a l"azy load pattern" in TaskBO (in this case = this) class like:

[NotUndoable]
private Guid GuidCommessaFaseTask = Guid.Empty;
public static readonly PropertyInfo<CommessaFaseTaskBO> CommessaFaseTaskBOProperty = RegisterProperty<CommessaFaseTaskBO>(c => c.Task, RelationshipTypes.PrivateField);
[NotUndoable]
private CommessaFaseTaskBO _CommessaFaseTaskBO = CommessaFaseTaskBOProperty.DefaultValue;
public CommessaFaseTaskBO Task
{
    get
            {
                if (_CommessaFaseTaskBO == null && this.GuidCommessaFaseTask != Guid.Empty)
                {
                    this._CommessaFaseTaskBO = this.Get<CommessaFaseTaskBO>(this.GuidCommessaFaseTask);
                }
                return GetProperty(CommessaFaseTaskBOProperty, _CommessaFaseTaskBO);
            }
            set { SetProperty(CommessaFaseTaskBOProperty, ref  _CommessaFaseTaskBO, value); }
        }  

 the method: "T this.Get<T>(Guid rowGuid)" route the call to the root object (factory object) that load T object (if it is not just in memory) and return the object's reference to the caller.

So eveything works fine until the chain between factory object (root) and child caller object isn't interrupted. Right?

My user interface app is a WPF application based on a "MDI like" interface with many child WPF views and where I would like to implement copy & paste (via Clipboard). So:

phase 1: the user select some TaskBO objects in a view and click copy command: my code create an ArrayList and fill it with selected TaskBO and passa the arraylist to Clipboard

DataObject data = new DataObject();
                ArrayList ls = new ArrayList();
                SelectedItems.ForEach(tf => ls.Add(tf.OggettoBO));
                data.SetData(typeof(TaskBO).FullName, ls);
                Clipboard.SetDataObject(data);

phase 2: the user switch to another view and click paste command: my code recovers the arraylist from the clipboard and try to insert the TaskBO in these view:

ArrayList data = (ArrayList)Clipboard.GetData(typeof(TaskBO).FullName);
this.factoryBO.CreateTaskFromOtherList((IEnumerable<TaskBO>)data, this.DataRiferimento.Date);
            
Therefore I try to recreate these object and relative child objects in a new list. All work fine if CommessaFaseTaskBO was just created before copy command but if it is not I cannot recreate it because GuidCommessaFaseTask is private and I don't want to public it.
I hope I've clearified you.
Best Regards
I hope I've clarified 

Copyright (c) Marimer LLC