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?
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.
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."
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.
© 2022 - 2024 — McMap. All rights reserved.