Skip to content

Automatically update persisted computed properties in EF Core on save changes.

License

Notifications You must be signed in to change notification settings

lucaslorentz/auto-compute

Repository files navigation

⚡ EF Core Auto Compute CI NuGet Coverage Status

Automatically update persisted computed properties in EF Core—so you don’t have to!


Table of Contents


Why Use Auto Compute?

Denormalizing data is a powerful performance optimization, but manually managing computed properties is error-prone and time-consuming. With EF Core Auto Compute:

  • 🚀 Boost Performance
    Eliminate costly joins and runtime calculations by persisting derived data.
  • 🔄 Stay Consistent
    Automatically keep computed values accurate as your data changes.
  • 💡 Write Less Code
    Define computed properties once—no more manual update logic!

Features

🔧 Computed Properties

Automatically recalculate properties when dependencies change.
Ideal for aggregations like counts, sums, or complex expressions.

modelBuilder.Entity<Person>().ComputedProperty(
   p => p.NumberOfCats, // Property to map
   p => p.Pets.Count(pet => pet.Type == PetType.Cat)); // Computed expression

⚡ Incremental Updates

Optimize performance by updating values without loading entire collections.
Perfect for large collections.

modelBuilder.Entity<Post>().ComputedProperty(
   p => p.LikeCount, // Property to map
   p => p.Interactions.Count(i => i.Type == InteractionType.Like), // Computed expression
   c => c.NumberIncremental()); // Change calculation

🔗 Computed Navigations

Auto-sync related entities when their dependencies change.
Great for cloning relationships or maintaining derived collections.

modelBuilder.Entity<Order>().ComputedNavigation(
   o => o.Items, // Navigation to map
   o => o.CloneFrom != null
     ? o.CloneFrom.Items.Select(i => new OrderItem
     {
         Product = i.Product,
         Quantity = i.Quantity
     }).ToArray()
     : o.AsComputedUntracked().Items, // Computed expression
   c => c.CurrentValue(), // Change calculation
   c => c.ReuseItemsByKey(e => new { e.Product })); // Other options

👀 Computed Observers

React to changes with event-driven callbacks.
Notify users, log data, or trigger workflows when values update.

modelBuilder.Entity<Person>().ComputedObserver(
   p => p.Pets.Count(x => x.Type == PetType.Cat), // Observed expression
   p => p.IsActive, // Filter
   c => c.CurrentValue(), // Change calculation
   async (person, numberOfCats) =>
   {
     Console.WriteLine($"Person {person.FullName} now has {numberOfCats} cats.");
   }); // Callback

Getting Started

  1. Install the Package:

    dotnet add package LLL.AutoCompute.EFCore
    
  2. Enable AutoCompute:

    dbContextOptions.UseAutoCompute();
  3. Map computed properties:

    modelBuilder.Entity<Person>().ComputedProperty(
        p => p.NumberOfCats, // Property to map
        p => p.Pets.Count(pet => pet.Type == PetType.Cat)); // Computed expression

Done! Computed properties will now update automatically during dbContext.SaveChanges().

How It Works

Auto Compute analyzes computed expressions and tracks all referenced data. When any of the referenced data changes, it traverses inverse navigations to identify affected entities and updates their computed properties accordingly.

Diagram

Roadmap

  • Computed properties, collections and references
  • Incremental computed properties
    • Incremental calculation
    • Load all navigation items to evaluate Linq All/Any/Contains
    • Load necessary navigation items to evaluate Linq Distinct
  • Computed observers
  • Async queue-based update for hot computed properties
    • Throttled update - Update X seconds after the first change
    • Debounced update - Update X seconds after the last change
  • Methods to query inconsistent entities
  • Periodic consistency check and fixes
  • Web-based UI for schema introspection, consistency check and fix

License

MIT licensed. See LICENSE for details.

About

Automatically update persisted computed properties in EF Core on save changes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages