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

lucaslorentz / auto-compute / 11755625072

09 Nov 2024 10:48AM UTC coverage: 79.945% (-0.09%) from 80.033%
11755625072

push

github

lucaslorentz
Add initial support for computed navigations and observers

159 of 359 new or added lines in 22 files covered. (44.29%)

41 existing lines in 7 files now uncovered.

1459 of 1825 relevant lines covered (79.95%)

113.62 hits per line

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

22.58
/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, TProperty>(
23
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
24
        Expression<Func<TEntity, TProperty>> propertyExpression,
25
        Expression<Func<TEntity, TProperty>> computedExpression,
26
        ChangeCalculationSelector<TProperty, 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, TProperty> ComputedNavigation<TEntity, TProperty>(
47
        this EntityTypeBuilder<TEntity> entityTypeBuilder,
48
        Expression<Func<TEntity, TProperty>> navigationExpression,
49
        Expression<Func<TEntity, TProperty>> computedExpression,
50
        ChangeCalculationSelector<TProperty, TProperty> calculationSelector)
51
        where TEntity : class
52
        where TProperty : 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 changeCalculation = calculationSelector(ChangeCalculations<TValue>.Instance);
×
87

NEW
88
        entityTypeBuilder.Metadata.AddComputedFactory(
×
NEW
89
            ComputedFactory.CreateComputedObserverFactory(
×
NEW
90
                computedExpression,
×
NEW
91
                changeCalculation,
×
NEW
92
                callback));
×
93
    }
94
}
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

© 2025 Coveralls, Inc