More on DataContext in (hopefully) a realistic world

Published 19 March 08 10:20 AM | despos

A comment from Bigyan to my DataContext article on DotNetSlackers yesterday made me think that I probably had to put my thoughts down a little more explicitly. The question that Bigyan poses is expressed in detail in this post of his and revolves around the best way of dealing with DataContext in multi-tier apps. My experience suggests that you use a new instance of the DataContext for each unit-of-work you need. The DataContext is a lightweight object to instantiate/dispose just for this. Can't find the link but somewhere on MSDN I've found a note (documentation) along the following, quite explicit, guidelines:

In general, a DataContext instance is designed to last for one unit of work however your application defines that term.

It is not recommended to maintain a DataContext live for more such as stored in a singleton object. Likewise, you don't have to pass it around, because it is not exactly a light object once in use with data attached: data + tracking info + mapping info. If you find it cumbersome to write new DataContext at each step, you can use a bit of abstraction and perhaps a Repository pattern. One more thought about not passing it around tiers and layers. You don't reuse the data context; you'll reuse the data you attach to it. So it makes sense to cache data somewhere and using distinct data context to do manipulation. On the new data context, you just attach any desired data. Yes, again, using the DataContext should be kind of using an ADO.NET connection: you use always new DbConnection objects but reuse command text and parameters. You never keep a connection open for a long time and don't pass it around tiers.

My two (euro)cents.

Filed under:

Comments

# podi said on March 19, 2008 10:40 AM:

Hi Dino,

What if I have a function which sometimes needs to recurrently call itself and which internally needs to  access a DataContext object?

Until today I was declaring a global DataContext instance, but your post explained me that I should rather not to. You mention also that I shall not pass it as an argument.

As far as I understand, in my recurrent function I need to instanciate DataContext object directly before accessing it and dispose right after.?

The question is, that I'm wondering about the performance, because this function is meant to be called very rarely, but once it is needed, it will be executed thousands times.

Thanks

# Ben said on March 19, 2008 06:16 PM:

Thanks!   This stopped me from storing my DataContext in a singleton.

# bigyanr said on March 20, 2008 06:00 PM:

Thanks for more info Dino. Here's what I have taken from all the feedbacks and research.

www.dotnetlog.com/.../follow-up--best-practice-and-effective-use-of-datacontext.aspx

# Pages tagged "realistic" said on March 29, 2008 07:46 AM:

Pingback from  Pages tagged "realistic"

# Wooley's LINQ Wonderings said on April 2, 2008 09:46 PM:

I had a blast on the Geek Speak today. If you missed it, they will have it available on demand from their

# Patay Khan said on April 22, 2008 08:15 AM:

Isn't one datacontext per http request seems more efficient? just like hibernate guys do? Can you pinpoint what can go wrong with that?

Leave a Comment

(required) 
(required) 
(optional)
(required)