views:

546

answers:

8

I started a project a long time ago and created a Data Access Layer project in my solution but have never developed anything in it. What is the purpose of a data access layer? Are there any good sources that I could learn more about the Data Access Layer?

+1  A: 

I recommend you read up here: http://msdn.microsoft.com/en-us/practices/default.aspx Using a DAL will help you isolate your data access from your presentation and business logic. I use it a lot so that I can easily swap out (through reflection and dynamically loading assemblies) data providers.

Read up, lots of good info there.

Also, look into the Data Access Block if you are planning on using .NET. It can be a big help.

Geoffrey Chetwood
+3  A: 

A data access layer follows the idea of "separation of concerns" whereby all of the logic required for your business logic to interact with your data layer (database) is isolated to a single set of classes (layer). This allows you to more easily change the backend physical data storage technology (move from XML files to a database, or from SQL Server to Oracle or MySQL, for example) without having a large impact (and if done right having zero impact) to your business logic.

There are a lot of tools that will help you build your data layer. If you search for the phrase "object relational mapper" or "ORM" you should find some more detailed information.

Scott Dorman
> This allows you to more easily change the backend physical data storage technology (move from XML files to a database, or from SQL Server to Oracle or MySQL, for example)Does that ever actually happen as an afterthought? I doubt it.
Jonathan Allen
@Grauenwolf: While changing databases is not that often, separating stuff is still "The Good Way Of Doing", what is more common in practice, you might and up in changeing the way you access your present data, or tweak some functionalities, and have it all in one place.
Slartibartfast
+1  A: 

The DAL should abstract your database from the rest of your project -- basically, there should be no SQL in any code other than the DAL, and only the DAL should know the structure of the database.

The purpose is mainly to insulate the rest of your app from database changes, and to make it easier to extend and support your app because you will always know where to go to modify database-interaction code.

Guy Starbuck
What if I put my data access in my BAL?
Bryan Roth
+5  A: 

In two words: Loose Coupling

To keep the code you use to pull data from your data store (database, flat files, web services, whatever) separate from business logic and presentation code. This way, if you have to change data stores, you don't end up rewriting the whole thing.

These days, various ORM frameworks are kind of blending the DAL with other layers. This typically makes development easier, but changing data stores can be painful. To be fair, changing data stores like that is pretty uncommon.

swilliams
> This way, if you have to change data stores, you don't end up rewriting the whole thing.Are you every really going to change data stores? Unless you plan on it from the beginning, I doubt it.
Jonathan Allen
Well, in some projects I do all the time. In my dev environment, I'm using SQLite, and in the production it's MySQL. The framework I'm using makes this easy though (Pylons).
swilliams
+1  A: 

The purpose is to abstract out the database access details that other parts of your application need not be concerned about.

Esteban Araya
+1  A: 

A data access layer is used to abstract away the storage and retrieval of data from its representation. You can read more about this kind of abstraction in 1994's Design Patterns

+1  A: 

The purpose is to abstract the data storage retrieval mechanism from data usage and manipulation.

Benefits:

  • Underlying storage can change (switch from Oracle to MSSQL for example), and you need a way to localize those changes
  • Schema changes - see above
  • You want a way to run disconnected from your db (demo mode): Add file serialization/deserialization to the DAL
theschmitzer
+3  A: 

Data access layers make a lot of sense when many different parts of your application need to access data the same way.

It also makes sense when you need access the same data in many different ways. For example, how word processors can read many different file types and silently convert them into the application's internal format.

Keep in mind that a DAL can also be very counter productive. If you are building a system where data access performance is critical, separating it from the business logic can make some vital optimizations impossible.

Jonathan Allen
What I did was implement the data access in my BAL.
Bryan Roth
Basic Assembly Language?!
Ian Nelson
LMAO, I meant BLL (Business Logic Layer).
Bryan Roth