Skip to main content
Version: Next

Dapper Integration and High Performance Queries

1. Introduction​

This document aims to provide an in-depth analysis of the purpose and scenarios for Dapper integration in this project, especially its advantages in complex queries, batch operations, or performance-sensitive businesses. We will explore the structure design of the ICMSPluginDapperRepository interface and the CMSPluginDapperRepository implementation class, explaining how they coexist and complement EF Core. We also provide code examples for Dapper-based native SQL queries, stored procedure calls, and batch insert/update operations, and discuss transaction management, parameterized query security, and SQL injection protection strategies.

2. Project Structure​

This project adopts a modular layered architecture. Dapper integration is mainly located in the EntityFrameworkCore layer, serving as a supplement to EF Core functionality. Dapper-related interfaces are defined in the Domain layer, implemented in the EntityFrameworkCore layer, and used in the Application layer via dependency injection.

3. Core Components​

The core components of Dapper integration include the ICMSPluginDapperRepository interface and the CMSPluginDapperRepository implementation class. The interface defines basic methods for obtaining database connections and transactions, while the implementation class inherits from the ABP framework's DapperRepository, providing the capability to integrate with the EF Core context.

4. Architecture Overview​

In this project, Dapper serves as a supplement to EF Core, primarily used to handle complex queries, batch operations, and performance-sensitive business scenarios. Both coexist by sharing the same database context, ensuring transaction consistency.

5. Detailed Component Analysis​

Dapper Repository Interface Analysis​

The ICMSPluginDapperRepository interface defines the basic contract for the Dapper repository, including methods to retrieve database connections and transactions. This interface inherits from ITransientDependency, ensuring a new instance is created for each request.

Dapper Repository Implementation Analysis​

The CMSPluginDapperRepository class implements the ICMSPluginDapperRepository interface and inherits from the ABP framework's DapperRepository<ICMSPluginDbContext>. By injecting IDbContextProvider<ICMSPluginDbContext> via the constructor, it achieves integration with the EF Core context.

Dapper Usage Scenario Analysis​

In CMSPluginQueryAppService, an instance of ICMSPluginDapperRepository is obtained via dependency injection to perform complex query operations. This design pattern allows Dapper to coexist with EF Core, leveraging the strengths of each.

6. Dependency Analysis​

Dapper integration is introduced via the NuGet package Volo.Abp.Dapper, which, together with Volo.Abp.EntityFrameworkCore, forms the foundation of the data access layer. This design ensures seamless integration of Dapper with EF Core.

7. Performance Considerations​

Dapper has significant advantages in the following scenarios:

  • Complex multi-table joins
  • Batch insert/update operations
  • Stored procedure calls
  • Performance-sensitive queries

Compared to EF Core, Dapper executes native SQL directly, avoiding the overhead of ORM query parsing and object mapping, making it particularly suitable for handling large data volumes and complex queries.

8. Troubleshooting Guide​

When using Dapper, pay attention to the following points:

  • Ensure the database connection string is configured correctly.
  • Use parameterized queries to prevent SQL injection.
  • Manage database transactions correctly.
  • Be aware of concurrency issues when Dapper shares the context with EF Core.

9. Conclusion​

In this project, Dapper acts as an effective supplement to EF Core, providing efficient solutions for complex queries and performance-sensitive businesses. Through the design of the ICMSPluginDapperRepository interface and the CMSPluginDapperRepository implementation class, seamless integration with EF Core is achieved. It is recommended to prioritize Dapper in the following scenarios:

  • Complex multi-table joins
  • Batch data operations
  • Stored procedure calls
  • Queries with extremely high performance requirements

For standard CRUD operations, it is still recommended to use EF Core to maintain code consistency and development efficiency.