I am using collections in my project and have come up against some
issues. My project is a web based thin client application.
The main issue I have is with collections, I have all the users
settings/permissions etc held in a collection which is in a module.
This enables the user to access their settings from page to page
without quering a database every time the page loads.
Administrators of the system can change users settings while the user
is logged in. But, after reading up on collections that have multiple
readers/writers I discovered that I need to lock the collection before
the administrator can apply the changes, as there could be more than
one admin changing a setting.
I have read through MS documentation and have put together the code
below, which seems to be working, but I have no way of really knowing,
as the only test I can do is try editing the sam account at the same
time from various computers, which does seem to work.
Please can someone verify I have done this correctly?
Here is the UserGroupPermissionCollection:
Public Class UserGroupPermissionCollection
Inherits System.Collections.CollectionBase
Private _userGroupIdHashtable As New Hashtable
Public ReadOnly Property UserGroupIdHashtable() As Hashtable
Get
Return _userGroupIdHashtable
End Get
End Property
Public Sub Add(ByVal newUserGroupFunctions As
UserGroupFunctionCollection)
Me.List.Add(newUserGroupFunctions)
UserGroupIdHashtable.Add(newUserGroupFunctions.Id,
newUserGroupFunctions)
End Sub
Public Sub Remove(ByVal removeUserGroupFunctions As
UserGroupFunctionCollection)
Me.List.Remove(removeUserGroupFunctions)
UserGroupIdHashtable.Remove(removeUserGroupFunctio ns.Id)
End Sub
Default Public ReadOnly Property Item(ByVal userGroupId As Int32)
As UserGroupFunctionCollection
Get
Return UserGroupIdHashtable.Item(userGroupId)
End Get
End Property
Public Shadows Sub Clear()
MyBase.Clear()
UserGroupIdHashtable.Clear()
End Sub
End Class
This is the declaration and property in the module:
Private _userGroupPermissions As New UserGroupPermissionCollection
Public ReadOnly Property UserGroupPermissions() As
UserGroupPermissionCollection
Get
Return _userGroupPermissions
End Get
End Property
This is my code in the aspx page:
Dim _newUserGroupFunctions As New UserGroupFunctionCollection
Dim userGroupFunction As UserGroupFunction
....Do some stuff and set values in the userGroupFunction Structure and
Add them to the _newUserGroupFunctions Collection
Dim _userGroupPermissions As System.Collections.ICollection =
UserGroupPermissions
SyncLock _userGroupPermissions.SyncRoot
'Get current function collection to be removed
Dim _removeUserGroupFunctions As UserGroupFunctionCollection = _
UserGroupPermissions(iUserGroupId)
If Not _removeUserGroupFunctions Is Nothing Then
'Remove collection
UserGroupPermissions.Remove(_removeUserGroupFuncti ons)
End If
UserGroupPermissions.Add(_newUserGroupFunctions) 'Add new
collection
End SyncLock
Is this right or have I totaly missunderstood the whole concepted?