Using a DataContext static variable

前端 未结 2 769
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 07:26

I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext objects declared as static in every page, and I create them the first time I find

2条回答
  •  误落风尘
    2021-01-06 08:21

    I use to have one DataContext per request, but it depends on the scenarios you're facing. I think the point with L2S was to use it with the unit of work pattern, where you have a context per ... well unit of work. But it doesn't work well in larger applications as it's pretty hard to reattach entities to a new context later.

    Rick Strahl has a real good introduction to the topic here:

    http://www.west-wind.com/weblog/posts/246222.aspx

    One thing I can say I have had problems with in the past, is to have one context to both read and write scenarios. The change tracking done in the datacontext is quite an overhead when you are just reading, which is what most webapps tends to do most of the time. You can make the datacontext readonly and it will speed up things quite a bit - but then you'll need another context for writing.

提交回复
热议问题