PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 Populate an object's properties from another object's properties-VBForums
Results 1 to 9 of 9

Thread: Populate an object's properties from another object's properties

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    67

    Question Populate an object's properties from another object's properties

    Hi

    I've written a Windows Service which takes its configuration settings from a database. Because the service is running constantly, I need to be sure that the configuration that's being used is always as up to date as possible and will react to any changes in the back-end database within a short period of time.

    My intention is to log any received changes so that at a glance I can review the logs and see when a change was made and what the substance of that change was, but I think I've got myself tangled up somewhere.

    The way I've set this up (and I'm happy to hear criticisms on it) is that when the service runs, a stored procedure is executed which retrieves all of the relevant configuration values from the database into a table which is held in memory. The RunningConfiguration object (which currently contains 11 properties of varying types) is then instantiated and the values of the properties set.

    At regular intervals (currently every 15 minutes), a copy of the configuration object is instantiated (PreviousConfiguration) and populated from the existing configuration, after which the RunningConfiguration is instantiated and populated again. After that takes place, the corresponding properties in both the RunningConfiguration and PreviousConfiguration objects are compared, and any variances stored in my database for future reference.

    This is entirely my lack of knowledge at play here, but I'd like to have a single Configuration object that can be cloned for the Running and Previous configurations. I'd also like to find a way of consuming both configuration sets and comparing them without needing to refer to specific properties by name. So far I can loop through the RunningConfiguration object and get it to give me the name, type and value of the property and I can loop through the PreviousConfiguration object and find the corresponding property name and verify that the type hasn't changed, but I've been singularly unable to write the value of the property from RunningConfiguration into the corresponding property on PreviousConfiguration.

    Any help would be gratefully appreciated.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,217

    Re: Populate an object's properties from another object's properties

    While it is possible to do the things you want via reflection, it is far easier to write it out (especially with only 11 properties), and it also makes the code easier to read.

    In terms of cloning an object, create a function called Clone within the class that returns an object of the same class, eg:
    Code:
    Class MyClass
        ...
    
        Function Clone() as MyClass
    
            Dim clonedObject as New MyClass
    
            clonedObject.PropertyA = Me.PropertyA
            clonedObject.PropertyB = Me.PropertyB
            ...
            clonedObject.PropertyL = Me.PropertyL
     
            Return clonedObject 
    
        End Function
    In terms of comparing, you can create a function within the class that takes an instance of the class of the parameter, and checks if they are the same, eg:
    Code:
        Function IsEqual(compareTo as MyClass) as Boolean
    
            If compareTo.PropertyA = Me.PropertyA AndAlso
               compareTo.PropertyB = Me.PropertyB AndAlso
               ...
               compareTo.PropertyL = Me.PropertyL Then
                 Return True
            Else
                 Return False
            End If
    
        End Function
    Copying values over is simple enough, so I'm sure you can work that part out.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,053

    Re: Populate an object's properties from another object's properties

    If you're using SQL Server then you can use a SqlDependency object to get immediate push notifications from the database when a change occurs and do away with the polling altogether.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    67

    Re: Populate an object's properties from another object's properties

    Quote Originally Posted by si_the_geek View Post
    While it is possible to do the things you want via reflection, it is far easier to write it out (especially with only 11 properties), and it also makes the code easier to read.

    In terms of cloning an object, create a function called Clone within the class that returns an object of the same class, eg:
    Code:
    Class MyClass
        ...
    
        Function Clone() as MyClass
    
            Dim clonedObject as New MyClass
    
            clonedObject.PropertyA = Me.PropertyA
            clonedObject.PropertyB = Me.PropertyB
            ...
            clonedObject.PropertyL = Me.PropertyL
     
            Return clonedObject 
    
        End Function
    In terms of comparing, you can create a function within the class that takes an instance of the class of the parameter, and checks if they are the same, eg:
    Code:
        Function IsEqual(compareTo as MyClass) as Boolean
    
            If compareTo.PropertyA = Me.PropertyA AndAlso
               compareTo.PropertyB = Me.PropertyB AndAlso
               ...
               compareTo.PropertyL = Me.PropertyL Then
                 Return True
            Else
                 Return False
            End If
    
        End Function
    Copying values over is simple enough, so I'm sure you can work that part out.
    Thanks for getting back to me. The reason I was hoping to find a slicker way of doing it is because once I get this service working the way I want to I plan to retrofit existing services to use the same approach. Some of those services contain far more than the 11 properties, so I’m trying to cut down the amount of code I’m writing, especially since I already need to create the two objects and ensure that the data types match throughout.

    My eventual intention is to have a DLL containing a method which will consume two objects: the first one being the object to be cloned and the second being the cloned result. This second object would then be read back to the calling application for subsequent analysis on any changes. I may also look to create a method which consumes two objects and returns a third which shows which properties have been altered.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    67

    Re: Populate an object's properties from another object's properties

    Quote Originally Posted by jmcilhinney View Post
    If you're using SQL Server then you can use a SqlDependency object to get immediate push notifications from the database when a change occurs and do away with the polling altogether.
    I AM using SQL2012 but didn’t know there was a way of generating push notifications in the way you’ve described. Such a technology would be extremely useful, not only for controlling and managing configuration changes but also in the general running of the services themselves.

    To put you in the picture I’ve been working quite heavily on building systems for generating outgoing email and SMS. My users get a forms app which allows them to turn a text file of data into a series of outgoing emails (for example). Part of the process schedules the emails throughout the working day. Where the service comes in is to actually start the send process at the allotted time: at present the service polls the database at regular intervals. If there’s nothing to do it waits until the next pass. However if there are messages to be sent they are extracted from the database and processed sequentially, after which the email record is time stamped to prevent duplication.

    Would you know of any online tutorial resources that I could use to learn how to harness the technology you speak of?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,053

    Re: Populate an object's properties from another object's properties

    Quote Originally Posted by ianbhenderson73 View Post
    Would you know of any online tutorial resources that I could use to learn how to harness the technology you speak of?
    I'm afraid I don't. I've never actually used the class myself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    67

    Re: Populate an object's properties from another object's properties

    Quote Originally Posted by jmcilhinney View Post
    I'm afraid I don't. I've never actually used the class myself.
    I’ll do some digging and see what I can find. I’ll update this thread with anything I find.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,217

    Re: Populate an object's properties from another object's properties

    Unfortunately the code I wrote for cloning via reflection was for a company I worked for, so I can't post it... but thankfully Microsoft have posted one that looks good:
    https://code.msdn.microsoft.com/wind...e5a41f#content
    (click "browse code", then DeepCloneHelper.vb)

    This actually does a "deep" clone (unlike my example above), so if one of the properties is an object then a new instance of that object will be made, and the properties of that object set appropriately.

  9. #9
    Junior Member HeribertoLugo's Avatar
    Join Date
    Nov 2014
    Posts
    29

    Re: Populate an object's properties from another object's properties

    to get your clone is simple, because it sounds like you have a simple object. You wouldnt need a deep copy, as a deep copy uses more resources and its for objects that contain other objects as properties.

    but if i read ur post right, you are saving all the values for the peroperties in the database, then creating objects from them.. it might be just easier to serialize your objects, save to database then deserialize back to object. you could event use that to make multiple copies of the object. newer databases i believe allow searching JSON. If you need to run queries, that wouldnt help. But, sql 2012 im sure allows querying xml, so that is a possible direction.

    Implement IClonable

    here is a sample:
    VB.NET Code:
    1. Imports System.Reflection
    2.  
    3. Public Class Class1
    4.     Implements ICloneable
    5.     Private _name As String
    6.     Private _id As Integer
    7.     Public Property name As String
    8.         Get
    9.             Return Me._name
    10.         End Get
    11.         Set(value As String)
    12.             Me._name = value
    13.         End Set
    14.     End Property
    15.  
    16.     Public Property id As Integer
    17.         Get
    18.             Return Me._id
    19.         End Get
    20.         Set(value As Integer)
    21.             Me._id = value
    22.         End Set
    23.     End Property
    24.  
    25.      Public Function Clone() As Object Implements ICloneable.Clone
    26.         Return Me.MemberwiseClone()
    27.     End Function
    28. End Class

    or

    VB.NET Code:
    1. Public Function Clone() As Object Implements ICloneable.Clone
    2.         Dim copy As Class1 = New Class1()
    3.  
    4.         For Each pInfo1 As PropertyInfo In Me.GetType().GetProperties(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.GetProperty)
    5.             For Each pInfo2 As PropertyInfo In copy.GetType().GetProperties(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.SetProperty)
    6.                 If pInfo1.Name = pInfo2.Name Then
    7.                     pInfo2.SetValue(copy, pInfo1.GetValue(Me))
    8.                 End If
    9.             Next
    10.  
    11.         Next
    12.  
    13.         Return copy
    14.     End Function
    15. End Class

    you can get all fancy and set private members or properties with indexes. this code will just clone simple public properties.

    and you would use it like this:

    VB.NET Code:
    1. Dim originl As Class1 = New Class1()
    2.         originl.id = 5
    3.         originl.name = "Heri"
    4.  
    5.         Dim copy As Class1 = originl.Clone()

    for the sql server thing, its event notifications. I looked into it few years ago when i wanted to implement signalR. Not all sql servers offer this, Azure definitely does not. It didnt seem hard to setup. Im sure you can find the same or better examples than i had. Just do a search. If you see your searching is lacking, try to search using signalR, although signalR is not what you want - that was how i had found it.

    you can start your search here:
    sql server notifs
    Last edited by HeribertoLugo; Jan 31st, 2018 at 12:01 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width