Skip to content

Commit

Permalink
fixed module implementation with interface segregation
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Nov 6, 2023
1 parent ec079a6 commit 7ff0169
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 27 deletions.
2 changes: 1 addition & 1 deletion _src/Implementation/Logger/Commands/ClearLogCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Implementation.Logger.Commands
{
public class ClearLogCommand : ICommand<bool>
internal class ClearLogCommand : ICommand<bool>
{
private readonly string _path;

Expand Down
4 changes: 2 additions & 2 deletions _src/Implementation/Logger/Commands/LogCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Implementation.Logger.Commands
{
public class LogCommand : ICommand<string>
internal class LogCommand : ICommand<string>
{
private readonly string _filePath;
private readonly string _content;
Expand All @@ -29,7 +29,7 @@ public async Task<string> Execute()
throw new FileNotFoundException("Log file does not exists!");
}

using (var streamWriter = new StreamWriter(_filePath, true, Encoding.ASCII))
await using (var streamWriter = new StreamWriter(_filePath, true, Encoding.ASCII))
{
await streamWriter.WriteAsync(_content);
}
Expand Down
45 changes: 45 additions & 0 deletions _src/Implementation/Module/AModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Infrastructure.Module;
using Microsoft.Extensions.DependencyInjection;

namespace Implementation.Module
{
/// <inheritdoc />
public abstract class AModule : IModule
{
protected IServiceCollection Collection;

/// <inheritdoc cref="IModule.IsRegistered"/>
public bool IsRegistered { get; } = false;

protected AModule(IServiceCollection collection)
{
Collection = collection ?? throw new ArgumentNullException(nameof(collection));
}

public virtual IServiceCollection RegisterServices()
{
if (IsRegistered)
{
return Collection;
}

RegisterServices(Collection);

return Collection;
}

public abstract IModule RegisterServices(IServiceCollection collection);

public virtual IModule RegisterOtherServices(IBaseModule module)
{
if (module.IsRegistered)
{
return this;
}

module.RegisterServices(Collection);
return this;
}
}
}
28 changes: 17 additions & 11 deletions _src/Implementation/Module/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Implementation.IO;
using Implementation.Navigator.Factories;
using Implementation.Repositories.Factories;
using Implementation.Time;
using Implementation.Time.Factories;
using Infrastructure.Application;
using Infrastructure.Configuration.Factories;
Expand All @@ -20,20 +21,28 @@

namespace Implementation.Module
{
public class CoreModule : IGeneralModule, ICancellableModule
public class CoreModule : AModule, IGeneralModule, ICancellableModule
{
public CancellationTokenSource Source { get; }

public CoreModule()
public CoreModule(IServiceCollection serviceCollection, CancellationTokenSource source) : base(serviceCollection)
{

Source = source ?? throw new ArgumentNullException(nameof(source));
}

public void RegisterServices(IServiceCollection collection, string projectNamespace)
public void RegisterServices(string projectNamespace)
{
var tokenSource = new CancellationTokenSource();
var mainFolder = Path.Join("joshika39", projectNamespace);
var userFolder = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), mainFolder);
Collection.AddTransient<IApplicationSettings, GeneralApplicationSettings>(provider =>
new GeneralApplicationSettings(userFolder, provider.GetRequiredService<IConfigurationQueryFactory>())
);

RegisterServices();
}

public override IModule RegisterServices(IServiceCollection collection)
{
collection.AddScoped<ILogger, Logger.Logger>(_ => new Logger.Logger(Guid.NewGuid()));
collection.AddTransient<INavigatorFactory, NavigatorFactory>();
collection.AddTransient<INavigatorElementFactory, NavigatorElementFactory>();
Expand All @@ -43,13 +52,10 @@ public void RegisterServices(IServiceCollection collection, string projectNamesp
collection.AddScoped<IDataParser, DefaultDataParser>();
collection.AddTransient<IRepositoryFactory, RepositoryFactory>();

collection.AddScoped<ILifeCycleManager>(_ => new LifeCycleManager(tokenSource));
collection.AddScoped<ILifeCycleManager>(_ => new LifeCycleManager(Source));
collection.AddSingleton<IStopWatchFactory, StopwatchFactory>();
collection.AddScoped<IStopwatch>(_ => new DefaultStopwatch(tokenSource.Token));

collection.AddTransient<IApplicationSettings, GeneralApplicationSettings>(provider =>
new GeneralApplicationSettings(userFolder, provider.GetRequiredService<IConfigurationQueryFactory>())
);
collection.AddScoped<IStopwatch>(_ => new DefaultStopwatch(Source.Token));
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Implementation.Navigator.Factories
{
public class NavigatorElementFactory : INavigatorElementFactory
internal class NavigatorElementFactory : INavigatorElementFactory
{

public INavigatorElement<T> CreateNavigatorElement<T>(string displayText, T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Implementation.Repositories.Factories
{
public class RepositoryFactory : IRepositoryFactory
internal class RepositoryFactory : IRepositoryFactory
{
private readonly IApplicationSettings _applicationSettings;

Expand Down
3 changes: 1 addition & 2 deletions _src/Implementation/Time/DefaultStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Implementation.Time;
using Infrastructure.Time;
using Infrastructure.Time.Listeners;

namespace GameFramework.Impl.Time
namespace Implementation.Time
{
internal sealed class DefaultStopwatch : IStopwatch
{
Expand Down
1 change: 0 additions & 1 deletion _src/Implementation/Time/Factories/StopwatchFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading;
using GameFramework.Impl.Time;
using Infrastructure.Time;
using Infrastructure.Time.Factories;

Expand Down
18 changes: 18 additions & 0 deletions _src/Infrastructure/Module/IBaseModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.DependencyInjection;

namespace Infrastructure.Module
{
public interface IBaseModule
{
/// <summary>
/// Shows whether the modules is already registered or not.
/// </summary>
bool IsRegistered { get; }

/// <summary>
/// Registers the services into the specified collection.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/> created in the composition root.</param>
IModule RegisterServices(IServiceCollection collection);
}
}
3 changes: 1 addition & 2 deletions _src/Infrastructure/Module/IGeneralModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public interface IGeneralModule
/// <summary>
/// Registers the services.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/> created in the composition root.</param>
/// <param name="projectNamespace">The name of the project which uses this module</param>
void RegisterServices(IServiceCollection collection, string projectNamespace);
void RegisterServices(string projectNamespace);
}
}
10 changes: 8 additions & 2 deletions _src/Infrastructure/Module/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public interface IModule
/// <summary>
/// Registers the services.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/> created in the composition root.</param>
void RegisterServices(IServiceCollection collection);
/// <returns>Returns self after registered the services</returns>
IServiceCollection RegisterServices();

/// <summary>
/// Registers the services of a different module.
/// </summary>
/// <param name="module">Other module</param>
IModule RegisterOtherServices(IBaseModule module);
}
}
3 changes: 2 additions & 1 deletion _src/_Tests/ImplementationTest/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Implementation.IO;
using Implementation.Module;
using Infrastructure.IO;
Expand All @@ -17,7 +18,7 @@ public class ReaderTests
public ReaderTests()
{
var collection = new ServiceCollection();
new CoreModule().RegisterServices(collection, "reader-tests");
new CoreModule(collection, new CancellationTokenSource()).RegisterServices("reader-tests");
_provider = collection.BuildServiceProvider();
}

Expand Down
2 changes: 1 addition & 1 deletion _src/_Tests/ImplementationTest/Time/StopwatchTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using GameFramework.Impl.Time;
using Implementation.Time;
using Infrastructure.Time;
using Infrastructure.Time.Listeners;
using Moq;
Expand Down
3 changes: 2 additions & 1 deletion _src/_Tests/ManualTests/Core.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using Implementation.Module;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -9,7 +10,7 @@ internal class Core
public IServiceProvider LoadModules()
{
var collection = new ServiceCollection();
new CoreModule().RegisterServices(collection, "ManualTests");
new CoreModule(collection, new CancellationTokenSource()).RegisterServices("ManualTests");
return collection.BuildServiceProvider();
}
}
Expand Down
1 change: 0 additions & 1 deletion _src/_Tests/ManualTests/Dump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ private void Dumper()
Console.ReadLine();
var provider = new Core().LoadModules();
var elemFact = provider.GetService<INavigatorElementFactory>();
// var test = _reader.ReadAllLines("Adjon meg hosszu szoveget");
var elements = new List<INavigatorElement<string>>()
{
elemFact.CreateNavigatorElement("asd", "asdValue"),
Expand Down

0 comments on commit 7ff0169

Please sign in to comment.