.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:

  1. objects start accumulate in it
  2. you can't use in multiple threads or multiple transactions @ same time anyway
  3. 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:

  1. have 1 datacontext multiple http-requests/wcf-calls
  2. have datacontext unbounded amount of time (i.e. duration desktop app nunning)
  3. have datacontext unbounded number of tracked entities
  4. use across threads

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -