views:

131

answers:

2

I'm creating a webserivce that will be using Linq-To-Sql to perform select only queries. In this respect would having the DataContext as a static field / property by acceptable since the operations will never be ones that modify the database or track object changes?

If not, what alternative approaches would be suitable?

+2  A: 

The DataContext is not threadsafe so storing it in a static would not be a good idea.

DataContext is not really expected to live very long, just instance it during Request processing.

AnthonyWJones
+2  A: 

I would recreate the datacontext on every method call, wrapped in a using block. This would help make sure that the objects that get created as a result of the operation get disposed and reduce your memory footprint. Since the object are serialized to be sent back, there shouldn't be any need to keep the datacontext around longer than what the call takes. The data context itself is not a particularly heavy object and recreated it as needed shouldn't be avoided. This is really how it was intended to be used -- as a unit of work object.

tvanfosson