Reputation: 428
I know that the accepted practice is to use DataContext in the Unit of Work pattern but I came across this statement from MSDN:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
As I read it, if a DataContext is declared as static it is thread safe.
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx
Upvotes: 2
Views: 1829
Reputation: 48230
It is the opposite.
Instance members are NOT thread safe. But you are going to access instance members from the same shared instance.
But even if the object would be thread safe, think of consequences of a growing 1st level cache for a long-living data context. Few threads and few dozens/hundeds/thousands of concurrent calls and your instance could run out of memory. Instead, you should dispose the object after it does its job.
Upvotes: 1
Reputation: 40139
No, that is not correct.
What that is saying is that static members of DataContext
itself are thread safe - not static instances of it.
Note that this is pretty much the standard for most classes in .NET. (with allowance for disagreement on what 'most' means). I am not immediately aware of any static members of DataContext
.
Upvotes: 6