Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL - where does your DataContext live?

I'm using LINQ to SQL in a data access object library. The library is used in both web (web application/web service) and non-web (windows service) contexts. Initially, I stored the DataContext on the current HttpContext since it permitted me to manage a fairly small unit of work (one web request) and avoided global objects in a web app. Obviously, this doesn't work in a Windows Service.

Rick Strahl has a nice article on managing the DataContext's lifetime: http://www.west-wind.com/weblog/posts/246222.aspx. Unfortunately, I can't make up my mind on the best approach. A global DataContext doesn't work for reasons he mentions, a per-Thread DataContext seems complicated and potentially more trouble than it's worth, and a per-object instance seems fussy - you lose some elegance when you attach the DataContext used to create a DAO to that DAO so it can update or delete later - not to mention, there's something unpleasantly chicken-and-eggish about the relationship.

Does anyone have personal experience that suggests one approach is better than another? Or better yet, does anyone have a fourth or fifth approach I'm not seeing? Where is the best place to store and manage your DataContext?

like image 397
Corbin March Avatar asked Oct 12 '08 23:10

Corbin March


People also ask

What is DataContext in LINQ?

Remarks. The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

Is LINQ to SQL deprecated?

While it is not dead or dying, it is deprecated.

What is Dbml in LINQ to SQL?

The DBML file is mapping that defines your classes based on your database schema. Yes, it defines your (default) connection string, but it doesn't "configure" your database at all. Linq to Sql uses a database-first approach where you have the database and model your classes after the DB schema.

Does Entity Framework use LINQ to SQL?

Entity Framework Core uses Language-Integrated Query (LINQ) to query data from the database. LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries.


3 Answers

The guidelines from the MSDN documentation on the DataContext class are what I would recommend following:

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

Because DataContext is IDisposable, I find it easiest to create and use a DataContext in a using statement within one method, so it can be disposed of properly.

Also note that "any instance members are not guaranteed to be thread safe", so sharing one DataContext between multiple threads would be unwise.

like image 121
Bradley Grainger Avatar answered Oct 12 '22 09:10

Bradley Grainger


Dependency Injection.

We prefer to keep our business layer ignorant of web vs non-web scenario's. Instead, business logic layer objects take a DataContext reference in their constructor which (explicitly) allows sharing the DataContext and (implicitly) allows sharing of the entity objects from query results as they are all from the same data context.

Also DataContexts implement IDisposable, so you really need to manage their lifetime. All our web forms have a base class, and part of that is a datacontext property (lazily created). That way everything on a page can share it, which is most often the case. The context is disposed of manually in the page's OnUnload() event.


  • You shouldn't mix linq entities from from different data contexts, and you typically get into trouble if you use the linq entities if the datacontext has been Dispose()'d of.
like image 6
Robert Paulson Avatar answered Oct 12 '22 11:10

Robert Paulson


I'm using a per-thread context. It is tricky to setup, but it cleans up everything that needs to talk to the db.

like image 1
Wyatt Avatar answered Oct 12 '22 09:10

Wyatt