Linq to SQL Stumper

Software

See if you can spot the contradiction in the following two FAQs:

Q: Should I create a new DataContext in every business logic method?
A: The DataContext conforms to the Unit of Work design pattern. Unless you are moving data between physical tiers between each operation you should keep your DataContext alive for the duration of work.
Q: Should I keep my DataContext in a static/global/shared variable?
A: The DataContext is not thread safe and is not meant to be shared. A DataContext is meant to be used for a single unit or work or at most for multiple consecutive units of work.

Hint: I’ve highlighted it for you.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • Ma.gnolia
  • Reddit
  • TwitThis
1 Comment

1 Comment

  1. Luke Daffron  •  May 15, 2009 @1:14 pm

    I think the bold text should read “you should keep your DataContext alive for exactly the duration of a unit work, and no more.”

    You might take a look at my recent post for a handy way to deal with the problem.

    http://lanitdev.wordpress.com/2009/05/15/linq-to-sql-datacontext-initializer/

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">