Skip to content

Commit 6a18496

Browse files
committed
[edit] Controller2Executor impl and enabled
1 parent 77d2c50 commit 6a18496

6 files changed

Lines changed: 98 additions & 4 deletions

File tree

src/Simplify.Web/Bootstrapper/Setup/BaseBootstrapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void Register(IEnumerable<Type>? typesToExclude = null)
3939
// Registering Simplify.Web types
4040

4141
RegisterIController1Factory();
42+
RegisterIController2Factory();
4243
RegisterControllerExecutorResolver();
4344
RegisterControllerExecutorResolverExecutors();
4445
RegisterControllersExecutor();

src/Simplify.Web/Bootstrapper/Setup/BaseBootstrapperControllersExecution.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Simplify.Web.Controllers.Execution.Resolver;
66
using Simplify.Web.Controllers.Response;
77
using Simplify.Web.Controllers.V1.Execution;
8+
using Simplify.Web.Controllers.V2.Execution;
89
using Simplify.Web.Modules.Redirection;
910

1011
namespace Simplify.Web.Bootstrapper.Setup;
@@ -29,6 +30,7 @@ public virtual void RegisterControllerExecutorResolverExecutors()
2930

3031
BootstrapperFactory.ContainerProvider.Register<IReadOnlyList<IControllerExecutor>>(r =>
3132
[
33+
new Controller2Executor(r.Resolve<IController2Factory>()),
3234
new Controller1Executor(r.Resolve<IController1Factory>())
3335
]);
3436
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Linq;
2+
using Simplify.DI;
3+
using Simplify.Web.Controllers.V2.Execution;
4+
5+
namespace Simplify.Web.Bootstrapper.Setup;
6+
7+
/// <summary>
8+
/// Provides the bootstrapper v1 controllers registration.
9+
/// </summary>
10+
public partial class BaseBootstrapper
11+
{
12+
public virtual void RegisterIController2Factory()
13+
{
14+
if (TypesToExclude.Contains(typeof(IController2Factory)))
15+
return;
16+
17+
BootstrapperFactory.ContainerProvider.Register<IController2Factory, Controller2Factory>();
18+
}
19+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Simplify.Web.Controllers.Execution;
7+
using Simplify.Web.Controllers.Meta;
8+
using Simplify.Web.Controllers.V2.Metadata;
9+
10+
namespace Simplify.Web.Controllers.V2.Execution;
11+
12+
/// <summary>
13+
/// Provides v2 controller executor.
14+
/// </summary>
15+
/// <param name="controllerFactory">The v2 controller factory.</param>
16+
public class Controller2Executor(IController2Factory controllerFactory) : IControllerExecutor
17+
{
18+
public bool CanHandle(IControllerMetadata controllerMetadata) => controllerMetadata is IController2Metadata;
19+
20+
public Task<ControllerResponse?> ExecuteAsync(IMatchedController matchedController, HttpContext context)
21+
{
22+
var controllerMetadata = (IController2Metadata)matchedController.Controller;
23+
var controller = controllerFactory.CreateController(matchedController);
24+
var methodParams = ConstructMethodParams(controllerMetadata.InvokeMethodParameters, matchedController.RouteParameters!);
25+
26+
return InvokeAsync(controllerMetadata.InvokeMethodInfo, methodParams, controller);
27+
}
28+
29+
private static async Task<ControllerResponse?> InvokeAsync(MethodInfo methodInfo, IList<object> methodParams,
30+
ResponseShortcutsControllerBase controller)
31+
{
32+
var result = methodInfo.Invoke(controller, [.. methodParams]);
33+
34+
switch (result)
35+
{
36+
case ControllerResponse response:
37+
return response;
38+
39+
case Task<ControllerResponse> response:
40+
await response;
41+
return response.Result;
42+
43+
case Task task:
44+
await task;
45+
break;
46+
}
47+
48+
return null;
49+
}
50+
51+
private static IList<object> ConstructMethodParams(IDictionary<string, Type> invokeMethodParameters, IReadOnlyDictionary<string, object> routeParameters)
52+
{
53+
var result = new List<object>(invokeMethodParameters.Count);
54+
55+
foreach (var item in invokeMethodParameters.Keys)
56+
{
57+
if (!routeParameters.TryGetValue(item, out var parameter))
58+
throw new InvalidOperationException($"Route parameter with the name '{item}' is not found.");
59+
60+
result.Add(parameter);
61+
}
62+
63+
return result;
64+
}
65+
}

src/Simplify.Web/Controllers/V2/Metadata/Controller2Metadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Controller2Metadata(Type controllerType) : base(controllerType)
1919
}
2020

2121
public MethodInfo InvokeMethodInfo { get; }
22-
public IDictionary<string, Type> InvokeMethodParameters { get; set; }
22+
public IDictionary<string, Type> InvokeMethodParameters { get; }
2323

2424
protected override IControllerRoute BuildControllerRoute(string path) => new Controller2Route(path, InvokeMethodParameters);
2525

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
using Simplify.Web.Controllers.Meta;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using Simplify.Web.Controllers.Meta;
25

36
namespace Simplify.Web.Controllers.V2.Metadata;
47

58
/// <summary>
6-
/// Represent a controller v2 metadata information.
9+
/// Represents a controller v2 metadata information.
710
/// </summary>
8-
public interface IController2Metadata : IControllerMetadata;
11+
public interface IController2Metadata : IControllerMetadata
12+
{
13+
public MethodInfo InvokeMethodInfo { get; }
14+
public IDictionary<string, Type> InvokeMethodParameters { get; }
15+
}

0 commit comments

Comments
 (0)