Created
August 20, 2020 21:17
-
-
Save mastoj/11a3bcdfb8693489cbddb013b2a93f4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace MinimalDispatcher | |
{ | |
public interface IHandle<TRequest, TResponse> { | |
Task<TResponse> Handle(TRequest request); | |
} | |
public interface DispatcherMiddleware { | |
void PreHook(object request) { | |
} | |
void PostHook(object request) { | |
} | |
} | |
public class Dispatcher | |
{ | |
private readonly Func<Type, object> getService; | |
private readonly Func<Type, IEnumerable<object>> getServices; | |
public Dispatcher(Func<Type, object> getService, Func<Type, IEnumerable<object>> getServices) | |
{ | |
this.getService = getService; | |
this.getServices = getServices; | |
} | |
public async Task<TResponse> Handle<TRequest, TResponse>(TRequest request) | |
{ | |
var middlewares = getServices(typeof(DispatcherMiddleware)).Select(m => m as DispatcherMiddleware).ToList(); | |
middlewares.ForEach(m => m.PreHook(request)); | |
var handler = getService(typeof(IHandle<TRequest, TResponse>)) as IHandle<TRequest, TResponse>; | |
var response = await handler.Handle(request); | |
middlewares.ForEach(m => m.PreHook(request)); | |
return response; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace dispatch | |
{ | |
public class MyType { | |
public string Wat => "Yolo"; | |
public override string ToString() | |
{ | |
return "Wat: Yolo"; | |
} | |
} | |
public class MyTypeHandler : IHandle<MyType, string> | |
{ | |
private readonly ILogger _logger; | |
public MyTypeHandler(ILogger<MyTypeHandler> logger){ | |
_logger = logger; | |
} | |
public Task<string> Handle(MyType request) | |
{ | |
_logger.LogInformation("Handling stuff"); | |
return Task.FromResult("Hello world"); | |
} | |
} | |
public class MyMiddleWare : DispatcherMiddleware { | |
public async void PreHook(object request) { | |
Console.WriteLine("Pre hook: " + request); | |
await Task.CompletedTask; | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args).Build().Run(); | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureLogging(logging => | |
{ | |
logging.ClearProviders(); | |
logging.AddConsole(); | |
}) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using MinimalDispatcher; | |
namespace dispatch | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers(); | |
services.AddTransient<IHandle<MyType, string>, MyTypeHandler>(); | |
services.AddTransient<DispatcherMiddleware, MyMiddleWare>(); | |
services.AddTransient<Dispatcher>(sp => { | |
var dispatcher = new Dispatcher(sp.GetService, sp.GetServices); | |
return dispatcher; | |
}); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseRouting(); | |
app.UseAuthorization(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllers(); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment