What is the difference between ORM and DAL?
Asked Answered
R

3

10

I have read up on both, but it has just confused me more. I have tried to find the differences (and similarities), but am unable to convince myself. Both of them are an intermediate layer between the business logic and the database. Is there a difference or are they the same?

Rusert answered 26/3, 2018 at 20:38 Comment(0)
A
16

ORM (Object/Relational Mapper):

  • It is a library/tool that executes the input SQL query (some ORMs also generate the query for you) and converts (maps) output DataReader (or equivalent in other languages) to your strongly typed objects. This is basic feature of any ORM.
  • That said, it works as layer between your data storage and your application.
  • Core role of ORM is mapping; it always (mostly?) return strongly typed objects. In rare cases, it may return values in basic data types provided by language like int, string etc.
  • This is application agnostic; except that you have to configure this separately per application/data store.
  • This only deals with RDBMS.
  • Some advanced ORMs (full-ORM) can be used as DAL; this is mostly a design decision.
  • Being application agnostic, this cannot implement your specific persistence logic.

DAL (Data Access Layer):

  • It is a layer that handles all your data needs. But this is different from ORM.
  • Actually, this may use ORM internally for any RDBMS communication. Although DAL can be designed without using any ORM also.
  • DAL may return strongly typed objects but not always necessary.
  • DAL may communicate with any form of data store including Web API, XML, RDBMS.
  • Mapping may or may not be the part of DAL depending on how it is being designed.
  • This is application specific.
  • There are different patterns available to implement DAL as Data Access Object or Repository.
  • Being designed for specific application, this may include your specific persistence logic like data encryption.
Aspergillum answered 27/3, 2018 at 10:7 Comment(0)
D
5

ORM is a general programming approach centered around dealing with data in systems in a way that presents them (and lets you work with them) as objects in your programming language of choice. Even if the data comes from a source that has nothing to do with your chosen programming language. The abstract concept of interacting with data through an object "veneer" is ORM.

DAL on the other hand is simply the name for the entire collection of things that a programming language offers that makes working with stored data easier. It's effectively a convenient term for talking about "all the APIs for dealing with stored data".

And to tie it together: "The Data Access Layer for your chosen programming language may use Object-Relational mapping."

Dihedral answered 26/3, 2018 at 21:37 Comment(2)
So basically DAO is also just part of this "collection of APIs" namely the DAL?Rusert
In the sense that if you use an object-relational mapping approach, that by definition means you have a data access object somewhere as part of the data access layer.Beaulahbeaulieu
C
1

In Java, the most common scenario when one refers to Object Relational Mapping is the act of mapping tables into Java objects. One very popular Object Relational Mapper is Hibernate. Let's say you have a table row of a Car. Surely it has many columns such as Year, Make, Model, etc. An ORM would map this table row into a Car.java class with data members that correspond to the columns of the row.

The mapping occurs inside the Data Access Layer, which consists of class(es) that perform the conversion. Additionally, you may have service classes that provide certain Java objects dependent on certain criteria (queries). One example would be let's say you have an application with Users. You would likely have a UserService.java class with a getAllUsersSortedByLastName() class to perform that operation. This would involve utilizing the active db connection, performing the proper query to get the rows of users, put those users rows into User Java objects, put those User.java objects in a List, and finally process that list if needed before returning. The ability to do what I describe would all be in the Data Access Layer of your application. You would use an ORM to perform the mapping between table rows and Java objects and vice versa.

Chaetopod answered 26/3, 2018 at 21:1 Comment(5)
So if Hibernate is an ORM, does it also contain the DAL? I know that ORM converts the DB rows to objects and then uses DAO to map between objects and DB.Rusert
No, the opposite. The DAL is a layer of utilities for accessing/modifying your Data. Within those utilities, the ORM helps in performing their respective functionalities.Chaetopod
Got it. So basically DAL is the DAO (kindof) right?Rusert
Let me try to give you a full example. In your Service.java, you would have as members one ore more sampleDAOImpl.java classes. If we assume Hibernate, your DAOImpl classes will have utility methods that perform hql queries to get your data. In the background Hibernate (your ORM) understands the hql queries and handles the talking with the database, in addition, it performs the mapping from table rows to Java objects. What I have described is what people call the Data Access Layer. No one component comprises the Data Access Layer, it is a combination of classes working together.Chaetopod
Got it, so basically the DAL is an "architectural name" and DAO is a class implementation.Rusert

© 2022 - 2024 — McMap. All rights reserved.