Skip to content

Commit

Permalink
Merge pull request #68 from r-koubou/refactor/usecase_dump
Browse files Browse the repository at this point in the history
Refactor/usecase dump
  • Loading branch information
r-koubou authored Nov 29, 2023
2 parents 552fa03 + aec5b9b commit e9e4aef
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;

using KeySwitchManager.Interactors.KeySwitches;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Create;
using KeySwitchManager.UseCase.KeySwitches.Export;

Expand All @@ -11,11 +10,11 @@ namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Create
public class CreateFileController : IController
{
private IExportStrategy Strategy { get; }
private IOutputPort<CreateOutputData> Presenter { get; }
private ICreatePresenter Presenter { get; }

public CreateFileController(
IExportStrategy strategy,
IOutputPort<CreateOutputData> presenter )
ICreatePresenter presenter )
{
Strategy = strategy;
Presenter = presenter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
using KeySwitchManager.Infrastructures.Storage.KeySwitches;
using KeySwitchManager.Infrastructures.Storage.Spreadsheet.ClosedXml.KeySwitches.Export;
using KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches.Export;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Create;
using KeySwitchManager.UseCase.KeySwitches.Export;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Create
{
public interface ICreateControllerFactory
{
IController Create( string outputFilePath, IOutputPort<CreateOutputData> presenter );
IController Create( string outputFilePath, ICreatePresenter presenter );
}

public class CreateFileControllerFactory : ICreateControllerFactory
{
IController ICreateControllerFactory.Create( string outputFilePath, IOutputPort<CreateOutputData> presenter )
IController ICreateControllerFactory.Create( string outputFilePath, ICreatePresenter presenter )
{
var outputDirectory = new DirectoryPath( Path.GetDirectoryName( outputFilePath ) ?? string.Empty );
var fileName = outputFilePath.ToLower();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.Interactors.KeySwitches;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

using RkHelper.System;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Dump
{
public class DumpFileController : IController
public class DumpController : IController
{
private IKeySwitchRepository SourceRepository { get; }
private IExportStrategy Strategy { get; }
private IDumpFilePresenter Presenter { get; }
private IDumpPresenter Presenter { get; }

public DumpFileController(
public DumpController(
IKeySwitchRepository sourceRepository,
IExportStrategy strategy,
IDumpFilePresenter presenter )
IDumpPresenter presenter )
{
SourceRepository = sourceRepository;
Strategy = strategy;
Expand All @@ -33,14 +34,13 @@ public void Dispose()

public async Task ExecuteAsync( CancellationToken cancellationToken )
{
IDumpFileUseCase interactor = new DumpFileInteractor(
IDumpUseCase interactor = new DumpInteractor(
SourceRepository,
Strategy,
Presenter
);

var response = await interactor.ExecuteAsync( new DumpFileRequest(), cancellationToken );
Presenter.Complete( response );
await interactor.HandleAsync( UnitInputData.Default, cancellationToken );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.IO;

using KeySwitchManager.Applications.Standalone.Core.Helpers;
using KeySwitchManager.Applications.Standalone.Core.Presenters;
using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.Commons.Data;
using KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches.Export;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Dump
Expand All @@ -20,9 +20,9 @@ IController IDumpControllerFactory.Create( string databasePath, string outputFil
var contentWriterFactory = new YamlExportContentFileWriterFactory( outputDirectory );
var strategy = new SingleExportStrategy( contentWriterFactory, contentFactory );

var presenter = new IDumpFilePresenter.Console();
var presenter = new DumpPresenter( logTextView );

return new DumpFileController(
return new DumpController(
database,
strategy,
presenter
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.UseCase.KeySwitches.Delete;
using KeySwitchManager.UseCase.KeySwitches.Dump;

namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public sealed class DumpPresenter : IDumpPresenter
{
public static readonly IDeletePresenter Null = new NullImpl();

private ILogTextView TextView { get; }

public DumpPresenter( ILogTextView textView )
{
TextView = textView;
}

public async Task HandleAsync( DumpOutputData outputData, CancellationToken cancellationToken = default )
{
TextView.Append( $"{outputData.Value.DumpDataCount} record(s) dumped" );
await Task.CompletedTask;
}

#region NullObject
private class NullImpl : IDeletePresenter
{
public async Task HandleDeleteBeginAsync( DeleteInputData inputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}

public async Task HandleAsync( DeleteOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
}
#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@
using System.Threading.Tasks;

using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

namespace KeySwitchManager.Interactors.KeySwitches
{
public class DumpFileInteractor : IDumpFileUseCase
public class DumpInteractor : IDumpUseCase
{
private IKeySwitchRepository Repository { get; }
private IExportStrategy Strategy { get; }
private IDumpFilePresenter Presenter { get; }
private IDumpPresenter Presenter { get; }

public DumpFileInteractor(
IKeySwitchRepository repository, IExportStrategy strategy ) :
this( repository, strategy, new IDumpFilePresenter.Null() ) {}

public DumpFileInteractor(
public DumpInteractor(
IKeySwitchRepository repository,
IExportStrategy strategy,
IDumpFilePresenter presenter )
IDumpPresenter presenter )
{
Repository = repository;
Strategy = strategy;
Strategy = strategy;
Presenter = presenter;
}

public async Task<DumpFileResponse> ExecuteAsync( DumpFileRequest request, CancellationToken cancellationToken )
public async Task HandleAsync( UnitInputData inputData, CancellationToken cancellationToken = default )
{
var all = await Repository.FindAllAsync( cancellationToken );

var sorted = all.OrderBy( x => x.DeveloperName.Value )
.ThenBy( x => x.ProductName.Value )
.ThenBy( x => x.InstrumentName.Value ).ToList();

if( sorted.Count > 0 )
var dumpedCount = sorted.Count;

if( dumpedCount > 0 )
{
await Strategy.ExportAsync( sorted, cancellationToken );
return new DumpFileResponse( sorted.Count );
}

Presenter.Present( $"No keyswitch(es) found." );
return new DumpFileResponse( 0 );
var output = new DumpOutputData( true, new DumpOutputValue( dumpedCount ) );

await Presenter.HandleAsync( output, cancellationToken );
}
}
}
14 changes: 14 additions & 0 deletions KeySwitchManager/Sources/Runtime/UseCases/Commons/IInputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ public interface IInputData<out TData>
TData Value { get; }
}

public abstract class InputData<TData> : IInputData<TData>
{
public virtual TData Value { get; }

protected InputData( TData value )
{
Value = value;
}
}

public sealed class UnitInputData : IInputData<Unit>
{
public static readonly UnitInputData Default = new();

public Unit Value => Unit.Default;

private UnitInputData() {}
}
}
Loading

0 comments on commit e9e4aef

Please sign in to comment.