Browse Source

Update POST.md

pull/20134/head
Engincan VESKE 2 years ago
committed by GitHub
parent
commit
e4657437a2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 20
      docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md

20
docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md

@ -4,11 +4,11 @@
ABP provides data filters that can filter queries automatically based on some rules. This feature is useful for implementing multi-tenancy, soft delete, and other global filters. It uses [EF Core's Global Query Filters system](https://learn.microsoft.com/en-us/ef/core/querying/filters) for the EF Core Integration.
EF Core Global Query Filters generate filter conditions and apply them to query SQL. ABP controls whether this filter condition takes effect through a variable. However, this variable may cause performance losses in some scenarios.
EF Core Global Query Filters generate filter conditions and apply them to SQL queries. ABP controls whether this filter condition takes effect through a variable. However, this variable may cause performance losses in some scenarios.
## The Filter Condition Variable
Think of a scenario with a global filter `IIsActive` that filters out inactive entities.
Think of a scenario with a global filter `IIsActive`, which filters out inactive entities:
```csharp
public class Book : IIsActive
@ -21,20 +21,20 @@ public class Book : IIsActive
The SQL generated by the [EF Core Global Query Filters](https://learn.microsoft.com/en-us/ef/core/querying/filters) is as follows:
The `__ef_filter__p_0` variable controls whether the filter condition takes effect.
```SQL
SELECT * FROM [AppBooks] AS [a]
WHERE (@__ef_filter__p_0 = CAST(1 AS bit) OR [a].[IsActive] = CAST(1 AS bit))
```
> The `__ef_filter__p_0` variable controls whether the filter condition takes effect.
The generated SQL is not optimal, and some databases do not optimize it well.
## Using User-defined function mapping for global filters
In the [latest ABP](https://github.com/abpframework/abp/pull/20065), we use the [User-defined function mapping](https://learn.microsoft.com/en-us/ef/core/querying/user-defined-function-mapping) to implement global filters more efficiently. This feature is enabled by default.
In the [upcoming preview version of ABP, v8.3.0-rc.1](https://github.com/abpframework/abp/pull/20065), we start the [User-defined function mapping](https://learn.microsoft.com/en-us/ef/core/querying/user-defined-function-mapping) to implement global filters more efficiently. This feature is enabled by default, so you don't need to make any changes if you create a new solution and start from scratch. Otherwise, you can enable it easily by following the instructions below.
To use this new feature for your custom global filters, you need to change your `DbContext` like below:
To use this new feature for your custom global filters, you need to change your `DbContext` as follows:
````csharp
protected bool IsActiveFilterEnabled => DataFilter?.IsEnabled<IIsActive>() ?? false;
@ -102,16 +102,16 @@ public override string GetCompiledQueryCacheKey()
}
````
After these changes, the SQL generated by the EF Core Global Query Filters is as follows:
After these changes, the SQL generated by the EF Core Global Query Filters will be as follows:
Enable `IIsActive` filter:
Enabling the `IIsActive` filter:
```SQL
SELECT * FROM [AppBooks] AS [a] WHERE
[a].[IsActive] = CAST(1 AS bit)
```
Disable `IIsActive` filter:
Disabling the `IIsActive` filter:
```SQL
SELECT * FROM [AppBooks] AS [a]
@ -119,7 +119,7 @@ SELECT * FROM [AppBooks] AS [a]
## Conclusion
We have implemented global filters using User-defined function mapping, which can generate more efficient SQL and thus improve performance.
We have implemented global filters using [User-defined function mapping](https://learn.microsoft.com/en-us/ef/core/querying/user-defined-function-mapping), which can generate more efficient SQL and thus improve performance.
Upgrade to the latest ABP version and enjoy the performance improvement!

Loading…
Cancel
Save