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

loresoft / MediatR.CommandQuery / 11418431610

19 Oct 2024 02:43PM UTC coverage: 58.337% (+0.9%) from 57.424%
11418431610

push

github

pwelter34
add HybridCache, misc refactors

331 of 675 branches covered (49.04%)

Branch coverage included in aggregate %.

28 of 176 new or added lines in 24 files covered. (15.91%)

156 existing lines in 33 files now uncovered.

1163 of 1886 relevant lines covered (61.66%)

19.49 hits per line

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

75.68
/src/MediatR.CommandQuery.EntityFrameworkCore/Handlers/EntityDeleteCommandHandler.cs
1
using AutoMapper;
2

3
using MediatR.CommandQuery.Commands;
4
using MediatR.CommandQuery.Definitions;
5

6
using Microsoft.EntityFrameworkCore;
7
using Microsoft.Extensions.Logging;
8

9
namespace MediatR.CommandQuery.EntityFrameworkCore.Handlers;
10

11
public class EntityDeleteCommandHandler<TContext, TEntity, TKey, TReadModel>
12
    : EntityDataContextHandlerBase<TContext, TEntity, TKey, TReadModel, EntityDeleteCommand<TKey, TReadModel>, TReadModel>
13
    where TContext : DbContext
14
    where TEntity : class, IHaveIdentifier<TKey>, new()
15
{
16
    public EntityDeleteCommandHandler(ILoggerFactory loggerFactory, TContext dataContext, IMapper mapper)
17
        : base(loggerFactory, dataContext, mapper)
4✔
18
    {
19
    }
4✔
20

21
    protected override async Task<TReadModel> Process(EntityDeleteCommand<TKey, TReadModel> request, CancellationToken cancellationToken)
22
    {
23
        ArgumentNullException.ThrowIfNull(request);
4✔
24

25
        var dbSet = DataContext
4✔
26
            .Set<TEntity>();
4✔
27

28
        var keyValue = new object[] { request.Id };
4✔
29

30
        var entity = await dbSet
4✔
31
            .FindAsync(keyValue, cancellationToken)
4✔
32
            .ConfigureAwait(false);
4✔
33

34
        if (entity == null)
4!
UNCOV
35
            return default!;
×
36

37
        // apply update metadata
38
        if (entity is ITrackUpdated updateEntity)
4✔
39
        {
40
            updateEntity.UpdatedBy = request.ActivatedBy;
4✔
41
            updateEntity.Updated = request.Activated;
4✔
42
        }
43

44
        // entity supports soft delete
45
        if (entity is ITrackDeleted deleteEntity)
4!
46
        {
UNCOV
47
            deleteEntity.IsDeleted = true;
×
48
        }
49
        else
50
        {
51
            // when history is tracked, need to update the entity with update metadata before deleting
52
            if (entity is ITrackHistory and ITrackUpdated)
4!
53
            {
UNCOV
54
                await DataContext
×
55
                    .SaveChangesAsync(cancellationToken)
×
56
                    .ConfigureAwait(false);
×
57
            }
58

59
            // delete the entity
60
            dbSet.Remove(entity);
4✔
61
        }
62

63
        await DataContext
4✔
64
            .SaveChangesAsync(cancellationToken)
4✔
65
            .ConfigureAwait(false);
4✔
66

67
        // convert deleted entity to read model
68
        var model = Mapper.Map<TReadModel>(entity);
4✔
69

70
        return model;
4✔
71
    }
4✔
72

73
}
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