I need to assign a guid to objects for managing state at app startup & shutdown It looks like i can store the lookup values in a dictionary using
dictionary<int,Guid>.Add(instance.GetHashCode(), myGUID());
are there any potential issues to be aware of here ?
NOTE
This does NOT need to persist between execution runs, only the guid like so
- create the object
- gethashcode(), associate with new or old guid
before app terminate, gethashcode() and lookup guid to update() or insert() into persistence engine USING GUID
only assumption is that the gethashcode() remains consistent while the process is running
also gethashcode() is called on the same object type (derived from window)
Update 2 - here is the bigger picture
- create a state machine to store info about WPF user controls (later ref as UC) between runs
- the types of user controls can change over time (added / removed)
- in the very 1st run, there is no prior state, the user interacts with a subset of UC and modifies their state, which needs to recreated when the app restarts
- this state snapshot is taken when the app has a normal shutdown
- also there can be multiple instances of a UC type
- at shutdown, each instance is assigned a guid and saved along with the type info and the state info
- all these guids are also stored in a collection
- at restart, for each guid, create object, store ref/guid, restore state per instance so the app looks exactly as before
- the user may add or remove UC instances/types and otherwise interact with the system
- at shutdown, the state is saved again
- choices at this time are to remove / delete all prior state and insert new state info to the persistence layer (sql db)
- with observation/analysis over time, it turns out that a lot of instances remain consistent/static and do not change - so their state need not be deleted/inserted again as the state info is now quite large and stored over a non local db
- so only the change delta is persisted
- to compute the delta, need to track reference lifetimes
- currently stored as
List<WeakReference>
at startup - on shutdown, iterate through this list and actual UC present on screen, add / update / delete keys accordingly
- send delta over to persistence
Hope the above makes it clear.
So now the question is - why not just store the HashCode (of usercontrol only)
instead of WeakReference
and eliminate the test for null reference while
iterating thru the list
update 3 - thanks all, going to use weakreference finally