Full Fledged ORM Challenges
When it comes to full-fledged ORM like Entity Framework, we have the below-mentioned challenges
- Classic (N+1) problem due to lazy loading with Entity Framework for database reads.
- Developer’s responsibility to either disable lazy loading or use it sparingly for specific use cases.
- LINQ query expertise required.
- Entity Framework slows down the data-heavy read operations if used incorrectly. Overkill for the reads.
- Developer consistently must verify the queries that get translated within SQL for every LINQ execution.
- Incorrect LINQ queries can have a drastic performance impact on the application.
- Challenges w.r.t to managing migrations and database schema updates.
Micro ORM to the Rescue
Micro ORM like Dapper can solve these issues. Dapper is an open-source, lightweight ORM developed by the Stack Overflow team. Mentioned below are the advantages of using such an ORM.
As seen below, Dapper Custom C# Library can be integrated within .Net layered architecture or within CQRS design.
Micro ORM & Full-fledged ORM’s (Entity Framework or NHibernate) can co-exist within a .NET application.
Note: Block marked as “Custom Library” is where the Micro ORM custom implementation will integrate.
- Extremely fast compared to other ORMs primarily because of its lightweight.
- Supports easy integration with MSSQL, Oracle, MySQL, SQLite, PostgreSQL (ADO.Net provider databases).
- Built with performance and ease of use in mind.
- Developers get the much faster speed of execution with all read or write operations compared to Entity Framework or NHibernate & performance is almost the same as ADO.Net as it’s a wrapper on top of ADO.Net.
- Fastest ORM among the other open-source micro ORM’s that are available.
- Supports transactions, stored procedures, bulk inserts of data.
- Provides built-in support for .Net Async features (which came with .Net 4.5).
- Leverages C# DLR (dynamic language runtime) which is a huge advantage.
- Works well with the database’s first approach leveraging stored procedures or RAW SQL to the fullest.
- The developer can use stored procedures with Dapper API leveraging SQL benefits.
- Dapper API helps to rule out SQL injections attacks with parameterized query parameters.
- Dapper, being a ADO.NET wrapper eliminates the need for the developers to write all the complex ADO.NET logic to get data. Thereby acts as a handy replacement to hand written ADO.Net.
- It enforces using SQL stored procedures to its maximum potential.
- Complex logic gets wrapped within Dapper and the developer focuses only on passing the stored procedure name, parameters etc to Dapper API & Dapper does the rest to execute T-SQL.
Other Use case (within a project using CQRS Pattern)
- Most windows or web applications heavily rely on reading operations which accounts for 80% of all operations.
Dapper can be easily integrated for all those read operations within such applications.
- Dapper can help read data from the Materialized / Indexed View in the fastest possible manner.
- Dapper ORM can be used within the Query part of the CQRS (command query responsibility segregation) implementation. The command part rather sticks to domain-driven design (DDD) & supports ACID transactions to facilitate data integrity using an ORM like Entity Framework or NHibernate.
- Thereby, both a Micro ORM & a full-fledged ORM can co-exist within a CQRS implementation.
Why Dapper is so fast
- It’s fast because it uses dynamic method generation (MSIL) to assign column values to properties.
- It’s a lightweight wrapper and an abstraction on top of ADO.Net.
- It focuses just on executing user-supplied queries and mapping the results to CLR object properties.
- It caches information for every query it runs thus allowing faster object materialization & parameter processing.
Dapper Micro ORM Vs Entity Framework Core benchmark
Note: Entity Framework 6 & 7 are much slower compared to EF core.
Overall a must-have tool for all database related operations in a web or windows application.