Skip to content

Commit ca57cad

Browse files
author
Mehdi Yousefi
committed
remove used code
1 parent 086f3b3 commit ca57cad

File tree

5 files changed

+12
-93
lines changed

5 files changed

+12
-93
lines changed

CQRS/Gufel.Dispatcher.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageId>Gufel.Dispatcher</PackageId>
1010
<Title>Gufel Dispatcher (CQRS And Mediator)</Title>
1111
<Authors>mahdiit</Authors>
12-
<Version>1.0.3</Version>
12+
<Version>1.0.4</Version>
1313
<Description>CQRS And Mediator with pipeline handler and message publisher</Description>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<RepositoryUrl>https://github.com/mahdiit/Gufel.CQRS</RepositoryUrl>
@@ -34,8 +34,6 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37-
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
38-
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
3937
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
4038
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.4" />
4139
</ItemGroup>

CQRS/Implement/Dispatcher.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,35 @@
11
using Gufel.Dispatcher.Base.Dispatcher;
2-
using Microsoft.AspNetCore.Http;
32
using Microsoft.Extensions.DependencyInjection;
43

54
namespace Gufel.Dispatcher.Implement
65
{
7-
public class Dispatcher : IDispatcher, IDisposable
6+
public sealed class Dispatcher(IServiceProvider serviceProvider) : IDispatcher
87
{
9-
private readonly IServiceScope? _serviceScope;
10-
private readonly IServiceProvider _serviceProvider;
11-
12-
public Dispatcher(
13-
IServiceScopeFactory serviceScope,
14-
IHttpContextAccessor httpContextAccessor)
15-
{
16-
if (httpContextAccessor.HttpContext != null)
17-
{
18-
_serviceProvider = httpContextAccessor.HttpContext.RequestServices;
19-
}
20-
else
21-
{
22-
_serviceScope = serviceScope.CreateScope();
23-
_serviceProvider = _serviceScope.ServiceProvider;
24-
}
25-
}
26-
278
public async Task Dispatch<TRequest>(TRequest request, CancellationToken cancellation)
289
where TRequest: IRequest
2910
{
30-
var pipeLines = _serviceProvider.GetServices<IPipelineHandler<TRequest>>();
11+
var pipeLines = serviceProvider.GetServices<IPipelineHandler<TRequest>>();
3112
foreach (var pipeline in pipeLines)
3213
{
3314
await pipeline.Handle(request, cancellation);
3415
}
3516

36-
var handler = _serviceProvider.GetRequiredService<IRequestHandler<TRequest>>();
17+
var handler = serviceProvider.GetRequiredService<IRequestHandler<TRequest>>();
3718
await handler.Handle(request, cancellation);
3819
}
3920

4021
public async Task<TResponse> Dispatch<TRequest, TResponse>(TRequest request, CancellationToken cancellation)
4122
where TRequest : IRequest<TResponse>
4223
where TResponse : IResponse
4324
{
44-
var pipeLines = _serviceProvider.GetServices<IPipelineHandler<TRequest, TResponse>>();
25+
var pipeLines = serviceProvider.GetServices<IPipelineHandler<TRequest, TResponse>>();
4526
foreach (var pipeline in pipeLines)
4627
{
4728
await pipeline.Handle(request, cancellation);
4829
}
4930

50-
var handler = _serviceProvider.GetRequiredService<IRequestHandler<TRequest, TResponse>>();
31+
var handler = serviceProvider.GetRequiredService<IRequestHandler<TRequest, TResponse>>();
5132
return await handler.Handle(request, cancellation);
5233
}
53-
54-
protected virtual void Dispose(bool disposing)
55-
{
56-
_serviceScope?.Dispose();
57-
}
58-
59-
public void Dispose()
60-
{
61-
Dispose(true);
62-
GC.SuppressFinalize(this);
63-
}
6434
}
6535
}

CQRS/Implement/MessagePublisher.cs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,16 @@
1-
using System.Collections;
2-
using System.Collections.Concurrent;
3-
using Gufel.Dispatcher.Base.MessagePublisher;
4-
using Microsoft.AspNetCore.Http;
1+
using Gufel.Dispatcher.Base.MessagePublisher;
52
using Microsoft.Extensions.DependencyInjection;
6-
using Microsoft.Extensions.Logging;
73

84
namespace Gufel.Dispatcher.Implement
95
{
10-
public class MessagePublisher : IMessagePublisher, IDisposable
6+
public sealed class MessagePublisher(IServiceProvider serviceProvider,
7+
IMessagePublishStrategy strategy) : IMessagePublisher
118
{
12-
private readonly IServiceScope? _serviceScope;
13-
private readonly IServiceProvider _serviceProvider;
14-
private readonly IMessagePublishStrategy _strategy;
15-
16-
public MessagePublisher(
17-
IServiceScopeFactory serviceScope,
18-
IHttpContextAccessor httpContextAccessor,
19-
IMessagePublishStrategy strategy)
20-
{
21-
_strategy = strategy;
22-
if (httpContextAccessor.HttpContext != null)
23-
{
24-
_serviceProvider = httpContextAccessor.HttpContext.RequestServices;
25-
}
26-
else
27-
{
28-
_serviceScope = serviceScope.CreateScope();
29-
_serviceProvider = _serviceScope.ServiceProvider;
30-
}
31-
}
32-
339
public void Publish<T>(string topic, T value)
3410
{
35-
var subscribers = _serviceProvider.GetServices<ISubscribeHandler<T>>()
11+
var subscribers = serviceProvider.GetServices<ISubscribeHandler<T>>()
3612
.Where(x => x.Topic == topic);
37-
_strategy.SendMessage(subscribers, value);
38-
}
39-
40-
protected virtual void Dispose(bool disposing)
41-
{
42-
_serviceScope?.Dispose();
43-
}
44-
45-
public void Dispose()
46-
{
47-
Dispose(true);
48-
GC.SuppressFinalize(this);
13+
strategy.SendMessage(subscribers, value);
4914
}
5015
}
5116
}

CQRS/Implement/RegisterDispatcherHelper.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Reflection;
22
using Gufel.Dispatcher.Base.Dispatcher;
33
using Gufel.Dispatcher.Base.MessagePublisher;
4-
using Microsoft.AspNetCore.Http;
54
using Microsoft.Extensions.DependencyInjection;
65

76
namespace Gufel.Dispatcher.Implement
@@ -24,18 +23,8 @@ private static void RegisterTypeImplement(IServiceCollection services, Assembly
2423
});
2524
}
2625

27-
private static void AddHttpContextAccessor(IServiceCollection services)
28-
{
29-
if (services.All(sd => sd.ServiceType != typeof(IHttpContextAccessor)))
30-
{
31-
services.AddHttpContextAccessor();
32-
}
33-
}
34-
3526
public static void AddDispatcher(this IServiceCollection services, Assembly assembly)
3627
{
37-
AddHttpContextAccessor(services);
38-
3928
RegisterTypeImplement(services, assembly,
4029
typeof(IPipelineHandler<,>),
4130
typeof(IRequestHandler<,>),
@@ -47,8 +36,6 @@ public static void AddDispatcher(this IServiceCollection services, Assembly asse
4736

4837
public static void AddMessagePublisher(this IServiceCollection services, IMessagePublishStrategy? strategy = null)
4938
{
50-
AddHttpContextAccessor(services);
51-
5239
services.AddSingleton(x => strategy ?? new ParallelMessagePublishStrategy());
5340

5441
services.AddScoped<IMessagePublisher, MessagePublisher>();

UnitTest/DispatcherTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public class DispatcherTests
6060

6161
public DispatcherTests()
6262
{
63-
var services = new ServiceCollection()
64-
.AddHttpContextAccessor();
63+
var services = new ServiceCollection();
6564

6665
_pipelineHandler = new TestPipelineHandler();
6766
_pipelineHandlerWithResponse = new TestPipelineHandlerWithResponse();

0 commit comments

Comments
 (0)