Read EntityFramework. DynamicFilters source code _ experiences _ design ideas _ 04, entityframework source code

Source: Internet
Author: User

Read EntityFramework. DynamicFilters source code _ experiences _ design ideas _ 04, entityframework source code

The previous few times, we learned how to use this dynamic filter from the instruction documents, examples, and unit tests. If you only know how to use this dynamic filter for the purpose, you can complete the corresponding function development, but what I want to know is:

 

I want to implement the false deletion function to delete the id = 11 in the People table.

Update people set IsDeleted = 1 where ID = 11

After deletion, query the data that has not been deleted.

Under normal circumstances, the fake deleted data is actually deleted for the user, but there is still

 

If the caller finally controls the query statement, the caller needs to manually filter out the false deleted data.

Select * from People where IsDeleted! = 1

When writing a statement, the user will not consider whether your data is falsely deleted.

 

At this time, he only needs to write select * from People to query it.

 

Actually, the query results are as follows:

 

 

When querying, you cannot find the deleted data. You should filter it out.

 

When a user does this, each statement must be processed and manually added with filtering conditions. When the user stands at the business layer to implement the business, he must take a test on the data layer, how to decouple users' businesses from the data layer

 

If the user does not perform this operation, the user needs to perform a query at the business layer.

Select * from People command and then the system automatically queries the following correct results

Then the system needs to perform dynamic processing to convert the original select * from People

Select * from People where IsDeleted! = 1. filter the false deletion conditions.

 

This is only a scenario in the dynamic filtering scenario. If there is only one such scenario in the system, we can do it manually and quickly. When there are many similar but different scenarios in the system, such as saas multi-tenant, I want to query

Currently, all data is logged on to the tenant, but there is no concept of a tenant for the login owner. This system seems like he is using it, and we are also setting it for different scenarios, if there are 100 different scenarios and 1000

Are we all going to do it one by one?

 

Is there a similarity in it that can be extracted? If the boss suddenly says that the system should not be deleted falsely, which occupies too much database space, how can we quickly remove the original settings?

 

Users are all oriented to objects. Even if users are set up, they cannot write a large SQL statement. If the database is not switched, if I change the SQL statement from oracle to mysql, do I have to change the statement every time? Is the content of each change slightly different? It is similar, but it cannot run without modification?

 

How to solve these problems, EntityFramework. DynamicFilters provides their own answers in the source code

 

Problems to be solved, such

 

EntityFramework. DynamicFilters. provides external filter construction methods through DynamicFilterExtensions. All methods are extended to the DbModelBuilder modelBuilder object in the EF framework through objects,

You can use the extension method to disable, enable, and create filters.

Supports various scenarios. For more information, see unit test.

Dynamic disabling and enabling

ModelBuilder. DisableFilterGlobally ("BlogEntryFilter") disable the filter.

ModelBuilder. EnableFilter (dbContext, filterName); enable filter

 

Filter modelBuilder. Filter ("IsDeleted", (ISoftDelete d) => d. IsDeleted, false );

Use LambdaToDbExpressionVisitor to convert the expression (ISoftDelete d) => d. IsDeleted, false) passed by the filter to a database expression.

 

Use the command truncation tool to intercept the statements to be executed to the database, add a filter created by the user, and form the final statement by using the DbExpression converted by LambdaToDbExpressionVisitor, for users, the details of manually adding statements are blocked.

 

System. Data. Entity. Infrastructure. Interception can be extended for those that do not meet our requirements.

 

//

// Summary:

// An object that implements this interface can be registered with System. Data. Entity. Infrastructure. Interception. DbInterception

// To receive configurications when Entity Framework executes commands.

//

// Remarks:

// Interceptors can also be registered in the config file of the application. See

// Http://go.microsoft.com/fwlink? LinkId = 260883 for more information about Entity

// Framework configuration.

Public interface IDbCommandInterceptor: IdbInterceptor

Command truncation interface to implement your own Interception Method

Then in InitializeDynamicFilters (this DbContext context)

Call the interceptor set and add your Interceptor to implement customization.

The anti-deletion plug-in of the Rafy framework is also implemented through the interceptor as follows:

StampSubmitInterceptor implements the abstract interceptor SubmitInterceptor

When the plug-in is initialized, add the custom plug-in to the set.

The current Interceptor. After execution, the next interceptor is called.

 

Link. InvokeNext (this, e );

The deletion plug-in of rafy is compared with the EntityFramework. DynamicFilters filter by using the command truncation tool.

 

The Rafy command truncation is the SubmitInterceptor customized by the author.

 

EntityFramework. DynamicFilters implements EF's IdbCommandInterceptor.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.