|
-
Jan 29th, 2018, 08:10 AM
#1
Thread Starter
Lively Member
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.
-
Jan 29th, 2018, 09:25 AM
#2
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.
-
Jan 29th, 2018, 09:26 AM
#3
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.
-
Jan 29th, 2018, 06:27 PM
#4
Thread Starter
Lively Member
Re: Populate an object's properties from another object's properties
 Originally Posted by si_the_geek
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.
-
Jan 29th, 2018, 06:36 PM
#5
Thread Starter
Lively Member
Re: Populate an object's properties from another object's properties
 Originally Posted by jmcilhinney
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?
-
Jan 29th, 2018, 08:51 PM
#6
Re: Populate an object's properties from another object's properties
 Originally Posted by ianbhenderson73
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.
-
Jan 30th, 2018, 01:57 AM
#7
Thread Starter
Lively Member
Re: Populate an object's properties from another object's properties
 Originally Posted by jmcilhinney
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.
-
Jan 30th, 2018, 06:05 PM
#8
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.
-
Jan 30th, 2018, 11:23 PM
#9
Junior Member
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:
Imports System.Reflection Public Class Class1 Implements ICloneable Private _name As String Private _id As Integer Public Property name As String Get Return Me._name End Get Set(value As String) Me._name = value End Set End Property Public Property id As Integer Get Return Me._id End Get Set(value As Integer) Me._id = value End Set End Property Public Function Clone() As Object Implements ICloneable.Clone Return Me.MemberwiseClone() End Function End Class
or
VB.NET Code:
Public Function Clone() As Object Implements ICloneable.Clone Dim copy As Class1 = New Class1() For Each pInfo1 As PropertyInfo In Me.GetType().GetProperties(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.GetProperty) For Each pInfo2 As PropertyInfo In copy.GetType().GetProperties(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.SetProperty) If pInfo1.Name = pInfo2.Name Then pInfo2.SetValue(copy, pInfo1.GetValue(Me)) End If Next Next Return copy End Function 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:
Dim originl As Class1 = New Class1() originl.id = 5 originl.name = "Heri" 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|