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

loresoft / MediatR.CommandQuery / 8911801772

01 May 2024 04:09PM CUT coverage: 57.379% (-1.8%) from 59.163%
8911801772

push

github

pwelter34
tweak build

342 of 700 branches covered (48.86%)

Branch coverage included in aggregate %.

1011 of 1658 relevant lines covered (60.98%)

18.48 hits per line

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

72.5
/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)
2✔
18
    {
19
    }
2✔
20

21
    protected override async Task<TReadModel> Process(EntityDeleteCommand<TKey, TReadModel> request, CancellationToken cancellationToken)
22
    {
23
        if (request is null)
2!
24
            throw new ArgumentNullException(nameof(request));
×
25

26
        var dbSet = DataContext
2✔
27
            .Set<TEntity>();
2✔
28

29
        var keyValue = new object[] { request.Id };
2✔
30

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

35
        if (entity == null)
2!
36
            return default!;
×
37

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

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

60
            // delete the entity
61
            dbSet.Remove(entity);
2✔
62
        }
63

64
        await DataContext
2✔
65
            .SaveChangesAsync(cancellationToken)
2✔
66
            .ConfigureAwait(false);
2✔
67

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

71
        return model;
2✔
72
    }
2✔
73

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