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

lucaslorentz / auto-compute / 11758855041

09 Nov 2024 07:22PM UTC coverage: 77.28% (+0.07%) from 77.208%
11758855041

push

github

lucaslorentz
Add initial support for computed navigations and observers

166 of 431 new or added lines in 25 files covered. (38.52%)

41 existing lines in 7 files now uncovered.

1466 of 1897 relevant lines covered (77.28%)

109.91 hits per line

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

0.0
/src/LLL.AutoCompute.EFCore/Metadata/Internal/ComputedNavigationKey.cs
1
using System.Linq.Expressions;
2
using LLL.AutoCompute.EFCore.Internal;
3
using Microsoft.EntityFrameworkCore;
4
using Microsoft.EntityFrameworkCore.ChangeTracking;
5
using Microsoft.EntityFrameworkCore.Metadata;
6

7
namespace LLL.AutoCompute.EFCore.Metadata.Internal;
8

9
public abstract class ComputedNavigationKey(
10
    INavigationBase navigation,
11
    IUnboundChangesProvider changesProvider,
12
    LambdaExpression keyExpression
NEW
13
) : Computed(changesProvider)
×
14
{
NEW
15
    public INavigationBase Navigation => navigation;
×
NEW
16
    public LambdaExpression KeyExpression => keyExpression;
×
17
}
18

19
public class ComputedNavigationKey<TEntity, TProperty, TKey>(
20
    INavigationBase navigation,
21
    IUnboundChangesProvider<IEFCoreComputedInput, TEntity, TKey> changesProvider,
22
    Expression<Func<TProperty, TKey>> keyExpression
NEW
23
) : ComputedNavigationKey(navigation, changesProvider, keyExpression)
×
24
    where TEntity : class
25
{
NEW
26
    public new IUnboundChangesProvider<IEFCoreComputedInput, TEntity, TKey> ChangesProvider => changesProvider;
×
27

28
    public override string ToDebugString()
29
    {
NEW
30
        return Navigation.ToString()!;
×
31
    }
32

33
    public override async Task<int> Update(DbContext dbContext)
34
    {
NEW
35
        var numberOfUpdates = 0;
×
NEW
36
        var input = dbContext.GetComputedInput();
×
NEW
37
        var changes = await changesProvider.GetChangesAsync(input, null);
×
NEW
38
        foreach (var (entity, change) in changes)
×
39
        {
NEW
40
            var entityEntry = dbContext.Entry(entity);
×
NEW
41
            var navigationEntry = entityEntry.Navigation(Navigation);
×
42

NEW
43
            var newKey = ChangesProvider.ChangeCalculation.IsIncremental
×
NEW
44
                ? ChangesProvider.ChangeCalculation.ApplyChange(
×
NEW
45
                    GetKey(GetOriginalValue(navigationEntry)),
×
NEW
46
                    change)
×
NEW
47
                : change;
×
48

NEW
49
            if (!Equals(GetKey((TProperty)navigationEntry.CurrentValue!), newKey))
×
50
            {
NEW
51
                var newValue = newKey is null
×
NEW
52
                    ? null
×
NEW
53
                    : await dbContext.FindAsync(navigationEntry.Metadata.TargetEntityType.ClrType, newKey);
×
54

NEW
55
                navigationEntry.CurrentValue = newValue;
×
NEW
56
                numberOfUpdates++;
×
57
            }
58
        }
NEW
59
        return numberOfUpdates;
×
60
    }
61

62
    private TKey GetKey(TProperty? value)
63
    {
NEW
64
        if (value is null)
×
NEW
65
            return default!;
×
NEW
66
        return keyExpression.Compile()(value);
×
67
    }
68

69
    private static TProperty GetOriginalValue(NavigationEntry navigationEntry)
70
    {
NEW
71
        if (navigationEntry.EntityEntry.State == EntityState.Added)
×
NEW
72
            return default!;
×
73

NEW
74
        return (TProperty)navigationEntry.GetOriginalValue()!;
×
75
    }
76
}
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