Are you stumped by the DAL?
So what is a Data Access Layer (DAL)? Have you just started coding and can’t get your head around it? Or have you been coding a while and heard the term but not investigated yet? This article aims to simply explain what it is, what it does and why you would use it.
What is the DAL?
The Data-Access Layer (DAL) is a component of a software architecture that provides simplified access to stored data and is responsible for managing the data storage and retrieval of an application.
There are multiple layers involved when it comes to the passing and retrieval of data. These include the presentation layer, the business logic layer, the Data Access Object and the storage layer.
Presentation Layer : It holds the part that the user can interact with, e.g. WebApi.
Business Logic Layer: The business logic layer holds all the logics related to the business requirements. It has all the objects and functionalities that handle the business logic of the application and is where the main implementation of the application takes place.
Data-Access Object (DAO): They are entities that are collectively known as the Data Access Layer (DAL). A DAL might be a single class, or it might be composed of multiple Data Access Objects (DAOs).
Storage Layer: The storage layer consists of the actual database where data is stored, upon which CRUD operations can be performed.
What does it do?
There are many purposes and functions of the DAL. I have outlined some below.
It abstracts your database from the rest of your project: It means that there is no SQL in your code except in the DAL. Only the DAL should know the structure of your database
Connecting to the data storage system and managing the connection.
Generating and executing SQL queries or other data access commands to retrieve and store data.
Mapping the data from the data storage system to the application’s data objects and vice versa.
Handling errors and exceptions related to data access.
Provides a centralised location from where calls are made onto the database(s).
Why should you use it?
There are lots of different benefits to using the DAL. It is worth noting that using a DAL isn't necessarily the best option for all applications. For example...
Some benefits include:
It can help isolate your data access from presentation and business logic
It can improve security of your system. For example if your database and DAL were held on inaccessible server(s), whilst your business logic is on a public facing server. If the public facing server was compromised your database and DAL wouldn’t be.
It can protect the rest of your app from database changes
It can make it easier to extend and support your app as you already know where the code is that interacts with the database
A DAL can support multiple databases, so the application is able to make use of any database as per its requirement.
In summary, the DAL is the layer that facilitates interaction between an application and the data storage. It can help isolate your data layer from the rest of your application, make it easier to extend or support your app and, in some instances, add additional security to your application. Hopefully this brief introduction about the DAL has left you better informed!