рефакторинг модуля исполнения

This commit is contained in:
2025-01-14 14:16:08 +03:00
parent 9128f3f6ee
commit d9e3e95dda
7 changed files with 72 additions and 55 deletions
@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Performance")] [assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Performance")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2f1a1e250b1a9c031df21e3ec1087cb18b8fa784")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9128f3f6ee0d3c89e7e0538ac244a133c746e4df")]
[assembly: System.Reflection.AssemblyProductAttribute("mROA.Performance")] [assembly: System.Reflection.AssemblyProductAttribute("mROA.Performance")]
[assembly: System.Reflection.AssemblyTitleAttribute("mROA.Performance")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA.Performance")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -7,7 +7,7 @@
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders> <NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.2</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" /> <SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Test")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2f1a1e250b1a9c031df21e3ec1087cb18b8fa784")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9128f3f6ee0d3c89e7e0538ac244a133c746e4df")]
[assembly: System.Reflection.AssemblyProductAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyProductAttribute("mROA.Test")]
[assembly: System.Reflection.AssemblyTitleAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA.Test")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
+1 -1
View File
@@ -7,7 +7,7 @@
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders> <NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.2</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" /> <SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
+44 -28
View File
@@ -17,7 +17,6 @@ public class PrepairedExecutionModule : IExecuteModule
_serialisationModule = serialisationModule; _serialisationModule = serialisationModule;
_contextRepo = contextRepo; _contextRepo = contextRepo;
_serialisationModule.SetExecuteModule(this); _serialisationModule.SetExecuteModule(this);
} }
public ICommandExecution Execute(ICallRequest command) public ICommandExecution Execute(ICallRequest command)
@@ -26,10 +25,52 @@ public class PrepairedExecutionModule : IExecuteModule
if (currentCommand == null) if (currentCommand == null)
throw new Exception($"Command {command.CommandId} not found"); throw new Exception($"Command {command.CommandId} not found");
var context = command.ObjectId != -1 ? _contextRepo.GetObject(command.ObjectId) : _contextRepo.GetSingleObject(currentCommand.DeclaringType); var context = command.ObjectId != -1
? _contextRepo.GetObject(command.ObjectId)
: _contextRepo.GetSingleObject(currentCommand.DeclaringType);
var parameter = command.Parameter; var parameter = command.Parameter;
if (currentCommand.ReturnType.BaseType == typeof(Task) && currentCommand.ReturnType.GenericTypeArguments.Length == 1) if (currentCommand.ReturnType.BaseType == typeof(Task) &&
currentCommand.ReturnType.GenericTypeArguments.Length == 1)
return TypedExecuteAsync(currentCommand, context, parameter, command);
if (currentCommand.ReturnType == typeof(Task))
return ExecuteAsync(currentCommand, context, parameter, command);
return Execute(currentCommand, context, parameter, command);
}
private ICommandExecution Execute(MethodInfo currentCommand, object context, object parameter, ICallRequest request)
{
var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]);
return new FinalCommandExecution
{ CommandId = request.CommandId, Result = finalResult, ClientId = request.ClientId };
}
private ICommandExecution ExecuteAsync(MethodInfo currentCommand, object context, object parameter,
ICallRequest command)
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var result = currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task;
var exec = new AsyncCommandExecution(tokenSource)
{ CommandId = command.CommandId, ClientId = command.ClientId };
result.ContinueWith(_ =>
{
_serialisationModule.PostResponse(new FinalCommandExecution
{
ExecutionId = exec.ExecutionId, Result = null, CommandId = command.CommandId,
ClientId = command.ClientId
});
}, token);
return exec;
}
private ICommandExecution TypedExecuteAsync(MethodInfo currentCommand, object context, object parameter,
ICallRequest command)
{ {
var tokenSource = new CancellationTokenSource(); var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token; var token = tokenSource.Token;
@@ -51,29 +92,4 @@ public class PrepairedExecutionModule : IExecuteModule
return exec; return exec;
} }
if (currentCommand.ReturnType == typeof(Task))
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var result = currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task;
var exec = new AsyncCommandExecution(tokenSource)
{ CommandId = command.CommandId, ClientId = command.ClientId };
result.ContinueWith(_ =>
{
_serialisationModule.PostResponse(new FinalCommandExecution
{
ExecutionId = exec.ExecutionId, Result = null, CommandId = command.CommandId,
ClientId = command.ClientId
});
}, token);
return exec;
}
var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]);
return new FinalCommandExecution { CommandId = command.CommandId, Result = finalResult };
}
} }
+5 -4
View File
@@ -1,9 +1,10 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// <auto-generated> // <auto-generated>
// This code was generated by a tool. // Этот код создан программой.
// Исполняемая версия:4.0.30319.42000
// //
// Changes to this file may cause incorrect behavior and will be lost if // Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
// the code is regenerated. // повторной генерации кода.
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -13,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("mROA")] [assembly: System.Reflection.AssemblyCompanyAttribute("mROA")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2f1a1e250b1a9c031df21e3ec1087cb18b8fa784")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9128f3f6ee0d3c89e7e0538ac244a133c746e4df")]
[assembly: System.Reflection.AssemblyProductAttribute("mROA")] [assembly: System.Reflection.AssemblyProductAttribute("mROA")]
[assembly: System.Reflection.AssemblyTitleAttribute("mROA")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
+1 -1
View File
@@ -7,7 +7,7 @@
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders> <NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.2</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" /> <SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />