• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / auto-compute / 11744711953

08 Nov 2024 03:11PM UTC coverage: 82.369%. First build
11744711953

push

github

lucaslorentz
Add initial support for computed navigations and observers

185 of 334 new or added lines in 17 files covered. (55.39%)

1509 of 1832 relevant lines covered (82.37%)

116.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.0
/src/LLL.AutoCompute.EFCore/Extensions/EntityTypeBuilderExtensions.cs
1
using System.Linq.Expressions;
2
using LLL.AutoCompute.EFCore.Metadata.Internal;
3
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4

5
namespace LLL.AutoCompute.EFCore;
6

7
public static class EntityTypeBuilderExtensions
8
{
9
    public static PropertyBuilder<TProperty> ComputedProperty<TEntity, TProperty>(
10
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
11
        Expression<Func<TEntity, TProperty>> propertyExpression,
12
        Expression<Func<TEntity, TProperty>> computedExpression)
13
        where TEntity : class
14
    {
15
        return ComputedProperty(
8✔
16
            entityTypeBuilder,
8✔
17
            propertyExpression,
8✔
18
            computedExpression,
8✔
19
            static c => c.CurrentValue());
16✔
20
    }
21

22
    public static PropertyBuilder<TProperty> ComputedProperty<TEntity, TValue, TProperty>(
23
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
24
        Expression<Func<TEntity, TProperty>> propertyExpression,
25
        Expression<Func<TEntity, TValue>> computedExpression,
26
        ChangeCalculationSelector<TValue, TProperty> calculationSelector)
27
        where TEntity : class
28
    {
29
        return entityTypeBuilder.Property(propertyExpression)
12✔
30
            .AutoCompute(computedExpression, calculationSelector);
12✔
31
    }
32

33
    public static NavigationBuilder<TEntity, TReference> ComputedNavigation<TEntity, TReference>(
34
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
35
        Expression<Func<TEntity, TReference>> navigationExpression,
36
        Expression<Func<TEntity, TReference>> computedExpression)
37
        where TEntity : class
38
        where TReference : class
39
    {
NEW
40
        return entityTypeBuilder.ComputedNavigation(
×
NEW
41
            navigationExpression,
×
NEW
42
            computedExpression,
×
NEW
43
            static c => c.CurrentValue());
×
44
    }
45

46
    public static NavigationBuilder<TEntity, TReference> ComputedNavigation<TEntity, TValue, TReference>(
47
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
48
        Expression<Func<TEntity, TReference>> navigationExpression,
49
        Expression<Func<TEntity, TValue>> computedExpression,
50
        ChangeCalculationSelector<TValue, TReference> calculationSelector)
51
        where TEntity : class
52
        where TReference : class
53
    {
NEW
54
        return entityTypeBuilder.Navigation(navigationExpression!)
×
NEW
55
            .AutoCompute(computedExpression, calculationSelector);
×
56
    }
57

58
    public static void ComputedObserver<TEntity, TValue, TChange>(
59
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
60
        Expression<Func<TEntity, TValue>> computedExpression,
61
        ChangeCalculationSelector<TValue, TChange> calculationSelector,
62
        Func<TEntity, TChange?, Task> callback)
63
        where TEntity : class
64
    {
NEW
65
        entityTypeBuilder
×
NEW
66
            .ComputedObserver(
×
NEW
67
                computedExpression,
×
NEW
68
                calculationSelector,
×
NEW
69
                async (ComputedChangeEventData<TEntity, TChange> eventData) =>
×
NEW
70
                {
×
NEW
71
                    foreach (var (entity, change) in eventData.Changes)
×
NEW
72
                    {
×
NEW
73
                        await callback(entity, change);
×
NEW
74
                    }
×
NEW
75
                }
×
NEW
76
            );
×
77
    }
78

79
    public static void ComputedObserver<TEntity, TValue, TChange>(
80
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
81
        Expression<Func<TEntity, TValue>> computedExpression,
82
        ChangeCalculationSelector<TValue, TChange> calculationSelector,
83
        Func<ComputedChangeEventData<TEntity, TChange>, Task> callback)
84
        where TEntity : class
85
    {
NEW
86
        var computedObserverFactories = ComputedAnnotations.ComputedObserverFactories.GetOrAdd(
×
NEW
87
            entityTypeBuilder.Metadata,
×
NEW
88
            static () => []
×
NEW
89
        );
×
90

NEW
91
        var changeCalculation = calculationSelector(ChangeCalculations<TValue>.Instance);
×
92

NEW
93
        computedObserverFactories.Add(
×
NEW
94
            ComputedFactory.CreateComputedObserverFactory(
×
NEW
95
                computedExpression,
×
NEW
96
                changeCalculation,
×
NEW
97
                callback));
×
98
    }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc