More in this section

Forums / Developing with Sitefinity / Fun with Nolics - Object Copy

Fun with Nolics - Object Copy

3 posts, 0 answered
  1. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    05 Mar 2008
    Link to this post

    Here is another neat little function of Nolics that I thought I would share. It is useful if you need to make a direct copy of an object.

    Here is how you call it.

    Dim transaction As New Transaction     
    Dim item As New YourNolicsObjectClass     
        
    Utility.CopyOdbObject(YourNolicsObjectClassInstance, item)     
    OdbClass.Create(item, transaction)     
    transaction.Commit()     
        
    YourNolicsObjectClassInstance = item    
     

    And here is the function:

    Public Shared Function CopyOdbObject(ByVal oldItem As ObjectByVal newItem As ObjectAs QuoteJobOrder  
                Try 
     
                    Dim cInfo As OdbClassManager.OdbClassInfo = OdbClassManager.GetClassInfo(oldItem.GetTypeTrue)  
     
                    For Each fva As FieldValueAttribute In FieldValueAttribute.GetValueTypes(oldItem.GetType())  
                        ' Skip all non database fields from export. If a field like link or relation don't   
                        ' have actual database field, GetPersistType() returns a type of void.  
                        If fva.Transient = True OrElse fva.GetPersistType() Is GetType(Void) Then 
                            Continue For 
                        End If 
     
                        If Not cInfo.IsPrimaryKey(fva.EffectiveFieldName) Then 
                            Dim fdp As New FieldPropertyDescriptor(fva)  
                            fdp.SetValue(newItem, fdp.GetValue(oldItem))  
                        End If 
     
                    Next 
     
                    Return newItem  
     
                Catch ex As Exception  
                    Return Nothing 
                End Try 
            End Function 

    Notice how you don't create the new object until after the copy is complete. This has to do with Nolics cache. This works great objects with lots of fields or objects that are updated frequently. You could easily add recursion for related (child) objects.  I hope this comes in handy to someone out there in Nolics land.

    Sincerely,

    Ben


  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    05 Mar 2008
    Link to this post
    Hi Ben,

    Thank you very much for notifying us, it is really appreciated. We are always happy to receive your ideas and suggestions. Your Telerik points have been updated.

    Sitefinity team wishes you all the best!

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. bnye
    bnye avatar
    332 posts
    Registered:
    22 Sep 2005
    05 Mar 2008
    Link to this post
    Woops the function should return a generic Object and not the QuoteJobOrder as shown below.

    Sincerely,

    Ben
Register for webinar
3 posts, 0 answered