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

March 8. 2009 10:39 PM

Thanks

January 6. 2010 01:09 AM

CNA training

Do you accept guest posts? I would love to write couple articles here.
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

CNA training

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

March 18. 2010 11:20 PM

Mississauga condo for sale

How-do-you-do, just needed you to know I have added your site to my Google bookmarks because of your extraordinary blog layout. But seriously, I think your site has one of the freshest theme I've came across. It really helps make reading your blog a lot easier.

Mississauga condo for sale

March 19. 2010 04:19 AM

Tattoo Ideas

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

Tattoo Ideas

March 21. 2010 09:21 AM

Carlos Makinster

This is a good blog post, I was wondering if I could use this write-up on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away. % BLOGTITLE %

Carlos Makinster

March 23. 2010 09:01 AM

Curtis Arnholt

This is a good post, I was wondering if I could use this piece of writing on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away. % BLOGTITLE %

Curtis Arnholt

March 27. 2010 04:23 PM

Normand Gillians

Im glad I located this blog site, I couldnt discover any knowledge on this subject before. Also operate a site and if you're ever serious in doing some guest writing for me you should feel free to let me know, im always look for people to check out my web page. Please stop by and leave a comment sometime!

Normand Gillians

March 30. 2010 07:56 PM

computer repairs gold coast

I am getting the following error when I try to compile the project:

Error 1 Class, struct, or interface method must have a return type

Its complaining about the DBManagerFactory class. Can someone help?

computer repairs gold coast

March 31. 2010 06:53 AM

NC Payday Loans

Good things happen when you pay attention.

NC Payday Loans

March 31. 2010 07:44 AM

AK Payday Loans

Tomorrow is often the busiest day of the week.

AK Payday Loans

March 31. 2010 07:45 AM

CO Payday Loans

The world leaders in innovation and creativity will also be world leaders in everything else.

CO Payday Loans

March 31. 2010 05:10 PM

MLM Leads

Great Info! FYI - This is a Game changer.

MLM Leads - Phone GOLD Miner PRO has cracked the MLM Leads Code showing you How to have 50 to 300 networkers ask YOU to call them (at Will) about YOUR Business

opportunity.Network Marketing has now been forever changed in favor of the little guy! The FUN is back in Success.

MLM Leads

April 2. 2010 05:48 PM

Mens Kegel exercises

Hi there, just wanted to leave a quick comment about the interface of your blog. It is really easy on the eye while also being catchy. I think I will do something similar for my blog as well. Thanks for the nice blog share.

Mens Kegel exercises

April 3. 2010 08:10 AM

NYC Plastic Surgeon

Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

NYC Plastic Surgeon

April 6. 2010 07:30 PM

hikaye

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

hikaye

April 13. 2010 10:56 PM

22 lcd monitor

I have discovered and learned much from your blog. Your blog is really good.

22 lcd monitor

April 15. 2010 03:05 PM

kapadokya

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

kapadokya

April 15. 2010 07:23 PM

fast loans

The supreme happiness in life is the conviction that we are loved.

fast loans

April 15. 2010 08:45 PM

fast personal loans

If you want to achieve excellence, you can get there today. As of this second, quit doing less-than-excellent work.

fast personal loans

April 20. 2010 02:28 PM

compact dryer

I have learned a lot from your blog. I have been a copywriter and I really loved to see your blog. Well done!

compact dryer

April 21. 2010 07:28 AM

Chandra Hawkinson

Im happy I discovered this blog site, I couldnt discover any knowledge on this topic prior to. Also operate a site and if you wish to ever serious in doing a bit of guest writing for me please feel free to let me know, im always look for people to check out my web site. Please stop by and leave a comment sometime!

Chandra Hawkinson

April 21. 2010 09:24 PM

plaka sorgulama

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

plaka sorgulama

April 22. 2010 01:59 AM

Ellsworth Fonseca

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites web site list and will be checking back soon. Please check out my site as well and let me know what you think.

Ellsworth Fonseca

April 22. 2010 06:35 AM

anime-live

gr8 Thank 4 shar Very intriguing article.

anime-live

April 22. 2010 07:22 AM

Dorcas Vallie

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites site list and will be checking back soon. Please check out my site as well and let me know what you think.

Dorcas Vallie

April 22. 2010 09:29 PM

Napoleon Policicchio

Good blog, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.

Napoleon Policicchio

April 23. 2010 07:39 AM

rezzan kiraz

I just couldnt leave your website before saying that I really enjoyed the quality facts you provide to all your visitors. Would be back generally to check up on new stuff in you article!

rezzan kiraz

April 23. 2010 07:55 PM

fallar fal bakma

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

fallar fal bakma

April 26. 2010 12:55 AM

Socorro Gainey

I'm glad I discovered this webpage, I couldnt find any information on this subject matter before. Also operate a site and in case you are ever interested in doing some guest writing for me you should feel free to let me know, im always look for people to check out my site. Please stop by and leave a comment sometime!

Socorro Gainey

April 26. 2010 08:52 PM

trafik ceza sorgulama

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

trafik ceza sorgulama

April 27. 2010 05:14 PM

Chennai Classifieds

Nice article here, I thought it was very well written. Look at all the comments you have gotten, there are so many out there.

Chennai Classifieds

April 28. 2010 08:47 AM

thy bilet fiyatları

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

thy bilet fiyatları

May 4. 2010 09:37 AM

bağkur borc sorgulama

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.

bağkur borc sorgulama

May 5. 2010 09:25 PM

sgk

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

sgk

May 9. 2010 11:16 PM

Transformadores Eletricos

Nice post. Help me a lot. thanks!

Transformadores Eletricos

May 12. 2010 07:18 AM

Mui Alevedo

This is a very important post, I was looking for this info. Just so you know I located your web site when I was doing research for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.

Mui Alevedo

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading