Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

Making Sense of LINQ-to-SQL



Most Web applications are data-driven and centered on relational databases. However, for years architects have designed applications modeling the domain of the problem through objects. But communication between objects and relational databases has never been smooth and easy. The difficulties that arise when you try to set up this sort of communication are often referred to as "object/relational impedance mismatches" (O/RM). A persistence layer is required to save and load from objects from relational tables. Over the years, a number of design patterns have been defined to provide guidance to architects engaged in this task. Today, developers write this persistence layer entirely on their own or, more often than not, with some help from O/RM tools such as NHibernate or Genome.

While a backend with a domain model and persistence layer is still the most recommended option for complex, large, durable, and enterprise-class applications, it might be too costly for simpler scenarios. And applications that fall under the umbrella of "simpler scenarios" are probably more numerous and used more frequently.

LINQ-to-SQL was created just to address the needs of these applications and provide a powerful, yet simple, set of tools to operate on data stores at a higher conceptual level.

The LINQ-to-SQL framework is specific to SQL Server and, unlike ADO.NET, can't be used to address other relational databases. LINQ-to-SQL is based on the LINQ syntax and is used to query and update an object model built on top of a given database.

LINQ operators such as from, where, and select work on the data context. The data context is a collection of objects created to model the schema of a particular database. In Visual Studio 2008, you find a tool -- the O/R designer -- purposely created to automate the creation of the data model for Linq-to-SQL.

From the programmer's perspective, note that the data context is filled up with data as you access objects programmatically, and any changes to the model (deletions, updates, insertions) are saved back to the database as you call a submit method on the data-context class. Lazy-loading and pre-fetch options give you a way to fine-tune the data loading to optimize the overall behavior of the application.

So what is LINQ-to-SQL and where does it belong? Is it a tool to express the domain model of a system incorporating both logic and persistence? Or is it a sort of object-oriented ADO.NET?

All in all, you can use LINQ-to-SQL for both extremes once you have clear the overall design of your system's backend. In any backend, no matter the complexity, you need logic and persistence. Then, based on the level of complexity of the domain model you decide whether you need a separate persistence layer or perhaps a unique layer of code. LINQ-to-SQL offers an effective domain model (sort of an Active Record) and persistence. In most cases, this is just enough.

In complex, typically enterprise, scenarios LINQ-to-SQL is only an excellent tool to perform physical data access when no O/RM tool is employed. In this case, the objects of the LINQ-to-SQL model are internal to the data access layer and have nothing to do with the domain model in the business layer. In the middle tier, I suggest you use another set of objects to manage entity classes. The middle-tier objects can then use LINQ-to-SQL classes to store data and add methods to expose any requested behavior to the presentation layer. The LINQ-to-SQL objects therefore will be only an object-oriented replacement for T-SQL opaque strings.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.