Skip to content

Commit

Permalink
chore(*): introduce I prefix for each interface
Browse files Browse the repository at this point in the history
closes #135
  • Loading branch information
ostridm committed Dec 14, 2022
1 parent 8172bc6 commit b042dbe
Show file tree
Hide file tree
Showing 93 changed files with 229 additions and 229 deletions.
6 changes: 3 additions & 3 deletions src/SecTester.Bus/Dispatchers/DefaultRmqConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

namespace SecTester.Bus.Dispatchers;

public class DefaultRmqConnectionManager : RmqConnectionManager
public class DefaultRmqConnectionManager : IRmqConnectionManager
{
private readonly IConnectionFactory _connectionFactory;
private readonly ILogger _logger;
private readonly RetryStrategy _retryStrategy;
private readonly IRetryStrategy _retryStrategy;
private readonly object _sync = new();
private IConnection? _connection;

public DefaultRmqConnectionManager(IConnectionFactory connectionFactory, ILogger<DefaultRmqConnectionManager> logger, RetryStrategy retryStrategy)
public DefaultRmqConnectionManager(IConnectionFactory connectionFactory, ILogger<DefaultRmqConnectionManager> logger, IRetryStrategy retryStrategy)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
Expand Down
8 changes: 4 additions & 4 deletions src/SecTester.Bus/Dispatchers/DefaultRmqEventBusFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
namespace SecTester.Bus.Dispatchers;

[ExcludeFromCodeCoverage]
public class DefaultRmqEventBusFactory : RmqEventBusFactory
public class DefaultRmqEventBusFactory : IRmqEventBusFactory
{
private readonly RetryStrategy _retryStrategy;
private readonly IRetryStrategy _retryStrategy;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILoggerFactory _loggerFactory;

public DefaultRmqEventBusFactory(IServiceScopeFactory serviceScopeFactory, RetryStrategy retryStrategy,
public DefaultRmqEventBusFactory(IServiceScopeFactory serviceScopeFactory, IRetryStrategy retryStrategy,
ILoggerFactory loggerFactory)
{
_serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
Expand All @@ -28,7 +28,7 @@ public RmqEventBus CreateEventBus(RmqEventBusOptions options)
return new RmqEventBus(options, connectionManager, _loggerFactory.CreateLogger<RmqEventBus>(), _serviceScopeFactory);
}

protected virtual RmqConnectionManager CreateConnectionManager(RmqEventBusOptions options)
protected virtual IRmqConnectionManager CreateConnectionManager(RmqEventBusOptions options)
{
var factory = new ConnectionFactory
{
Expand Down
6 changes: 3 additions & 3 deletions src/SecTester.Bus/Dispatchers/HttpCommandDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace SecTester.Bus.Dispatchers;

public class HttpCommandDispatcher : CommandDispatcher
public class HttpCommandDispatcher : ICommandDispatcher
{
private const string AuthScheme = "api-key";
private const string CorrelationIdHeaderField = "x-correlation-id";
Expand All @@ -26,9 +26,9 @@ public class HttpCommandDispatcher : CommandDispatcher
HttpMethod.Head, HttpMethod.Options, HttpMethod.Get, HttpMethod.Trace
};

private readonly RetryStrategy _retryStrategy;
private readonly IRetryStrategy _retryStrategy;

public HttpCommandDispatcher(IHttpClientFactory httpClientFactory, HttpCommandDispatcherConfig config, RetryStrategy retryStrategy)
public HttpCommandDispatcher(IHttpClientFactory httpClientFactory, HttpCommandDispatcherConfig config, IRetryStrategy retryStrategy)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SecTester.Bus.Dispatchers;

public interface RmqConnectionManager : IDisposable
public interface IRmqConnectionManager : IDisposable
{
bool IsConnected { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SecTester.Bus.Dispatchers;

public interface RmqEventBusFactory
public interface IRmqEventBusFactory
{
RmqEventBus CreateEventBus(RmqEventBusOptions options);
}
16 changes: 8 additions & 8 deletions src/SecTester.Bus/Dispatchers/RmqEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

namespace SecTester.Bus.Dispatchers;

public class RmqEventBus : EventBus
public class RmqEventBus : IEventBus
{
private const string ReplyQueueName = "amq.rabbitmq.reply-to";

private readonly RmqConnectionManager _connectionManager;
private readonly IRmqConnectionManager _connectionManager;
private readonly List<Type> _eventTypes = new();
private readonly Dictionary<string, List<Type>> _handlers = new();
private readonly ILogger _logger;
Expand All @@ -32,7 +32,7 @@ public class RmqEventBus : EventBus
private readonly IServiceScopeFactory _scopeFactory;
private IModel _channel;

public RmqEventBus(RmqEventBusOptions options, RmqConnectionManager connectionManager, ILogger<RmqEventBus> logger,
public RmqEventBus(RmqEventBusOptions options, IRmqConnectionManager connectionManager, ILogger<RmqEventBus> logger,
IServiceScopeFactory scopeFactory)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
Expand Down Expand Up @@ -86,7 +86,7 @@ public Task Publish<TEvent>(TEvent message) where TEvent : Event
return MessageSerializer.Deserialize<TResult>(result);
}

public void Register<THandler, TEvent, TResult>() where THandler : EventListener<TEvent, TResult> where TEvent : Event
public void Register<THandler, TEvent, TResult>() where THandler : IEventListener<TEvent, TResult> where TEvent : Event
{
var eventName = MessageUtils.GetMessageType<TEvent>();
var handlerType = typeof(THandler);
Expand All @@ -102,7 +102,7 @@ public void Register<THandler, TEvent, TResult>() where THandler : EventListener
_handlers[eventName].Add(handlerType);
}

public void Unregister<THandler, TEvent, TResult>() where THandler : EventListener<TEvent, TResult> where TEvent : Event
public void Unregister<THandler, TEvent, TResult>() where THandler : IEventListener<TEvent, TResult> where TEvent : Event
{
var eventName = MessageUtils.GetMessageType<TEvent>();
var handlerType = typeof(THandler);
Expand All @@ -123,10 +123,10 @@ public void Unregister<THandler, TEvent, TResult>() where THandler : EventListen
}
}

public void Register<THandler, TEvent>() where THandler : EventListener<TEvent> where TEvent : Event =>
public void Register<THandler, TEvent>() where THandler : IEventListener<TEvent> where TEvent : Event =>
Register<THandler, TEvent, Unit>();

public void Unregister<THandler, TEvent>() where THandler : EventListener<TEvent> where TEvent : Event =>
public void Unregister<THandler, TEvent>() where THandler : IEventListener<TEvent> where TEvent : Event =>
Unregister<THandler, TEvent, Unit>();

public void Dispose()
Expand Down Expand Up @@ -253,7 +253,7 @@ private async Task HandleEvent(Type eventHandler, ConsumedMessage consumedMessag

var concreteType = eventHandler.GetConcreteEventListenerType();
var payload = MessageSerializer.Deserialize(consumedMessage.Payload!, eventType);
var task = (Task)concreteType.InvokeMember(nameof(EventListener<Event>.Handle), BindingFlags.InvokeMethod, null, instance, new[]
var task = (Task)concreteType.InvokeMember(nameof(IEventListener<Event>.Handle), BindingFlags.InvokeMethod, null, instance, new[]
{
payload
}, CultureInfo.InvariantCulture);
Expand Down
4 changes: 2 additions & 2 deletions src/SecTester.Bus/Extensions/GenericTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class GenericTypeExtensions
public static Type GetConcreteEventListenerType(this Type type)
{
var (input, output) = GetEventListenerGenericTypes(type);
var interfaceType = typeof(EventListener<,>);
var interfaceType = typeof(IEventListener<,>);

return interfaceType.MakeGenericType(input, output);
}
Expand All @@ -33,6 +33,6 @@ private static bool IsEventListenerType(Type it)

private static Type GetEventListenerType()
{
return typeof(EventListener<,>);
return typeof(IEventListener<,>);
}
}
6 changes: 3 additions & 3 deletions src/SecTester.Bus/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static IServiceCollection AddSecTesterBus(this IServiceCollection collect
collection
.AddHttpCommandDispatcher()
.AddSingleton(new ExponentialBackoffOptions())
.AddSingleton<RetryStrategy, ExponentialBackoffRetryStrategy>()
.AddSingleton<RmqEventBusFactory, DefaultRmqEventBusFactory>();
.AddSingleton<IRetryStrategy, ExponentialBackoffIRetryStrategy>()
.AddSingleton<IRmqEventBusFactory, DefaultRmqEventBusFactory>();

private static IServiceCollection AddHttpCommandDispatcher(this IServiceCollection collection) =>
collection
Expand All @@ -26,7 +26,7 @@ private static IServiceCollection AddHttpCommandDispatcher(this IServiceCollecti
return new HttpCommandDispatcherConfig(config.Api, config.Credentials!.Token, TimeSpan.FromSeconds(10));
})
.AddScoped<HttpCommandDispatcher>()
.AddScoped<CommandDispatcher>(sp => sp.GetRequiredService<HttpCommandDispatcher>())
.AddScoped<ICommandDispatcher>(sp => sp.GetRequiredService<HttpCommandDispatcher>())
.AddHttpClientForHttpCommandDispatcher();

private static IServiceCollection AddHttpClientForHttpCommandDispatcher(this IServiceCollection collection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

namespace SecTester.Bus.RetryStrategies;

public class ExponentialBackoffRetryStrategy : RetryStrategy
public class ExponentialBackoffIRetryStrategy : IRetryStrategy
{
private readonly ExponentialBackoffOptions _options;

public ExponentialBackoffRetryStrategy(ExponentialBackoffOptions options)
public ExponentialBackoffIRetryStrategy(ExponentialBackoffOptions options)
{
_options = options;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SecTester.Core/Bus/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Command(string type, string correlationId, DateTime createdAt, bool expec
Ttl = ttl;
}

public Task<TResponse?> Execute(CommandDispatcher dispatcher)
public Task<TResponse?> Execute(ICommandDispatcher dispatcher)
{
return dispatcher.Execute(this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SecTester.Core/Bus/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SecTester.Core.Bus;

public record Event : Message
{
public Task Publish(EventDispatcher dispatcher)
public Task Publish(IEventDispatcher dispatcher)
{
return dispatcher.Publish(this);
}
Expand Down
11 changes: 0 additions & 11 deletions src/SecTester.Core/Bus/EventListener.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SecTester.Core.Bus;

public interface CommandDispatcher
public interface ICommandDispatcher
{
Task<TResult?> Execute<TResult>(Command<TResult> message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace SecTester.Core.Bus;

public interface EventBus : EventDispatcher, CommandDispatcher, IDisposable
public interface IEventBus : IEventDispatcher, ICommandDispatcher, IDisposable
{
void Register<THandler, TEvent>()
where THandler : EventListener<TEvent>
where THandler : IEventListener<TEvent>
where TEvent : Event;

void Register<THandler, TEvent, TResult>()
where THandler : EventListener<TEvent, TResult>
where THandler : IEventListener<TEvent, TResult>
where TEvent : Event;

void Unregister<THandler, TEvent>()
where THandler : EventListener<TEvent>
where THandler : IEventListener<TEvent>
where TEvent : Event;

void Unregister<THandler, TEvent, TResult>()
where THandler : EventListener<TEvent, TResult>
where THandler : IEventListener<TEvent, TResult>
where TEvent : Event;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SecTester.Core.Bus;

public interface EventDispatcher
public interface IEventDispatcher
{
Task Publish<TEvent>(TEvent message) where TEvent : Event;
}
11 changes: 11 additions & 0 deletions src/SecTester.Core/Bus/IEventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace SecTester.Core.Bus;

public interface IEventListener<in TEvent, TResult> where TEvent : Event
{
Task<TResult> Handle(TEvent message);
}

public interface IEventListener<in TEvent> : IEventListener<TEvent, Unit> where TEvent : Event
{ }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SecTester.Core.Bus;

public interface RetryStrategy
public interface IRetryStrategy
{
Task<TResult> Acquire<TResult>(Func<Task<TResult>> task, CancellationToken cancellationToken = default);
}
8 changes: 4 additions & 4 deletions src/SecTester.Core/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Configuration
private readonly Regex _schemaRegex = new(@"^.+:\/\/");
private readonly Regex _hostnameNormalizationRegex = new(@"^(?!(?:\w+:)?\/\/)|^\/\/");
private readonly string[] _loopbackAddresses = { "localhost", "127.0.0.1" };
private readonly List<CredentialProvider> _credentialProviders;
private readonly List<ICredentialProvider> _credentialProviders;

public string Bus { get; private set; }

Expand All @@ -24,17 +24,17 @@ public class Configuration

public LogLevel LogLevel { get; private set; }

public IReadOnlyCollection<CredentialProvider> CredentialProviders => _credentialProviders.AsReadOnly();
public IReadOnlyCollection<ICredentialProvider> CredentialProviders => _credentialProviders.AsReadOnly();

// TODO: provide a more convenient way of setting these properties
public string Name { get; } = "sectester-net";
public string Version { get; } = "0.0.1";
public string RepeaterVersion { get; } = "9.0.0";

public Configuration(string? hostname, Credentials? credentials = null, List<CredentialProvider>? credentialProviders = null, LogLevel logLevel = LogLevel.Error)
public Configuration(string? hostname, Credentials? credentials = null, List<ICredentialProvider>? credentialProviders = null, LogLevel logLevel = LogLevel.Error)
{
LogLevel = logLevel;
credentialProviders ??= new List<CredentialProvider> { new EnvCredentialProvider() };
credentialProviders ??= new List<ICredentialProvider> { new EnvCredentialProvider() };
hostname = hostname?.Trim();
hostname = hostname ?? throw new ArgumentNullException(nameof(hostname), "Please provide 'hostname' option.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SecTester.Core.CredentialProviders;

public class EnvCredentialProvider : CredentialProvider
public class EnvCredentialProvider : ICredentialProvider
{
public const string BrightToken = "BRIGHT_TOKEN";

Expand Down
4 changes: 2 additions & 2 deletions src/SecTester.Core/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static IServiceCollection AddSecTesterConfig(this IServiceCollection coll
public static IServiceCollection AddSecTesterConfig(this IServiceCollection collection, Configuration configuration) =>
collection
.AddSingleton(configuration)
.AddSingleton<SystemTimeProvider>(new UtcSystemTimeProvider())
.AddSingleton<AnsiCodeColorizer>(new DefaultAnsiCodeColorizer(ConsoleUtils.IsColored))
.AddSingleton<ISystemTimeProvider>(new UtcSystemTimeProvider())
.AddSingleton<IAnsiCodeColorizer>(new DefaultAnsiCodeColorizer(ConsoleUtils.IsColored))
.AddLogging(builder =>
{
builder.SetMinimumLevel(configuration.LogLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SecTester.Core;

public interface CredentialProvider
public interface ICredentialProvider
{
Task<Credentials?> Get();
}
6 changes: 3 additions & 3 deletions src/SecTester.Core/Logger/ColoredConsoleFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace SecTester.Core.Logger;

public class ColoredConsoleFormatter : DefaultConsoleFormatter
{
private readonly AnsiCodeColorizer _ansiCodeColorizer;
private readonly IAnsiCodeColorizer _ansiCodeColorizer;

public ColoredConsoleFormatter(IOptionsMonitor<ConsoleFormatterOptions> options, SystemTimeProvider systemTimeProvider,
AnsiCodeColorizer ansiCodeColorizer)
public ColoredConsoleFormatter(IOptionsMonitor<ConsoleFormatterOptions> options, ISystemTimeProvider systemTimeProvider,
IAnsiCodeColorizer ansiCodeColorizer)
: base(nameof(ColoredConsoleFormatter), options, systemTimeProvider)
{
_ansiCodeColorizer = ansiCodeColorizer;
Expand Down
2 changes: 1 addition & 1 deletion src/SecTester.Core/Logger/DefaultAnsiCodeColorizer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SecTester.Core.Logger;

public class DefaultAnsiCodeColorizer : AnsiCodeColorizer
public class DefaultAnsiCodeColorizer : IAnsiCodeColorizer
{
private readonly bool _enabled;

Expand Down
6 changes: 3 additions & 3 deletions src/SecTester.Core/Logger/DefaultConsoleFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public class DefaultConsoleFormatter : ConsoleFormatter, IDisposable
private readonly IDisposable _optionsReloadToken;
private ConsoleFormatterOptions _formatterOptions;

private readonly SystemTimeProvider _systemTimeProvider;
private readonly ISystemTimeProvider _systemTimeProvider;

public DefaultConsoleFormatter(IOptionsMonitor<ConsoleFormatterOptions> options,
SystemTimeProvider systemTimeProvider)
ISystemTimeProvider systemTimeProvider)
: this(nameof(DefaultConsoleFormatter), options, systemTimeProvider)
{
}

public DefaultConsoleFormatter(string name, IOptionsMonitor<ConsoleFormatterOptions> options, SystemTimeProvider systemTimeProvider)
public DefaultConsoleFormatter(string name, IOptionsMonitor<ConsoleFormatterOptions> options, ISystemTimeProvider systemTimeProvider)
: base(name)
{
_optionsReloadToken = options.OnChange(ReloadLoggerOptions);
Expand Down
Loading

0 comments on commit b042dbe

Please sign in to comment.