Creating a Data Access Layer with Linq to SQL, part 1

There is no doubt that Linq to SQL will have an enormous impact on the way we write data access layers. I wouldn't be surprised to find out that the impact is so profound, that we might even have to reconsider the very nature of a data access layer. In fact, what is a data access layer (DAL) anyway?

Let's start by trying to create a (working) definition of a DAL. Wikipedia is usually a good place to start, but you'll find that the Wikipedia article on DAL's doesn't exactly contain all the answers. So let' give it a try ourselves.

A DAL is a layer. That means it is part of a layered architecture. Other layers use the DAL to do data access. Indeed, the DAL is the layer accessing the data (and in the context of Linq to SQL, that's relational data), and no other layers access the data directly.

That's a good start, but what is layer? Is that a special kind of component? Not in my mind it isn't. To me, a layer can contain multiple components, and that applies to a DAL as well. Let's say I have a simple banking system. It contains functionality on clients, their accounts, and the operations (such as money transfers) they do on those accounts. That might result in a vertical partitioning of the application in three modules, "Clients", "Accounts" and "Operations". Each of those would be layered, and you'd find components at the intersections of the vertical modules and the horizontal layers. So you'd have "Clients DAL", "Accounts DAL" and "Operations DAL" components. Obviously, these components are related, they have dependencies between them. The Operations DAL depends upon the Accounts DAL (and possibly the Clients DAL as well), and the accounts DAL depends on the Clients DAL.

In .NET, components like these correspond to assemblies. So our DAL would consist of several assemblies, with (non-circular) references (dependencies) to each other. Which part of the functionality do we put where?

The Clients DAL doesn't know about accounts, that's the responsibility of the Accounts DAL. That one knows about accounts, and about clients as well. After all, accounts are owned by clients. So that means the function to retrieve the list of accounts belonging to a given client sits in the Accounts DAL, not in the Clients DAL. Since this function has a client as a parameter (or at least a client id), the Accounts DAL may indeed need a reference to the Clients DAL. Each object returned by this function has a reference to the client owning the account, or at least the id of that client.

But wait, what's the impact of Linq to SQL on what I said so far? If I have a database with Clients and Accounts tables (amongst many others) with a foreign key between them, the typical Client and Account entity classes will have a relationship between them as well. The Account class will have a delay-loaded Client property, and the Client class will have an Accounts property. That's a mutual dependency, so these two classes need to sit in the same assembly. But where does that lead us to? Typically, all tables in a database are somehow related to each other, i.e. there are no disconnected islands of tables with relations between them, but no relations to other islands in the database. But that leads us to just one DAL per database! Is that what we want?

Well, SQLMetal, the Linq to SQL tool that generates an entity model based on a database schema, definitely pushes us in that direction. Typically, it generates one source code file containing one DataContext and all the entities in your data model. But that's just the entities though, that code doesn't do any data access! To actually access the data, you need to write queries! And those queries are the responsibility of our DAL components.

In our example above, we had three DAL components, and all three of them would access the same DataContext. That implies that the DataContext should exist in an assembly of its own, an assembly underlying all DAL assemblies. But that's an additional layer, isn't it?

Well maybe it is. Maybe we need to split our traditional Data Access Layer into two distinct sublayers. For lack of better terms, I'll call them the "Entity Layer" and the "Entity Access Layer".

The Entity Layer has just one assembly in it, so we might just as well refer to that assembly as the Entity Layer as well. The entire assembly is compiled from just one code file (and some housekeeping stuff perhaps, like an AssemblyInfo.cs file), generated by SQLMetal.

The Entity Access Layer (EAL) has several assemblies (three in our example), all using the Entity Layer. The EAL assemblies contain the actual queries.

Next time, we'll look at the interface between the EAL assemblies and the business layer: what parameters are used, what results are returned? Do we expose the Entity Layer types? Do we expose query expressions or query results only?

That's enough food for thought right now, and comments are more than welcome.


Comments (28) -

March 8. 2009 10:39 PM

Thanks

January 10. 2010 01:26 AM

alen air purifier information

I have been surfing online more than three hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the internet will be much more useful than ever before.

alen air purifier information

January 13. 2010 04:16 AM

Valentine 2010

Hey - nice blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

Valentine 2010

January 15. 2010 08:13 AM

ock9 premium

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

ock9 premium

January 21. 2010 11:49 PM

How To Get Rid Of Eczema

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

How To Get Rid Of Eczema

January 24. 2010 01:38 AM

online rulett

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

online rulett

January 25. 2010 08:37 AM

club penguin

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

club penguin

January 31. 2010 01:21 PM

cabinet hardware

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual...

cabinet hardware

February 2. 2010 06:25 AM

ekstern backup

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ekstern backup

February 8. 2010 05:57 PM

arthritis joint pain

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

arthritis joint pain

February 9. 2010 11:51 PM

contractor accountants

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

contractor accountants

February 13. 2010 11:09 AM

no loss robot

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??

no loss robot

February 13. 2010 07:44 PM

Grapevine Garage Door Repair

Hi. this is kind of an "unconventional" question , but have other visitors asked you how get the menu bar to look like you've got it? I also have a blog and am really looking to alter around the theme, however am scared to death to mess with it for fear of the search engines punishing me. I am very new to all of this ...so i am just not positive exactly how to try to to it all yet. I'll just keep working on it one day at a time.

Grapevine Garage Door Repair

February 16. 2010 01:38 PM

custom term papers

I will use this for my report, Linq to SQl will provide runtime infrastructure for relational datas without losing any queries. Thank you for sharing this!

custom term papers

February 18. 2010 02:10 PM

seo website promotion

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

seo website promotion

February 23. 2010 11:20 PM

save marriage

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.

save marriage

February 25. 2010 08:41 PM

electronic cigarette

The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

electronic cigarette

February 28. 2010 01:27 AM

Houston Computer Repair

I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.

Houston Computer Repair

February 28. 2010 04:43 AM

swoopo software

I have recently started using the blogengine.net and I having some problems here? in your blog you stated that we need to enable write permissions on the App_Data folder...unfortunately I don't understand how to enable it.

swoopo software

March 2. 2010 07:00 AM

Linkbuilding Tool

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

Linkbuilding Tool

March 3. 2010 10:12 PM

Middlebrook Heights NJ Handyman Services

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

Middlebrook Heights NJ Handyman Services

March 9. 2010 05:21 AM

Jerrie Deroche

Really good site, where did you come up with the knowledge in this piece? I'm happy I found it though, ill be checking back soon to see what other articles you have.

Jerrie Deroche

March 10. 2010 06:24 AM

oregon duii

I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

oregon duii

March 15. 2010 09:46 PM

Minnie Brogley

This is a fantastic post, but I was wondering how do I suscribe to the RSS feed?

Minnie Brogley

March 16. 2010 09:51 AM

emekli sandıgı

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

emekli sandıgı

March 16. 2010 04:00 PM

Lansinoh Double Electric Breast Pump BPA Free

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

Lansinoh Double Electric Breast Pump BPA Free

March 16. 2010 04:31 PM

arac sorgulama

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

arac sorgulama

March 16. 2010 05:48 PM

whirlpool refrigerator water filter

Hi. this is kind of an "unconventional" question , but have other visitors asked you how get the menu bar to look like you've got it? I also have a blog and am really looking to alter around the theme, however am scared to death to mess with it for fear of the search engines punishing me. I am very new to all of this ...so i am just not positive exactly how to try to to it all yet. I'll just keep working on it one day at a time.

whirlpool refrigerator water filter

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Download the U2U brochure

Download Brochure

Receive the U2U Newsletter. Submit your email address:
 
 


 


Search

rss  RSS

Recent posts

None

Archive

Blogroll