mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-21 02:17:20 +02:00
Operations pipeline start
This commit is contained in:
1 parent
6c10c6c654
commit
7ffbeb6b17
19 files changed
+432
-5
No files matched your search
+23
@@ -0,0 +1,23 @@
|
||||
using WotBlitzStatisticsPro.Common.Model;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline
|
||||
{
|
||||
public class AccountInformationPipelineContext
|
||||
{
|
||||
public AccountInformationPipelineContext(
|
||||
long accountId,
|
||||
RealmType realmType,
|
||||
RequestLanguage requestLanguage)
|
||||
{
|
||||
AccountId = accountId;
|
||||
RealmType = realmType;
|
||||
RequestLanguage = requestLanguage;
|
||||
}
|
||||
|
||||
|
||||
public long AccountId { get; }
|
||||
public RealmType RealmType { get; }
|
||||
public RequestLanguage RequestLanguage { get; }
|
||||
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using WotBlitzStatisticsPro.Logic.Pipeline;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
|
||||
{
|
||||
public class GetAccountInfoOperation: IOperation<AccountInformationPipelineContext>
|
||||
{
|
||||
public Task Invoke(AccountInformationPipelineContext context, Func<AccountInformationPipelineContext, Task> next)
|
||||
{
|
||||
return next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using WotBlitzStatisticsPro.Logic.Pipeline;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
|
||||
{
|
||||
public class GetTanksInfoOperation: IOperation<AccountInformationPipelineContext>
|
||||
{
|
||||
public Task Invoke(AccountInformationPipelineContext context, Func<AccountInformationPipelineContext, Task> next)
|
||||
{
|
||||
return next.Invoke(context);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
using WotBlitzStatisticsPro.Common.Model;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic
|
||||
{
|
||||
public interface IWargamingAccounts
|
||||
{
|
||||
Task<bool> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents pipeline single operation
|
||||
/// </summary>
|
||||
public interface IOperation<TContext>
|
||||
{
|
||||
/// <summary>
|
||||
/// Invokes the pipeline operation
|
||||
/// </summary>
|
||||
/// <param name="context">Pipeline context</param>
|
||||
/// <param name="next">Next operation</param>
|
||||
Task Invoke(TContext context, Func<TContext, Task> next);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// Factory creates the operation instances.
|
||||
/// </summary>
|
||||
public interface IOperationFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates new operation instance
|
||||
/// </summary>
|
||||
/// <typeparam name="TContext"></typeparam>
|
||||
/// <param name="operationType"></param>
|
||||
/// <returns></returns>
|
||||
IOperation<TContext> Create<TContext>(Type operationType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents operation pipeline
|
||||
/// </summary>
|
||||
public interface IPipeline<TContext>
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds the pipeline.
|
||||
/// </summary>
|
||||
/// <returns>First operation in pipeline.</returns>
|
||||
IOperation<TContext> Build();
|
||||
|
||||
/// <summary>
|
||||
/// Adds operation type to the pipeline
|
||||
/// </summary>
|
||||
/// <typeparam name="TOperation">Operation type</typeparam>
|
||||
/// <returns>Pipeline instance</returns>
|
||||
IPipeline<TContext> AddOperation<TOperation>() where TOperation : IOperation<TContext>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
public class OperationMediator<TContext>: IOperation<TContext>
|
||||
{
|
||||
private readonly IOperation<TContext> _current;
|
||||
private readonly IOperation<TContext> _next;
|
||||
|
||||
public OperationMediator(IOperation<TContext> current, IOperation<TContext> next)
|
||||
{
|
||||
_current = current ?? throw new ArgumentNullException(nameof(current));
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public Task Invoke(TContext context, Func<TContext, Task> next)
|
||||
{
|
||||
return _current.Invoke(context, c => _next == null ? Task.CompletedTask : _next.Invoke(c, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
public class Pipeline<TContext>: IPipeline<TContext>
|
||||
{
|
||||
private readonly IOperationFactory _operationFactory;
|
||||
private readonly List<Type> _operations = new List<Type>();
|
||||
|
||||
public Pipeline(IOperationFactory operationFactory)
|
||||
{
|
||||
_operationFactory = operationFactory;
|
||||
}
|
||||
|
||||
public IOperation<TContext> Build()
|
||||
{
|
||||
var allOperationTypes = _operations.ToArray();
|
||||
var lastOperationType = allOperationTypes.LastOrDefault();
|
||||
if (lastOperationType == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var nextOperation = new OperationMediator<TContext>(_operationFactory.Create<TContext>(lastOperationType), null);
|
||||
|
||||
for (int i = allOperationTypes.Length - 2; i >= 0; i--)
|
||||
{
|
||||
nextOperation = new OperationMediator<TContext>(_operationFactory.Create<TContext>(allOperationTypes[i]), nextOperation);
|
||||
}
|
||||
|
||||
return nextOperation;
|
||||
}
|
||||
|
||||
public IPipeline<TContext> AddOperation<TOperation>() where TOperation : IOperation<TContext>
|
||||
{
|
||||
_operations.Add(typeof(TOperation));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic.Pipeline
|
||||
{
|
||||
public class ServiceProviderOperationFactory: IOperationFactory
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public ServiceProviderOperationFactory(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public IOperation<TContext> Create<TContext>(Type operationType)
|
||||
{
|
||||
return _serviceProvider.GetService(operationType) as IOperation<TContext>;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Threading.Tasks;
|
||||
using WotBlitzStatisticsPro.Common.Model;
|
||||
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline;
|
||||
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations;
|
||||
using WotBlitzStatisticsPro.Logic.Pipeline;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Logic
|
||||
{
|
||||
public class WargamingAccounts: IWargamingAccounts
|
||||
{
|
||||
private readonly IOperationFactory _operationFactory;
|
||||
|
||||
public WargamingAccounts(IOperationFactory operationFactory)
|
||||
{
|
||||
_operationFactory = operationFactory;
|
||||
}
|
||||
|
||||
public async Task<bool> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage)
|
||||
{
|
||||
var context = new AccountInformationPipelineContext(accountId, realm, requestLanguage);
|
||||
var pipeline = new Pipeline<AccountInformationPipelineContext>(_operationFactory);
|
||||
|
||||
pipeline.AddOperation<GetAccountInfoOperation>()
|
||||
.AddOperation<GetTanksInfoOperation>()
|
||||
;
|
||||
// ToDo: add other operations
|
||||
|
||||
await pipeline.Build()
|
||||
.Invoke(context, null)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
// ToDo: return context response
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ using AutoMapper;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using WotBlitzStatisticsPro.Common.Model;
|
||||
using WotBlitzStatisticsPro.DataAccess;
|
||||
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations;
|
||||
using WotBlitzStatisticsPro.Logic.Dictionaries;
|
||||
using WotBlitzStatisticsPro.Logic.Pipeline;
|
||||
using WotBlitzStatisticsPro.WgApiClient;
|
||||
|
||||
[assembly: InternalsVisibleTo("WotBlitzStatisticsPro.Tests")]
|
||||
@@ -17,12 +19,14 @@ namespace WotBlitzStatisticsPro.Logic
|
||||
public static void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
ConfigureDictionariesFactory(services);
|
||||
ConfigureOperationsFactory(services);
|
||||
WotBlitzStatisticsDataAccessInstaller.ConfigureServices(services);
|
||||
services.AddAutoMapper(typeof(WotBlitzStatisticsLogicInstaller));
|
||||
services.AddHttpClient<IWargamingApiClient, WargamingApiClient>();
|
||||
services.AddHttpClient<IWargamingDictionariesApiClient, WargamingApiClient>();
|
||||
services.AddTransient<IWargamingSearch, WargamingSearch>();
|
||||
services.AddTransient<IWargamingDictionaries, WargamingDictionaries>();
|
||||
services.AddTransient<IWargamingAccounts, WargamingAccounts>();
|
||||
}
|
||||
|
||||
private static void ConfigureDictionariesFactory(IServiceCollection services)
|
||||
@@ -31,10 +35,10 @@ namespace WotBlitzStatisticsPro.Logic
|
||||
services.AddTransient<AchievementsDictionaryUpdater>();
|
||||
services.AddTransient<VehiclesDictionaryUpdater>();
|
||||
|
||||
RegisterFactoryMethod(services);
|
||||
RegisterDictionariesFactoryMethod(services);
|
||||
}
|
||||
|
||||
internal static void RegisterFactoryMethod(IServiceCollection services)
|
||||
internal static void RegisterDictionariesFactoryMethod(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<DictionariesUpdaterResolver>(serviceProvider => dictionaryType =>
|
||||
{
|
||||
@@ -51,5 +55,14 @@ namespace WotBlitzStatisticsPro.Logic
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void ConfigureOperationsFactory(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<GetAccountInfoOperation>();
|
||||
services.AddTransient<GetTanksInfoOperation>();
|
||||
|
||||
services.AddTransient<IOperationFactory>(serviceProvider =>
|
||||
new ServiceProviderOperationFactory(serviceProvider));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user