.net - DataContext Scope Best-Practice -
when using linq-to-sql, visual studio creates datacontext class you. i'm wondering when instantiate context, in 1 of projects used 1 datacontext instanse 300 crud commands (whole project) , worked little problem managed solve later, , in recent project have created instance of per crud command, here's question, best way use datacontext? 1 per command or 1 per project? or maybe 1 per transaction (that thinks idea too)? , there difference in speed , performance each way?
when keep open short waste performance because instantiating datacontext not free. cheap measurable. shows in profiles.
when keep open long problems too:
- objects start accumulate in it
- you can't use in multiple threads or multiple transactions @ same time anyway
- if 1 of submitchanges fails logical reason, context dead because same error appear forever, no matter how retry
here rules should never without deep understanding:
- have 1 datacontext multiple http-requests/wcf-calls
- have datacontext unbounded amount of time (i.e. duration desktop app nunning)
- have datacontext unbounded number of tracked entities
- use across threads
Comments
Post a Comment