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

lucaslorentz / auto-compute / 11754242014

09 Nov 2024 07:02AM UTC coverage: 81.696% (-0.1%) from 81.803%
11754242014

push

github

lucaslorentz
Add initial support for computed navigations and observers

154 of 315 new or added lines in 14 files covered. (48.89%)

40 existing lines in 5 files now uncovered.

1455 of 1781 relevant lines covered (81.7%)

116.0 hits per line

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

34.67
/src/LLL.AutoCompute.EFCore/Metadata/Internal/ComputedFactory.cs
1
using System.Linq.Expressions;
2
using LLL.AutoCompute.EFCore.Internal;
3
using Microsoft.EntityFrameworkCore.Metadata;
4

5
namespace LLL.AutoCompute.EFCore.Metadata.Internal;
6

7
public delegate Computed ComputedFactory<TTarget>(
8
    IComputedExpressionAnalyzer<IEFCoreComputedInput> analyzer,
9
    TTarget target);
10

11
public class ComputedFactory
12
{
13
    public static ComputedFactory<IProperty> CreateComputedPropertyFactory<TEntity, TProperty, TValue, TChange>(
14
        Expression<Func<TEntity, TValue>> computedExpression,
15
        IChangeCalculation<TValue, TChange> changeCalculation,
16
        Func<TProperty, TChange, TProperty> applyChange)
17
        where TEntity : class
18
    {
19
        return (analyzer, property) =>
12✔
20
        {
12✔
21
            try
12✔
22
            {
12✔
23
                if (property.DeclaringType.ClrType != typeof(TEntity))
18✔
24
                    throw new Exception($"Expected entity type {property.DeclaringType.ClrType} but got {typeof(TEntity)}");
12✔
25

12✔
26
                var changesProvider = analyzer.GetChangesProvider(
18✔
27
                    computedExpression,
18✔
28
                    default,
18✔
29
                    changeCalculation);
18✔
30

12✔
31
                if (!changesProvider.EntityContext.AllAccessedMembers.Any())
18✔
32
                    throw new Exception("Computed expression doesn't have tracked accessed members");
12✔
33

12✔
34
                return new ComputedProperty<TEntity, TProperty, TChange>(
18✔
35
                    property,
18✔
36
                    changesProvider,
18✔
37
                    computedExpression,
18✔
38
                    applyChange);
18✔
39
            }
12✔
40
            catch (Exception ex)
12✔
41
            {
12✔
42
                throw new InvalidOperationException($"Invalid computed expression for '{property.DeclaringType.ShortName()}.{property.Name}': {ex.Message}", ex);
12✔
43
            }
12✔
44
        };
12✔
45
    }
46

47
    public static ComputedFactory<INavigationBase> CreateComputedNavigationFactory<TEntity, TProperty, TValue, TChange>(
48
        Expression<Func<TEntity, TValue>> computedExpression,
49
        IChangeCalculation<TValue, TChange> changeCalculation,
50
        Func<TProperty, TChange, TProperty> applyChange)
51
        where TEntity : class
52
        where TProperty : class
53
    {
NEW
54
        return (analyzer, navigation) =>
×
NEW
55
        {
×
NEW
56
            try
×
NEW
57
            {
×
NEW
58
                if (navigation.DeclaringType.ClrType != typeof(TEntity))
×
NEW
59
                    throw new Exception($"Expected entity type {navigation.DeclaringType.ClrType} but got {typeof(TEntity)}");
×
NEW
60
                    
×
NEW
61
                var changesProvider = analyzer.GetChangesProvider(
×
NEW
62
                    computedExpression,
×
NEW
63
                    default,
×
NEW
64
                    changeCalculation);
×
NEW
65

×
NEW
66
                if (!changesProvider.EntityContext.AllAccessedMembers.Any())
×
NEW
67
                    throw new Exception("Computed expression doesn't have tracked accessed members");
×
NEW
68

×
NEW
69
                return new ComputedNavigation<TEntity, TProperty, TChange>(
×
NEW
70
                    navigation,
×
NEW
71
                    changesProvider,
×
NEW
72
                    computedExpression,
×
NEW
73
                    applyChange);
×
NEW
74
            }
×
NEW
75
            catch (Exception ex)
×
NEW
76
            {
×
NEW
77
                throw new InvalidOperationException($"Invalid computed expression for '{navigation.DeclaringType.ShortName()}.{navigation.Name}': {ex.Message}", ex);
×
NEW
78
            }
×
NEW
79
        };
×
80
    }
81

82
    public static ComputedFactory<IEntityType> CreateComputedObserverFactory<TEntity, TValue, TChange>(
83
        Expression<Func<TEntity, TValue>> computedExpression,
84
        IChangeCalculation<TValue, TChange> changeCalculation,
85
        Func<ComputedChangeEventData<TEntity, TChange>, Task> callback)
86
        where TEntity : class
87
    {
NEW
88
        return (analyzer, entityType) =>
×
NEW
89
        {
×
NEW
90
            try
×
NEW
91
            {
×
NEW
92
                var changesProvider = analyzer.GetChangesProvider(
×
NEW
93
                    computedExpression,
×
NEW
94
                    default,
×
NEW
95
                    changeCalculation);
×
NEW
96

×
NEW
97
                if (!changesProvider.EntityContext.AllAccessedMembers.Any())
×
NEW
98
                    throw new Exception("Computed expression doesn't have tracked accessed members");
×
NEW
99

×
NEW
100
                return new ComputedObserver<TEntity, TChange>(
×
NEW
101
                    entityType,
×
NEW
102
                    changesProvider,
×
NEW
103
                    computedExpression,
×
NEW
104
                    callback);
×
NEW
105
            }
×
NEW
106
            catch (Exception ex)
×
NEW
107
            {
×
NEW
108
                throw new InvalidOperationException($"Invalid computed expression for reaction in '{entityType.Name}': {ex.Message}", ex);
×
NEW
109
            }
×
NEW
110
        };
×
111
    }
112
}
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