Welcome to

Magenic Technologies Community Blog

Sign in | Join | Help

Aaron's Technology Musings

Who let this guy on the podium?

LINQ to SQL != Typed DataSet Replacement

A couple of questions I have received while giving talks, not to mention a question I see when lurking in, and occasionaly responding in, the Microsoft LINQ forum, is that LINQ to SQL is not feature complete when compared with Typed Data Sets.

My response is, of course, is that LINQ to SQL is not designed to replace Typed DataSets (TDSs).  The reason for existence of TDSs is to do an end-run around having a whole mess of business logic and other things that get between a bound control (such as a grid), and a database table or CRUD stored procedure set.  And at that, so long as your business logic is all in stored procs, or in the presentation layer, is great.  And no, I am not being facetious - as I have indeed used them in the past to bang out quick forms over data apps or prototypes.

That said, in the new world, having an architecture that allows for TDD is, and it pains me to say this, almost mandatory.  That means your objects have to be mockable.  Which of course means that you have to have a reasonable amount of seperation of concerns in order to make that happen.  Given this reality, it is no suprise that with LINQ to SQL, Microsoft has moved away from datasets that have things like caption properties that tell the header in some form down the road what to display.  That kind of mixing of presentation and DAL logic is, what do we say... not "politically correct" anymore. 

In fact, it probably isn't technically a good idea either - especially in a world where maintainability finally is considered important.  Given that frameworks change, DALs change, technical fashion changes, I want all my business logic to reside in PONOs that are framework independent, or if they are going to utilize an inheritance based framework, one that is likely to be around for a good deal of time, such as CSLA.

In that light, one needs to a.) differentiate between LINQ in general - which is a mechanism that allows for set based manipulations of collections, b.) LINQ to SQL, which is an implementation if IQueryable<T> for hitting SQL Server databases, useful in a data access layer, and c.) Typed Data Sets - which is another way to implement a data access layer where you intend to use the data access layer as your business object as well (noting that you could, in theory, use the partial classes feature to write business logic in a TDS - but you would have the same mocking problem as above).

In other words, if you need your data and business layers to be the same thing - use Typed data sets.  If you are implementing TDD, consider LINQ to SQL, or NHibernate, or any other framework designed more for seperation of concerns.

Lastly, yes, I know that people have asked about mockability of LINQ to SQL objects - though if you are using them as DTOs only, I would see that as less of a problem- you could create an interface that the LINQ to SQL oriented DTOs implement that could also be implemented by a mock object - though if you really want to test your queries - particularly the ones that LINQ to SQL generates, you probably need to be doing integration testing, and not unit testing, anyway.

Published Monday, August 27, 2007 4:02 PM by aarone

Filed under: , ,

Comments

# re: LINQ to SQL != Typed DataSet Replacement @ Wednesday, October 03, 2007 9:00 AM

Hey Aaron -

You've been pretty active with LINQ for a while now.  How about addressing some of the following in a future posting?

(How to do the following with LTS):

* Dirty Reads (is there another way other than System.Transactions)

* using App Roles to avoid granting dbdatareader to end-users

* Persistence Ignorance (Domain Driven Design) - this would lead into the "Mockability" discussion

* Caching

Now that MSFT is in the ORM space, folks looking to adopt an ORM tool are going to need to determine if Linq To SQL (or Linq to Entities on top of Entity Framework) is a viable alternative to the more mature ORM tools out there.

Kirk

# re: LINQ to SQL != Typed DataSet Replacement @ Wednesday, October 03, 2007 10:06 AM

Well - now you are going to put me on the spot :)

I am not the biggest LINQ to SQL defender you will find out there.  I think that if you want something like what NHibernate does from Microsoft - with a complete feature set as it relates to lazy load, caching, etc - start at Linq to entities, and go from there.  I have not done a deep dive into LINQ to Entities yet, unfortunatley ... call me skittish, but I suspect that L2E is going to have a high bar to cross given that there are already well developed solutions that do what it does - and also support LINQ just as well (i.e. I am fully confident that Oren has, or has in the works, a nice IQueryable implementation).

I do see a role for Linq to SQL in the DAL as a way of transporting data from one place to another - but almost certainly *not* as a basis for DDD.  That said, I am waiting until the RTM to judge it more fully - the SQL that it generates is getting better and better - and for projects that don't involve a DBA demanding to manage the sql that you use, I see it is as a viable solution.

The big thing that LINQ does isn't LINQ to SQL - it's set operations on collections - and allowing IQueryable implementations - people who want to extend it - to optimize them.  My biggest fear is that if LINQ to SQL turns out to be a bust, that LINQ in general does not suffer.  I think most smart people can tell the difference, but  you never know.

aarone

New Comments to this post are disabled
Powered by Community Server, by Telligent Systems