开发者

What is better: reusing System.Data.Linq.DataContext context or disposing of it ASAP?

开发者 https://www.devze.com 2023-03-28 00:00 出处:网络
If I am using a SqlConnection, apparently the best practice is to dispose of it 开发者_C百科ASAP, and let the connection pooling handle the details. Should I follow the same pattern when I am using Sy

If I am using a SqlConnection, apparently the best practice is to dispose of it 开发者_C百科ASAP, and let the connection pooling handle the details. Should I follow the same pattern when I am using System.Data.Linq.DataContext?

Should I create my context once and pass it along to my methods, or should I get the connection string from my config file and create the contexts multiple times and save on passing the parameters?

Edit: A useful link about identity maps: Architecting LINQ To SQL Applications, part 7


You should keep a data context around only as long as necessary to perform an operation.

The reason for this is that it uses something called an Identity Map so that every time you select say customer 1 you get the same object back. This means it is holding lots of references and will consume more and more memory over time and these results will become increasingly stale.

In the case of a web app it is common to create one per request and the DataContext class is optimised for quick creation.

0

精彩评论

暂无评论...
验证码 换一张
取 消