Stephen,
> I must start learning more about patterns
Yes patterns are good to know! The three books I would recommend include:
1. "Design Patterns - Elements of Reusable Object-Oriented Software" by GOF
(gang of four = Erich Gamma, Richard Helm, Ralph Johnson, & John Vlissides)
2. "Visual Basic Design Patterns - VB 6.0 an VB.NET" by James W. Cooper
3. "Patterns Of Enterprise Application Architecture" by Martin Fowler
All three from Addison Wesley.
Is there a question in there? :-)
You describe a Person Collection that sounds like the Repository Pattern.
http://www.martinfowler.com/eaaCatalog/repository.html
Is this your question?
> 'Loop through each person and find the matching ID
> ..or maybe use HashTable to retrieve Person by ID?
Seeing as Person has an ID, I would use a HashTable, I would derive from
DictionaryBase actually, as the retrieval by ID (key) is quicker. Also
consider storing a System.WeakReference to Person, instead of Person
directly. Especially if the Person object is fairly large. Unless you
already have a method of cleaning up unused Person objects in the
collection.
The GC knows about WeakReferences, when a Garbage Collection occurs the
objects referenced by WeakReferences are discarded if there are no other
references to that object. If your program currently is using the Person its
safe, if the Person only exists in the Collection its toast.
Then you need to modify the 'FindByID' that if it finds a WeakReference with
Target property Nothing, then you need to load the object again.
Something like:
' Assumes collection inherits from DictionaryBase
> Friend Function FindByID(ByVal id as Integer) as Person
Dim wr As WeakReference
Dim person As Person
If InnerHashtable.Contains(id) Then
wr = DirectCast(InnerHashtable(id), WeakReference)
person = DirectCast(wr.Target, Person)
If person Is Nothing Then
person = CreatePerson(id)
wr.Target = person
End If
Else
person = CreatePerson(id)
wr = New WeakReference(person)
InnerHashTable.Add(person.ID, person)
End If
Return person
> End Function
' A factory pattern
Private Function CreatePerson(Byval id as integer) as Person
Dim proxy as sDocument = <call data tier>
return new Person(id, proxy)
End Function
The problem now becomes your collection can fill up with unused
WeakReferences. I would probably have a timer event in the collection that
looks for weak references and cleans them out. Also make sure you coordinate
the cleanup process correctly with your find process, you don't want an
entry disappearing while you are recreating it...
Hope this helps
Jay
> Thanks Jay,
> I must start learning more about patterns
[quoted text clipped - 33 lines]
>
> Stephen