Search code examples
ormdata-access-layer

What is the difference between ORM and DAL?


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?


Solution

  • 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.