News
Entity Framework 7 Preview 7 Adds & Improves Interceptors
Entity Framework 7 Preview 7 is all about Interceptors, used for working with EF Core operations.
Interceptors enable interception, modification and/or suppression of EF Core operations, such as low-level database operations to execute a command and higher-level operations like calls to SaveChanges
.
"Interceptors support async operations and the ability to modify or suppress operations, making them more powerful than traditional events, logging, and diagnostics," Microsoft said this week in announcing the preview. While Interceptor functionality is somewhat similar to that of events in terms of reacting to what EF Core is doing, they also let developers change the behavior of EF by suppressing or replacing actions that EF was going to perform, or by modifying or replacing action results. They can even perform async database access, such as changing complete database commands.
For this dev cycle, following general EF Core themes on the Themes of .NET site, the team introduced support for several new interceptors and improved existing interceptors.
Among the new Interceptor offerings is IMaterializationInterceptor, which can change or replace the entity instance before and after an entity instance is created, and before and after properties of that instance are initialized.
The post goes into detail about LINQ expression tree interception, optimistic concurrency interception and more, explaining how it can sometimes take an enormous amount of code to accomplish a simple task like making a change to a query.
"This brings us to a critical point about interceptors," Microsoft said. "Always ask yourself if there is an easier way of doing what you want. Interceptors are powerful, but it's easy to get things wrong. They are, as the saying goes, an easy way to shoot yourself in the foot."
Existing interceptors that were enhanced in this preview include:
EF7 also includes new traditional .NET events for:
While Interceptor discussion dominates the announcement post, other improvements include:
Going forward, Microsoft will continue work in preparation for a General Availability release expected in November. The dev team collects feedback from feature requests that are voted on by developers in the project's GitHub repo. Top feature requests are illustrated in the graphic above.
Microsof'ts "Plan for Entity Framework Core 7.0" indicates large investments in EF7 will fall mainly under the following themes:
- Highly requested features
- NET platforms and ecosystem
- Clear path forward from EF6
- Performance
This week Microsoft also shipped .NET 7 Preview 7 and Visual Studio 2022 17.3.
About the Author
David Ramel is an editor and writer at Converge 360.