tags:

views:

15

answers:

1

With linq2sql, is it possible to have a 3 tierd app or so I have to pass the dbcontext around since its an expensive call?

+2  A: 

A common thing to do is to create a Repository class which implements IDisposable, and have the context inside the repository. When it gets created you instantiate your context, and then dispose it when the repo gets disposed.

That way you can use it throughout the life of your repository.

Joseph
I would go one step further and say that the lifetime of the datacontext should tied to a unit of work, not the repository it's in. Instantiate and dispose a new datacontext for every unit of work.
Eric King
@eking absolutely. I would recommend using your repository in a unit of work pattern, for sure.
Joseph
@Joseph, we're on the same page. :-) +1
Eric King
is a unit of work a request?
mrblah