Friday, November 03, 2006

Web cache gets object references - not a copy

Did you know that if your retrieve an object from the cache, such as a DataView, that the object returned is a reference to the actual object. This means that if you plan to use .RowFilter, then you can end up with concurrency issues if other users are reading from the cache too. The solution is to create a new dataview from a cached table.

Bad:
Dim dvUsers As DataView = CType(_Context.Cache.Get("Users"), DataView)
dvUsers.RowFilter = "User_No=2"

Return dvUsers("Name")

Good:
Dim dtUsers As DataTable = CType(_Context.Cache.Get("Users"), DataTable)
Dim dvUsers As New DataView(dtUsers)
dvUsers.RowFilter = "User_No=2"
Return dvUsers("Name")

No comments: