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

loresoft / MediatR.CommandQuery / 7697858967

29 Jan 2024 02:45PM UTC coverage: 58.562%. Remained the same
7697858967

push

github

web-flow
Merge pull request #473 from loresoft/dependabot/github_actions/actions/deploy-pages-4

Bump actions/deploy-pages from 2 to 4

230 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

560 of 911 relevant lines covered (61.47%)

19.53 hits per line

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

0.0
/src/MediatR.CommandQuery.Cosmos/DomainServiceExtensions.cs
1
using Cosmos.Abstracts;
2

3
using FluentValidation;
4

5
using MediatR.CommandQuery.Behaviors;
6
using MediatR.CommandQuery.Commands;
7
using MediatR.CommandQuery.Cosmos.Handlers;
8
using MediatR.CommandQuery.Definitions;
9
using MediatR.CommandQuery.Extensions;
10
using MediatR.CommandQuery.Queries;
11
using MediatR.CommandQuery.Services;
12
using MediatR.Registration;
13

14
using Microsoft.Extensions.DependencyInjection;
15
using Microsoft.Extensions.DependencyInjection.Extensions;
16

17
namespace MediatR.CommandQuery.Cosmos;
18

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

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

30
        return services;
×
31
    }
32

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

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

48
        return services;
×
49
    }
50

51

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

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

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

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

81
        return services;
×
82
    }
83

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

91
        services.AddTransient<IPipelineBehavior<EntityIdentifierQuery<string, TReadModel>, TReadModel>, MemoryCacheQueryBehavior<EntityIdentifierQuery<string, TReadModel>, TReadModel>>();
×
92
        services.AddTransient<IPipelineBehavior<EntityIdentifiersQuery<string, TReadModel>, IReadOnlyCollection<TReadModel>>, MemoryCacheQueryBehavior<EntityIdentifiersQuery<string, TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
93
        services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, MemoryCacheQueryBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>>();
×
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, TReadModel>(this IServiceCollection services)
100
        where TRepository : ICosmosRepository<TEntity>
101
        where TEntity : class, IHaveIdentifier<string>, new()
102
    {
103
        if (services is null)
×
104
            throw new System.ArgumentNullException(nameof(services));
×
105

106
        services.AddTransient<IPipelineBehavior<EntityIdentifierQuery<string, TReadModel>, TReadModel>, DistributedCacheQueryBehavior<EntityIdentifierQuery<string, TReadModel>, TReadModel>>();
×
107
        services.AddTransient<IPipelineBehavior<EntityIdentifiersQuery<string, TReadModel>, IReadOnlyCollection<TReadModel>>, DistributedCacheQueryBehavior<EntityIdentifiersQuery<string, TReadModel>, IReadOnlyCollection<TReadModel>>>();
×
108
        services.AddTransient<IPipelineBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>, DistributedCacheQueryBehavior<EntityPagedQuery<TReadModel>, EntityPagedResult<TReadModel>>>();
×
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, TReadModel, TCreateModel, TUpdateModel>(this IServiceCollection services)
116
        where TRepository : ICosmosRepository<TEntity>
117
        where TEntity : class, IHaveIdentifier<string>, new()
118
        where TCreateModel : class
119
        where TUpdateModel : class
120
    {
121
        if (services is null)
×
122
            throw new System.ArgumentNullException(nameof(services));
×
123

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

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

133
        return services;
×
134
    }
135

136

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

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

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

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

161
        services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TCreateModel, TReadModel>>();
×
162
        services.AddTransient<IPipelineBehavior<EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<string, TCreateModel, TReadModel>>();
×
163

164
        return services;
×
165
    }
166

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

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

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

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

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

195
        services.AddTransient<IPipelineBehavior<EntityUpdateCommand<string, TUpdateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TUpdateModel, TReadModel>>();
×
196
        services.AddTransient<IPipelineBehavior<EntityUpdateCommand<string, TUpdateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<string, TUpdateModel, TReadModel>>();
×
197

198
        return services;
×
199
    }
200

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

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

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

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

225
        services.AddTransient<IPipelineBehavior<EntityUpsertCommand<string, TUpdateModel, TReadModel>, TReadModel>, ValidateEntityModelCommandBehavior<TUpdateModel, TReadModel>>();
×
226
        services.AddTransient<IPipelineBehavior<EntityUpsertCommand<string, TUpdateModel, TReadModel>, TReadModel>, EntityChangeNotificationBehavior<string, TUpdateModel, TReadModel>>();
×
227

228
        return services;
×
229
    }
230

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

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

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

244
        return services;
×
245
    }
246

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

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

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

260
        return services;
×
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