The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Clone Method

Last post 08-23-2008, 2:31 AM by iVirtualDocket. 8 replies.
Sort Posts: Previous Next
  •  02-26-2007, 7:15 AM 569

    Clone Method

    Did I read somewhere of a method to clone an Item? If so I can't find it now.

    A feature I would find useful would be the ability to clone the current item. The scenario is data entry where many of the values from one entry to the next are the same with little changing.

    User clicks "Clone". Code adds a new item to the collection and populates fields with values from current item (excluding a key field (AutoInc)).  The user is then in a situation as if they had clicked new and typed in all the data and is now ready to click Save.

    Perhaps this is a little off the wall but my thinking here is that if a new field is added to the table and the ES classes are re-generated then it would be very easy to "forget" to update the code cloning the item manually. The result would be missing data from the clone. Of course this would be caught in testing Wink

    Currently I do something like:

    Code:
            Dim newItem As New myItem
            newItem = Me.bs.Current
    
            bs.AddNew()
    
            ' Copy data from cloned item to form controls
    With newItem Me.FirstNameTextBox.Text = .FirstName ... End With ' Focus on first data entry field

     

  •  02-28-2007, 7:42 AM 633 in reply to 569

    Re: Clone Method

    If i am not wrong I think Mike will do this in another way, but this function works: 

    #region Clone

    /// <summary>

    /// Clone a object

    /// </summary>

    /// <param name="objetoClonar"></param>

    /// <returns>New cloned object</returns>

    public static Object Clone(Object objetoClonar) {

    // Serialize

    BinaryFormatter bf = new BinaryFormatter();

    MemoryStream ms = new MemoryStream();

    bf.Serialize(ms, objetoClonar);

    // Deserialize into a new entity

    ms.Position = 0;

    Object objetoClonado = bf.Deserialize(ms);

    ms.Close();

    return objetoClonado;

    }

    #endregion

    To use this function simply call:

    Employee employee1 = New Employee();

    Employee newEmployee = (Employee) Clone(employee1);

     

     

  •  02-28-2007, 12:58 PM 649 in reply to 633

    Re: Clone Method

    liffey, Adding a Clone method is on our enhancements list, but not scheduled for 1.6. paschoal1, thanks for sharing that.
    David Neal Parsons
    www.entityspaces.net
  •  06-06-2007, 2:27 PM 3077 in reply to 633

    Re: Clone Method

    I've tried this code on the Entity(Category) and the object does get recreated, but the there is no data inside the object. The entity esCategory does inherity from EntityBase.

    Do you have any suggestions as to why none of the data would come across, but the object definition does?

  •  11-23-2007, 8:00 AM 6802 in reply to 649

    Re: Clone Method

    Hi,

     Can you tell me if the Clone method made it into ES2007, and if not, is it still on your roadmap?

     

    thanks

     

  •  11-24-2007, 5:43 AM 6807 in reply to 6802

    Re: Clone Method

    It is in the roadmap still but hasn't been implemented, I can show you how to do a work around, I'll post back here later today.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  11-24-2007, 6:31 AM 6808 in reply to 569

    Re: Clone Method

    Hi,

    here's what I think might work, please note that I haven't tested it:

     In your entity custom class place this function :

    Code:
            Public Function Clone() As YourEntityType
                Dim oEntity As New YourEntityType
                'Give this new object a 'copy' of the underlying datatable
                oEntity.Table = Me.Table.Clone
                'Mark all as added so that the auto inc PK is created upon INSERT
                oEntity.MarkAllColumnsAsDirty(DataRowState.Added)
                Return oEntity
            End Function

    hope it works.


    Thanks!

    - El pipo -
  •  08-22-2008, 9:22 AM 10875 in reply to 6807

    Re: Clone Method

    I still don't see a Clone method in ES 2008.  Is this still on the roadmap?  Also, what about cloning a collection?

    ES 2008.1.1110.0
    VS 2008 Pro SP1 on Vista Ultimate x64
    SQL Server 2000
  •  08-23-2008, 2:31 AM 10881 in reply to 10875

    Re: Clone Method

    Code:
    1    
    2                    // Load the collection
    3                    TblCompanyCollection local = new TblCompanyCollection();
    4                    local.LoadAll();
    5    
    6                    // Iterate through the stub and update the collection
    7                    Proxy.TblCompanyCollectionProxyStub download = ((Proxy.TblCompanyCollectionProxyStub)stub);
    8    
    9                    // Configure the sub progress bar
    10                   int position = 0;
    11                   ConfigureSubBar(download.Collection.Count);
    12                   foreach (Proxy.TblCompanyProxyStub item in download.Collection)
    13                   {
    14   
    15                       // Attempt to load the local collection from the items primary key
    16                       TblCompany record = local.FindByPrimaryKey((Guid)item.CompanyId);
    17                       if (record == null)
    18                       {
    19                           record = local.AddNew();
    20                       }
    21   
    22                       record.CompanyId = item.CompanyId;
    23                       record.Active = item.Active;
    24                       record.Description = item.Description;
    25                       record.Password = item.Password;
    26   
    27                       // Incremement the sub progress
    28                       UpdateProcessBarSub(ref position);
    29   
    30                   }
    31   
    32                   // Save the collection
    33                   local.Save();
    
     
    I'd find this especially useful too, as I have to do the above for each one of my tables.

    Current Projects:
    iVirtualDocket | iWontRemember
View as RSS news feed in XML