From c5e9a0e48c3dd08ea03ab10f8c23bb4e4ca57f34 Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Wed, 19 Apr 2023 13:04:04 +0200 Subject: [PATCH] API Controllers Factory whit dependency injection --- .../APIControllerFactory.cs | 56 +++++++++++++++++++ .../App_Infrastructure/ControllerFactory.cs | 1 + .../ControllersExtensions.cs | 20 +++++++ .../App_Start/ServicesConfig.cs | 1 + .../LaaProductionWeb/LaaProductionWeb.csproj | 2 + .../LaaProductionWeb/Rest/RestController.cs | 13 +++++ 6 files changed, 93 insertions(+) create mode 100644 LaaProductionWeb/LaaProductionWeb/App_Infrastructure/APIControllerFactory.cs create mode 100644 LaaProductionWeb/LaaProductionWeb/Rest/RestController.cs diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/APIControllerFactory.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/APIControllerFactory.cs new file mode 100644 index 00000000..c9f29e11 --- /dev/null +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/APIControllerFactory.cs @@ -0,0 +1,56 @@ +namespace LaaProductionWeb.App_Infrastructure +{ + using LaaProductionWeb.Rest; + + using System; + using System.Collections.Generic; + using System.Net.Http; + using System.Web.Http; + using System.Web.Http.Controllers; + using System.Web.Http.Dispatcher; + + public class APIControllerActivator : IHttpControllerActivator + { + private readonly IServiceProvider serviceProvider; + + public APIControllerActivator(IServiceProvider serviceProvider) + => this.serviceProvider = serviceProvider; + + public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) + => this.serviceProvider.GetService(controllerType) as IHttpController; + } + + public class APIControllerFactory : IHttpControllerSelector + { + public HttpControllerDescriptor SelectController(HttpRequestMessage request) + { + var routeData = request.GetRouteData(); + var controllerName = default(string); + var controllerType = default(Type); + + if (routeData.Values.TryGetValue("controller", out object controllerValue)) + { + controllerName = $"{controllerValue}Controller"; + } + + if (routeData.Values.TryGetValue("area", out object area)) + { + controllerType = Type.GetType($"LaaProductionWeb.Areas.{area}.Rest.{controllerName}", false, true); + } + else + { + controllerType = Type.GetType($"LaaProductionWeb.Rest.{controllerName}", false, true); + } + + return new HttpControllerDescriptor() + { + ControllerName = controllerName, + ControllerType = controllerType, + Configuration = GlobalConfiguration.Configuration + }; + } + + public IDictionary GetControllerMapping() + => new Dictionary(); + } +} \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs index 46846efb..cb323330 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllerFactory.cs @@ -1,5 +1,6 @@ namespace LaaProductionWeb.App_Infrastructure { + using System; using System.Web; using System.Web.Mvc; diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllersExtensions.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllersExtensions.cs index b50cecd8..9706cf45 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllersExtensions.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/ControllersExtensions.cs @@ -9,6 +9,9 @@ using System.Reflection; using System.Security.Claims; using System.Security.Principal; + using System.Web.Http; + using System.Web.Http.Controllers; + using System.Web.Http.Dispatcher; using System.Web.Mvc; public static class ControllersExtensions @@ -28,12 +31,29 @@ return services; } + public static IServiceCollection AddAPIControllers(this IServiceCollection services) + { + var controllers = typeof(ControllersExtensions) + .Assembly + .GetTypes() + .Where(x => !x.IsAbstract + && typeof(IHttpController).IsAssignableFrom(x)); + + foreach (var controller in controllers) + { + services.AddTransient(controller); + } + + return services; + } public static IServiceProvider BuildControllerFactory(this IServiceProvider serviceProvider) { var controllerFactory = new ControllerFactory(serviceProvider); ControllerBuilder.Current.SetControllerFactory(controllerFactory); + GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new APIControllerActivator(serviceProvider)); + GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new APIControllerFactory()); return serviceProvider; } diff --git a/LaaProductionWeb/LaaProductionWeb/App_Start/ServicesConfig.cs b/LaaProductionWeb/LaaProductionWeb/App_Start/ServicesConfig.cs index d203e145..e309bd19 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Start/ServicesConfig.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Start/ServicesConfig.cs @@ -19,6 +19,7 @@ { services .AddControllers() + .AddAPIControllers() .AddTransientServices() .AddDbContext(ApplicationSettings.ConnectionString); diff --git a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj index 39459a58..0234c269 100644 --- a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj +++ b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj @@ -150,6 +150,7 @@ + @@ -175,6 +176,7 @@ Global.asax + diff --git a/LaaProductionWeb/LaaProductionWeb/Rest/RestController.cs b/LaaProductionWeb/LaaProductionWeb/Rest/RestController.cs new file mode 100644 index 00000000..8142bf78 --- /dev/null +++ b/LaaProductionWeb/LaaProductionWeb/Rest/RestController.cs @@ -0,0 +1,13 @@ +namespace LaaProductionWeb.Rest +{ + using System.Web.Http; + using System.Web.Http.Results; + + public class RestController : ApiController + { + public JsonResult Get() + { + return this.Json("it works"); + } + } +} \ No newline at end of file