I think LINQ has several hurdles to leap before we can adopt it seriously. However, LINQ does offer some interesting benefits, and as SQL developers and DBAs, it behooves us to learn about this new technology and its promises.
LINQ's benefits
LINQ's benefits include, most significantly, a standardised way to query not just tables in a relational database but also text files, XML files, and other data sources using identical syntax. A second benefit is the ability to use this standardised method from any .NET-compliant language such as C#, VB.NET, and others.
See LINQ in action
The following snippet illustrates (in C#) a simple LINQ code block, using the Northwind database as its target. To see LINQ at work, we'll begin with a simple C# 3.0 program that uses the standard query operators to process the contents of an array.
using System;
using System.Query;
using System.Collections.Generic;
class app {
static void Main() {
string[] names = { "Alpha", "Brian", "Connie", "David",
"Frank", "George",
"Harry", "Inigo" };
IEnumerable<string> expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
foreach (string item in expr)
Console.WriteLine(item);
}
}
This program delivers this output:
ALPHA
BRIAN
DAVID
FRANK
HARRY
INIGO
This example illustrates the coding style of LINQ and the movement of code to the front end that, in my opinion, should reside in the back end (even though I will admit that the code is clean and relatively transparent).
What front end and back end means to me
I'd like to offer this quick aside to discuss the terms front end and back end, which refer to the classic client-server configuration. The back end is the database itself, which resides on a server. The front end refers to the collection of client applications that speak to the database of interest; these might include apps written in any of the popular programming languages (e.g., Visual Basic, C++, and PHP). When talking about the front end, we must also include applications such as Microsoft Access, Excel, and Word.
In a small organisation with fewer than 100 computers, say, these distinctions may seem academic, but my motto is "Design for success, otherwise you build failure in." That is why I want as much logic as possible to reside in the back end rather than the front end. Suppose I have a procedure or function that creates a recordset, qualifies the selection with one or more parameters, and then orders it by one or more columns. If I place the logic of that procedure in any given front end, then I will have to duplicate it in any new front end that is added next week or next month. Although we like to think that logic is independent of language, in practice, that is not the case.
Development has evolved from classic client-server to various forms of three-tier architecture (DLL-hell, SOAP, etc.). In these architectures, the front end talks to a middle tier which contains considerable logic and is addressable by all the front ends of interest. The middle tier speaks in turn to the database of interest. How you achieve this communication varies with the front end language and the middle-tier implementation, but suffice to say that this middle tier communicates with the database server and that the front end must only concern itself with talking to the middle tier. So I stretch the term back end to cover this architecture as well. My point here is that the front end should do as little as possible. If you use a middle tier, then move all the logic you can to that level, then reconsider your design and move all the logic you can to the back end.
My bias may be clouding my optimism
I must reveal that I hold this as a sacred principle: Whatever the back end can do, the back end should do. Given this bias, you can understand my reluctance to see database logic move into the front end.
Why am I so steadfast in this principle? Because, in my experience, several different front-end applications may hit a single database -- they could be Web sites, Visual Studio apps, Delphi apps, and so on. To the extent that code moves from the back end to the front end, you must duplicate and translate the instructions from one front end to another. This is fine if you decide to stay within Visual Studio .NET, but suppose that you or your employer has other ideas? Or, what if you're interested in porting this logic to other databases?
My perspective is not unanimous. I know several developers who argue that certain applications and situations demand ad-hoc, dynamically constructed queries. I hold that these situations have not been analysed sufficiently, but I am willing to be proven incorrect on this point. So let us examine the other perspective and assume that it is correct: There are N situations in which the only correct approach is to have the front end create a valid SQL statement, protect the code from SQL injection, and then pass the statement to the database engine.
Let us further assume that regardless of the correctness of either position, there are numerous developers who would like to develop their SQL instructions in the front end rather than the back end. It is this set of developers for whom LINQ is most interesting. To the extent that you subscribe to my sacred principle, you may find this movement toward the front end disturbing.
The jury is still out
It's too early to determine whether this new technology will be the next-best-thing in database development. For one thing, it still needs to be tested in the trenches of database and application development. I do think LINQ's multi-data source abstraction is very cool, but, as I've stated, I feel that the movement of code from the back end to the front end is inherently a mistake.
Only time will tell. But until that time when LINQ's advantages are proven (as compared to an equally functional database which contains the logic in the back end, leaving the front end with the trivial job of constructing calls to stored procedures and supplying the parameters they demand), I'm sticking with the "classic" approach: Whatever the back end can do, the back end should do.
Do you need help with .Net? 





1
Fallen - 04/11/05
I wonder why the authour assumes that LINQ only has front end layers? I agree with the argument, but the reason why this ONLY applies to front end layers was not given. Are there no other layers where this could be helpful, or are there existing technologies that are more suited in those layers?
» Report offensive content
2
Christof - 05/11/05
This article is really ridiculous!! There's no reason you couldn't abstract your data layer and business logic layer using webservices, using ADO.NET providers that are database agnostic AND at the same time leverage LINQ to implement all of this. WITHOUT ever even thinking about what the author calls "the front end". No conclusions, no "meat", I wondered if he ever had a serious look at LINQ...
» Report offensive content
3
Mirad - 09/11/05
Architectually, the author is right about where the logic should be, but this has nothing to do with LINQ. The way I see it it would enable development of a single Data Access Layer, agnostic to the underlying database, which is not really possible, with SQL, today, due to subtle, but significant differences different RDBMS have. Moreover, LINQ should enable developers to treat a wide rane of datasources, be them text files, XML, collections or relational databases uniformly.
This would enable a true and complete abstraction of the application logic from database, which along with the current standard of separating what the author calls "front end", i.e., the UI, which I think is what we should all be concerned with - application (business) logic is the heart of the application, while UI and data repositories can be plugin and swapped easily.
» Report offensive content
4
Ann Onymous - 09/11/05
Why not just write your middle layer in LINQ, making that part easier, and continue as usual with various frontends on top?
» Report offensive content
5
Dave - 10/11/05
LINQ doesn't preclude you from designing a proper 3-tier architecture. From my persepective, it would be a great way to do data access in the middle-tier. There's no reason it has to be relegated to the front end. Just like ADO.NET, or any other data access technology, it can be deployed wherever you want it in your application.
» Report offensive content
6
Chris Garty - 11/11/05
The article is jumbled.
1. You say that middle tier is included in the back-end.
2. You say that putting logic in the back-end is good
3. You say that LINQ stuff can be written in the middle tier (back-end)
And then you conclude by saying LINQ is bad because it is all front-end?!?
Why didn't you say LINQ is good as long as you be careful to put most of it in the back-end?
» Report offensive content
7
Ryan Haney - 20/03/07
Actually...LINQ is great for all tiers...
From the middle tier - accessing data in a uniform fashion.
From the front-end - accessing data retrieved from the middle tier in a uniform fashion.
Assuming that the middle tier is an XML web service, XLINQ and/or LINQ can be used to efficiently examine the results. I agree with the placement of the logic, but also believe that at some point, your front-end should be performing the validations it can before sending the request to the middle tier.
» Report offensive content
8
Ben - 28/10/07
This article needs to be taken down. I don't think hes really dug into LINQ besides the example shown. This is a perfect example of the kind of information people get that leads them to misunderstand and misjudge technologies for years. He also has a strange understanding of tiers.
» Report offensive content
9
Jeff Tucker - 19/12/07
In the CodeProject there is an article on 3-tier architecture "Enterprise Application Architecture with LINQ to SQL" that describes a good approach that can use only stored procs, but still involve generation of the data transfer objects (your table entities in Linq to SQL). The secret is to do all the data querying within the datalayer, and only pass IEnumerable<tableEntity> out of the datalayer. Scott Guthrie describes in blog 6 (I think) how to customize a stored proc to return and entity instead of one of those classes idiosyncratic to a stored proc. For the data transfer objects replace the sqlmetal generated xml and code with a dbml. The datacontext class can be snipped out and moved to the DataLayer to prevent developers from partying with dynamic IQueryable objects. Turn off lazy loading in the DataContext object.
» Report offensive content
10
marcwolf - 12/02/08
I remember back in the earlier days of MS there was a project called Cirrus.
It was to provide a universal way to connect an Oracle DB, a MS-SQL DB, a MySQL DB, and if necessary a spreasheets as well.
We know that application today as MS-ACCESS.
In many ways LINQ is offering the same - a simple way to create a flat access format of various types of data - whether the data is from your mailbox, your Oracle server, or your oversea's MS-SQL server. In the end all like to be approx the same for reading and manipulating.
Really loooking forward to playing with LINQ
Take Care
Da Wuff!!!
» Report offensive content
11
mememe - 20/01/09
MS might drop LINQ to SQL, and replace it with LINQ to entities... and you can use storedProcs as linq objects too, so the processing can still reside wherever the H you want it too... its the Integrated Queries that make accessing that data easy... DUUUU
» Report offensive content
12
jamesclose - 23/09/09
I am currently considering LINQ as a reason to go to VS 2008.
But I am inclined to agree with the author here, it feels like going back to embedding query logic in the "middle layer". I am concerned about black boxing the database connection usage and also all the performance of all these SQL like queries in .NET. Isn't that what the database is optimized for? It seems a bit like doing joins on datasets in .NET on the fly and no one would seriously consider doing that, would they?
I understand you can still use stored procs with LINQ, but that seems even weirder, why disperse logic between stored procs and LINQ? It would seem to make sense to do all you can with LINQ alone or SQL alone, not both.
» Report offensive content