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

loresoft / MediatR.CommandQuery / 7293057134

21 Dec 2023 08:49PM UTC coverage: 58.562% (-0.5%) from 59.098%
7293057134

push

github

pwelter34
upgrade to .net 8 and lastest mediatR

230 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

20 of 54 new or added lines in 3 files covered. (37.04%)

7 existing lines in 7 files now uncovered.

560 of 911 relevant lines covered (61.47%)

19.52 hits per line

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

75.17
/src/MediatR.CommandQuery.MongoDB/DomainServiceExtensions.cs
1
using FluentValidation;
2

3
using MediatR.CommandQuery.Behaviors;
4
using MediatR.CommandQuery.Commands;
5
using MediatR.CommandQuery.Definitions;
6
using MediatR.CommandQuery.Extensions;
7
using MediatR.CommandQuery.MongoDB.Handlers;
8
using MediatR.CommandQuery.Queries;
9
using MediatR.CommandQuery.Services;
10
using MediatR.Registration;
11

12
using Microsoft.Extensions.DependencyInjection;
13
using Microsoft.Extensions.DependencyInjection.Extensions;
14

15
using MongoDB.Abstracts;
16

17
namespace MediatR.CommandQuery.MongoDB;
18

19
public static class DomainServiceExtensions
20
{
21
    public static IServiceCollection AddMediator(this IServiceCollection services)
22
    {
23
        if (services is null)
3!
24
            throw new System.ArgumentNullException(nameof(services));
×
25

26
        // Register MediatR
27
        var serviceConfig = new MediatRServiceConfiguration();
3✔
28
        ServiceRegistrar.AddRequiredServices(services, serviceConfig);
3✔
29

30
        return services;
3✔
31
    }
32

33
    public static IServiceCollection AddValidatorsFromAssembly<T>(this IServiceCollection services)
34
    {
35
        if (services is null)
3!
36
            throw new System.ArgumentNullException(nameof(services));
×
37

38
        // Register validators
39
        var scanner = AssemblyScanner.FindValidatorsInAssemblyContaining<T>();
3✔
40
        foreach (var scanResult in scanner)
90✔
41
        {
42
            //Register as interface
43
            services.TryAdd(new ServiceDescriptor(scanResult.InterfaceType, scanResult.ValidatorType, ServiceLifetime.Singleton));
42✔
44
            //Register as self
45
            services.TryAdd(new ServiceDescriptor(scanResult.ValidatorType, scanResult.ValidatorType, ServiceLifetime.Singleton));
42✔
46
        }
47

48
        return services;
3✔
49
    }
50

51

52
    public static IServiceCollection AddEntityQueries<TRepository, TEntity, TKey, TReadModel>(this IServiceCollection services)
53
        where TRepository : IMongoRepository<TEntity, TKey>
54
        where TEntity : class, IHaveIdentifier<TKey>, new()
55
        where TReadModel : class
56
    {
57
        if (services is null)
24!
58
            throw new System.ArgumentNullException(nameof(services));
×
59

60
        // standard queries
61
        services.TryAddTransient<IRequestHandler<EntityIdentifierQuery<TKey, TReadModel>, TReadModel>, EntityIdentifierQueryHandler<TRepository, TEntity, TKey, TReadModel>>();
24✔
62
        services.TryAddTransient<IRequestHandler<EntityIdentifiersQuery<TKey, TReadModel>, IReadOnlyCollection<TReadModel>>, EntityIdentifiersQueryHandler<TRepository, TEntity, TKey, TReadModel>>();
24✔
63
        services.TryAddTransient<IRequestHandler<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, EntityPagedQueryHandler<TRepository, TEntity, TKey, TReadModel>>();
24✔
64
        services.TryAddTransient<IRequestHandler<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>, EntitySelectQueryHandler<TRepository, TEntity, TKey, TReadModel>>();
24✔
65

66
        // pipeline registration, run in order registered
67
        bool supportsTenant = typeof(TReadModel).Implements<IHaveTenant<TKey>>();
24✔
68
        if (supportsTenant)
24✔
69
        {
70
            services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, TenantPagedQueryBehavior<TKey, TReadModel>>();
3✔
71
            services.AddTransient<IPipelineBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>, TenantSelectQueryBehavior<TKey, TReadModel>>();
3✔
72
        }
73

74
        bool supportsDeleted = typeof(TReadModel).Implements<ITrackDeleted>();
24✔
75
        if (supportsDeleted)
24✔
76
        {
77
            services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, DeletedPagedQueryBehavior<TReadModel>>();
3✔
78
            services.AddTransient<IPipelineBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>, DeletedSelectQueryBehavior<TReadModel>>();
3✔
79
        }
80

81
        return services;
24✔
82
    }
83

84
    public static IServiceCollection AddEntityQueryMemoryCache<TRepository, TEntity, TKey, TReadModel>(this IServiceCollection services)
85
        where TRepository : IMongoRepository<TEntity, TKey>
86
        where TEntity : class, IHaveIdentifier<TKey>, new()
87
    {
88
        if (services is null)
×
89
            throw new System.ArgumentNullException(nameof(services));
×
90

NEW
91
        services.AddTransient<IPipelineBehavior<EntityIdentifierQuery<TKey, TReadModel>, TReadModel>, MemoryCacheQueryBehavior<EntityIdentifierQuery<TKey, TReadModel>, TReadModel>>();
×
NEW
92
        services.AddTransient<IPipelineBehavior<EntityIdentifiersQuery<TKey, TReadModel>, IReadOnlyCollection<TReadModel>>, MemoryCacheQueryBehavior<EntityIdentifiersQuery<TKey, TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
NEW
93
        services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, MemoryCacheQueryBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>>();
×
NEW
94
        services.AddTransient<IPipelineBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>, MemoryCacheQueryBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
95

96
        return services;
×
97
    }
98

99
    public static IServiceCollection AddEntityQueryDistributedCache<TRepository, TEntity, TKey, TReadModel>(this IServiceCollection services)
100
        where TRepository : IMongoRepository<TEntity, TKey>
101
        where TEntity : class, IHaveIdentifier<TKey>, new()
102
    {
103
        if (services is null)
×
104
            throw new System.ArgumentNullException(nameof(services));
×
105

NEW
106
        services.AddTransient<IPipelineBehavior<EntityIdentifierQuery<TKey, TReadModel>, TReadModel>, DistributedCacheQueryBehavior<EntityIdentifierQuery<TKey, TReadModel>, TReadModel>>();
×
NEW
107
        services.AddTransient<IPipelineBehavior<EntityIdentifiersQuery<TKey, TReadModel>, IReadOnlyCollection<TReadModel>>, DistributedCacheQueryBehavior<EntityIdentifiersQuery<TKey, TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
NEW
108
        services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, DistributedCacheQueryBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>>();
×
NEW
109
        services.AddTransient<IPipelineBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>, DistributedCacheQueryBehavior<EntitySelectQuery<TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
110

111
        return services;
×
112
    }
113

114

115
    public static IServiceCollection AddEntityCommands<TRepository, TEntity, TKey, TReadModel, TCreateModel, TUpdateModel>(this IServiceCollection services)
116
        where TRepository : IMongoRepository<TEntity, TKey>
117
        where TEntity : class, IHaveIdentifier<TKey>, new()
118
        where TCreateModel : class
119
        where TUpdateModel : class
120
    {
121
        if (services is null)
21!
122
            throw new System.ArgumentNullException(nameof(services));
×
123

124
        services.TryAddSingleton<IPrincipalReader, PrincipalReader>();
21✔
125

126
        services
21✔
127
            .AddEntityCreateCommand<TRepository, TEntity, TKey, TReadModel, TCreateModel>()
21✔
128
            .AddEntityUpdateCommand<TRepository, TEntity, TKey, TReadModel, TUpdateModel>()
21✔
129
            .AddEntityUpsertCommand<TRepository, TEntity, TKey, TReadModel, TUpdateModel>()
21✔
130
            .AddEntityPatchCommand<TRepository, TEntity, TKey, TReadModel>()
21✔
131
            .AddEntityDeleteCommand<TRepository, TEntity, TKey, TReadModel>();
21✔
132

133
        return services;
21✔
134
    }
135

136

137
    public static IServiceCollection AddEntityCreateCommand<TRepository, TEntity, TKey, TReadModel, TCreateModel>(this IServiceCollection services)
138
        where TRepository : IMongoRepository<TEntity, TKey>
139
        where TEntity : class, IHaveIdentifier<TKey>, new()
140
        where TCreateModel : class
141
    {
142
        if (services is null)
21!
143
            throw new System.ArgumentNullException(nameof(services));
×
144

145
        // standard crud commands
146
        services.TryAddTransient<IRequestHandler<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, EntityCreateCommandHandler<TRepository, TEntity, TKey, TCreateModel, TReadModel>>();
21✔
147

148
        // pipeline registration, run in order registered
149
        var createType = typeof(TCreateModel);
21✔
150
        bool supportsTenant = createType.Implements<IHaveTenant<TKey>>();
21✔
151
        if (supportsTenant)
21✔
152
        {
153
            services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, TenantDefaultCommandBehavior<TKey, TCreateModel, TReadModel>>();
3✔
154
            services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, TenantAuthenticateCommandBehavior<TKey, TCreateModel, TReadModel>>();
3✔
155
        }
156

157
        bool supportsTracking = createType.Implements<ITrackCreated>();
21✔
158
        if (supportsTracking)
21✔
159
            services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, TrackChangeCommandBehavior<TCreateModel, TReadModel>>();
21✔
160

161
        services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TCreateModel, TReadModel>>();
21✔
162
        services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<TKey, TCreateModel, TReadModel>>();
21✔
163

164
        return services;
21✔
165
    }
166

167
    public static IServiceCollection AddEntityUpdateCommand<TRepository, TEntity, TKey, TReadModel, TUpdateModel>(this IServiceCollection services)
168
        where TRepository : IMongoRepository<TEntity, TKey>
169
        where TEntity : class, IHaveIdentifier<TKey>, new()
170
        where TUpdateModel : class
171
    {
172
        if (services is null)
21!
173
            throw new System.ArgumentNullException(nameof(services));
×
174

175
        // allow query for update models
176
        services.TryAddTransient<IRequestHandler<EntityIdentifierQuery<TKey, TUpdateModel>, TUpdateModel>, EntityIdentifierQueryHandler<TRepository, TEntity, TKey, TUpdateModel>>();
21✔
177
        services.TryAddTransient<IRequestHandler<EntityIdentifiersQuery<TKey, TUpdateModel>, IReadOnlyCollection<TUpdateModel>>, EntityIdentifiersQueryHandler<TRepository, TEntity, TKey, TUpdateModel>>();
21✔
178

179
        // standard crud commands
180
        services.TryAddTransient<IRequestHandler<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, EntityUpdateCommandHandler<TRepository, TEntity, TKey, TUpdateModel, TReadModel>>();
21✔
181

182
        // pipeline registration, run in order registered
183
        var updateType = typeof(TUpdateModel);
21✔
184
        bool supportsTenant = updateType.Implements<IHaveTenant<TKey>>();
21✔
185
        if (supportsTenant)
21✔
186
        {
187
            services.AddTransient<IPipelineBehavior<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TenantDefaultCommandBehavior<TKey, TUpdateModel, TReadModel>>();
3✔
188
            services.AddTransient<IPipelineBehavior<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TenantAuthenticateCommandBehavior<TKey, TUpdateModel, TReadModel>>();
3✔
189
        }
190

191
        bool supportsTracking = updateType.Implements<ITrackUpdated>();
21✔
192
        if (supportsTracking)
21✔
193
            services.AddTransient<IPipelineBehavior<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TrackChangeCommandBehavior<TUpdateModel, TReadModel>>();
21✔
194

195
        services.AddTransient<IPipelineBehavior<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TUpdateModel, TReadModel>>();
21✔
196
        services.AddTransient<IPipelineBehavior<EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<TKey, TUpdateModel, TReadModel>>();
21✔
197

198
        return services;
21✔
199
    }
200

201
    public static IServiceCollection AddEntityUpsertCommand<TRepository, TEntity, TKey, TReadModel, TUpdateModel>(this IServiceCollection services)
202
        where TRepository : IMongoRepository<TEntity, TKey>
203
        where TEntity : class, IHaveIdentifier<TKey>, new()
204
        where TUpdateModel : class
205
    {
206
        if (services is null)
21!
207
            throw new System.ArgumentNullException(nameof(services));
×
208

209
        // standard crud commands
210
        services.TryAddTransient<IRequestHandler<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, EntityUpsertCommandHandler<TRepository, TEntity, TKey, TUpdateModel, TReadModel>>();
21✔
211

212
        // pipeline registration, run in order registered
213
        var updateType = typeof(TUpdateModel);
21✔
214
        bool supportsTenant = updateType.Implements<IHaveTenant<TKey>>();
21✔
215
        if (supportsTenant)
21✔
216
        {
217
            services.AddTransient<IPipelineBehavior<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TenantDefaultCommandBehavior<TKey, TUpdateModel, TReadModel>>();
3✔
218
            services.AddTransient<IPipelineBehavior<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TenantAuthenticateCommandBehavior<TKey, TUpdateModel, TReadModel>>();
3✔
219
        }
220

221
        bool supportsTracking = updateType.Implements<ITrackUpdated>();
21✔
222
        if (supportsTracking)
21✔
223
            services.AddTransient<IPipelineBehavior<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, TrackChangeCommandBehavior<TUpdateModel, TReadModel>>();
21✔
224

225
        services.AddTransient<IPipelineBehavior<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TUpdateModel, TReadModel>>();
21✔
226
        services.AddTransient<IPipelineBehavior<EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<TKey, TUpdateModel, TReadModel>>();
21✔
227

228
        return services;
21✔
229
    }
230

231
    public static IServiceCollection AddEntityPatchCommand<TRepository, TEntity, TKey, TReadModel>(this IServiceCollection services)
232
        where TRepository : IMongoRepository<TEntity, TKey>
233
        where TEntity : class, IHaveIdentifier<TKey>, new()
234
    {
235
        if (services is null)
21!
236
            throw new System.ArgumentNullException(nameof(services));
×
237

238
        // standard crud commands
239
        services.TryAddTransient<IRequestHandler<EntityPatchCommand<TKey, TReadModel>, TReadModel>, EntityPatchCommandHandler<TRepository, TEntity, TKey, TReadModel>>();
21✔
240

241
        // pipeline registration, run in order registered
242
        services.AddTransient<IPipelineBehavior<EntityPatchCommand<TKey, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<TKey, TEntity, TReadModel>>();
21✔
243

244
        return services;
21✔
245
    }
246

247
    public static IServiceCollection AddEntityDeleteCommand<TRepository, TEntity, TKey, TReadModel>(this IServiceCollection services)
248
        where TRepository : IMongoRepository<TEntity, TKey>
249
        where TEntity : class, IHaveIdentifier<TKey>, new()
250
    {
251
        if (services is null)
21!
252
            throw new System.ArgumentNullException(nameof(services));
×
253

254
        // standard crud commands
255
        services.TryAddTransient<IRequestHandler<EntityDeleteCommand<TKey, TReadModel>, TReadModel>, EntityDeleteCommandHandler<TRepository, TEntity, TKey, TReadModel>>();
21✔
256

257
        // pipeline registration, run in order registered
258
        services.AddTransient<IPipelineBehavior<EntityDeleteCommand<TKey, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<TKey, TEntity, TReadModel>>();
21✔
259

260
        return services;
21✔
261
    }
262
}
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